Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix vflip buffers
Browse files Browse the repository at this point in the history
Vipitis committed Jun 6, 2024
1 parent 12bff83 commit 3221ea7
Showing 2 changed files with 67 additions and 17 deletions.
16 changes: 10 additions & 6 deletions wgpu_shadertoy/inputs.py
Original file line number Diff line number Diff line change
@@ -178,24 +178,28 @@ def bind_texture(self, device: wgpu.GPUDevice) -> Tuple[list, list]:
)
return binding_layout, bind_groups_layout_entry

def header_glsl(self, input_idx=0):
def header_glsl(self):
"""
GLSL code snippet added to the compatibilty header for Shadertoy inputs.
"""
binding_id = (2 * input_idx) + 1
sampler_id = 2 * (input_idx + 1)
input_idx = self.channel_idx
binding_id = self.texture_binding
sampler_id = self.sampler_binding
return f"""
layout(binding = {binding_id}) uniform texture2D i_channel{input_idx};
layout(binding = {sampler_id}) uniform sampler sampler{input_idx};
#define iChannel{input_idx} sampler2D(i_channel{input_idx}, sampler{input_idx})
"""

def header_wgsl(self, input_idx=0):
def header_wgsl(self):
"""
WGSL code snippet added to the compatibilty header for Shadertoy inputs.
"""
binding_id = (2 * input_idx) + 1
sampler_id = 2 * (input_idx + 1)
input_idx = self.channel_idx
binding_id = self.texture_binding
sampler_id = self.sampler_binding
return f"""
@group(0) @binding{binding_id}
var i_channel{input_idx}: texture_2d<f32>;
68 changes: 57 additions & 11 deletions wgpu_shadertoy/passes.py
Original file line number Diff line number Diff line change
@@ -24,6 +24,25 @@
}
}
"""
# TODO: avoid redundant globals, refactor to something like a headers.py file?
vertex_code_glsl_flipped = """#version 450 core
layout(location = 0) out vec2 vert_uv;
void main(void){
int index = int(gl_VertexID);
if (index == 0) {
gl_Position = vec4(-1.0, -1.0, 0.0, 1.0);
vert_uv = vec2(0.0, 0.0); // Flipped
} else if (index == 1) {
gl_Position = vec4(3.0, -1.0, 0.0, 1.0);
vert_uv = vec2(2.0, 0.0); // Flipped
} else {
gl_Position = vec4(-1.0, 3.0, 0.0, 1.0);
vert_uv = vec2(0.0, 2.0); // Flipped
}
}
"""


builtin_variables_glsl = """#version 450 core
@@ -126,6 +145,31 @@
}
"""

vertex_code_wgsl_flipped = """
struct Varyings {
@builtin(position) position : vec4<f32>,
@location(0) vert_uv : vec2<f32>,
};
@vertex
fn main(@builtin(vertex_index) index: u32) -> Varyings {
var out: Varyings;
if (index == u32(0)) {
out.position = vec4<f32>(-1.0, -1.0, 0.0, 1.0);
out.vert_uv = vec2<f32>(0.0, 0.0); // Flipped
} else if (index == u32(1)) {
out.position = vec4<f32>(3.0, -1.0, 0.0, 1.0);
out.vert_uv = vec2<f32>(2.0, 0.0); // Flipped
} else {
out.position = vec4<f32>(-1.0, 3.0, 0.0, 1.0);
out.vert_uv = vec2<f32>(0.0, -2.0); // Flipped
}
return out;
}
"""


builtin_variables_wgsl = """
@@ -329,15 +373,21 @@ def prepare_render(self, device: wgpu.GPUDevice) -> None:
self.channels = self._attach_inputs(self._inputs)
shader_type = self.shader_type
if shader_type == "glsl":
vertex_shader_code = vertex_code_glsl
if type(self) is BufferRenderPass:
vertex_shader_code = vertex_code_glsl_flipped
else:
vertex_shader_code = vertex_code_glsl
frag_shader_code = (
builtin_variables_glsl
+ self.main.common
+ self.shader_code
+ fragment_code_glsl
)
elif shader_type == "wgsl":
vertex_shader_code = vertex_code_wgsl
if type(self) is BufferRenderPass:
vertex_shader_code = vertex_code_wgsl_flipped
else:
vertex_shader_code = vertex_code_wgsl
frag_shader_code = (
builtin_variables_wgsl
+ self.main.common
@@ -526,15 +576,13 @@ def texture_size(self) -> tuple:
def _pad_columns(self, cols: int, alignment=64) -> int:
if cols % alignment != 0:
cols = (cols // alignment + 1) * alignment
print(cols)
return cols

def resize(self, new_cols: int, new_rows: int) -> None:
"""
resizes the buffer to a new speicified size.
Downscaling keeps the top leftmost corner,
upscaling pads the bottom and right with black.
(this becomes bottom left, after vflip).
Downscaling keeps the bottom left corner,
upscaling pads the top and right with black.
"""
# TODO: could this be redone as a compute shader?

@@ -544,12 +592,10 @@ def resize(self, new_cols: int, new_rows: int) -> None:
return
old = self._download_texture()
if new_rows < old_rows or new_cols < old_cols:
new = old[-new_rows:, :new_cols, :]
new = old[:new_rows, :new_cols]
else:
new = np.pad(
old,
((new_rows - old_rows, 0), (0, new_cols - old_cols), (0, 0)),
mode="constant",
old, ((0, new_rows - old_rows), (0, new_cols - old_cols), (0, 0))
)
self._upload_texture(new)

@@ -677,7 +723,7 @@ def _download_texture(
frame = device.queue.read_buffer(buffer)
frame = np.frombuffer(frame, dtype=np.uint8).reshape(size[1], size[0], 4)
# redundant copy?
self._last_frame = frame
# self._last_frame = frame
return frame

def _upload_texture(self, data, device=None, command_encoder=None):

0 comments on commit 3221ea7

Please sign in to comment.