How to use the wgpu.TextureFormat function in wgpu

To help you get started, we’ve selected a few wgpu examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github almarklein / wgpu-py / tests / test_rs_basics.py View on Github external
def test_write_texture1():
    device = wgpu.utils.get_default_device()

    nx, ny, nz = 100, 1, 1
    data1 = memoryview(np.random.random(size=100).astype(np.float32))
    bpp = data1.nbytes // (nx * ny * nz)
    texture_format = wgpu.TextureFormat.r32float
    texture_dim = wgpu.TextureDimension.d1

    # Create buffers and textures
    tex3 = device.create_texture(
        size=(nx, ny, nz),
        dimension=texture_dim,
        format=texture_format,
        usage=wgpu.TextureUsage.COPY_SRC | wgpu.TextureUsage.COPY_DST,
    )
    buf4 = device.create_buffer(
        size=data1.nbytes, usage=wgpu.BufferUsage.COPY_DST | wgpu.BufferUsage.MAP_READ
    )

    # Upload from CPU to texture
    command_encoder = device.create_command_encoder()
    device.default_queue.write_texture(
github almarklein / wgpu-py / tests / test_rs_render_tex.py View on Github external
def fragment_shader(
        tex: ("texture", 0, "2d f32"),
        sampler: ("sampler", 1, ""),
        tcoord: ("input", 0, vec2),
        out_color: ("output", 0, vec4),
    ):
        out_color = tex.sample(sampler, tcoord)  # noqa

    # Create texture data
    nx, ny, nz = 2, 2, 1
    x = [50, 50, 0, 255, 100, 100, 0, 255, 150, 150, 0, 255, 200, 200, 0, 255]
    texture_data = (ctypes.c_uint8 * (4 * nx * ny))(*x)

    # Render and validate
    render_textured_square(
        fragment_shader, wgpu.TextureFormat.rgba8unorm, (nx, ny, nz), texture_data
    )
github almarklein / wgpu-py / tests / test_gui_glfw.py View on Github external
render_pipeline = device.create_render_pipeline(
        layout=pipeline_layout,
        vertex_stage={"module": vshader, "entry_point": "main"},
        fragment_stage={"module": fshader, "entry_point": "main"},
        primitive_topology=wgpu.PrimitiveTopology.triangle_strip,
        rasterization_state={
            "front_face": wgpu.FrontFace.ccw,
            "cull_mode": wgpu.CullMode.none,
            "depth_bias": 0,
            "depth_bias_slope_scale": 0.0,
            "depth_bias_clamp": 0.0,
        },
        color_states=[
            {
                "format": wgpu.TextureFormat.bgra8unorm_srgb,
                "alpha_blend": (
                    wgpu.BlendFactor.one,
                    wgpu.BlendFactor.zero,
                    wgpu.BlendOperation.add,
                ),
                "color_blend": (
                    wgpu.BlendFactor.one,
                    wgpu.BlendFactor.zero,
                    wgpu.BlendOperation.add,
                ),
                "write_mask": wgpu.ColorWrite.ALL,
            }
        ],
        vertex_state={"index_format": wgpu.IndexFormat.uint32, "vertex_buffers": [],},
        sample_count=1,
        sample_mask=0xFFFFFFFF,
github almarklein / wgpu-py / tests / test_rs_compute_tex.py View on Github external
color = vec4(color.x + f32(index.x), color.y + 1.0, color.z * 2.0, color.a)
        tex2.write(index.xyz, color)

    # Generate data
    nx, ny, nz, nc = 7, 8, 6, 1
    data1 = (ctypes.c_float * nc * nx * ny * nz)()
    for z in range(nz):
        for y in range(ny):
            for x in range(nx):
                for c in range(nc):
                    data1[z][y][x][c] = random.randint(0, 20)

    # Compute and validate
    _compute_texture(
        compute_shader,
        wgpu.TextureFormat.r32float,
        wgpu.TextureDimension.d3,
        (nx, ny, nz, nc),
        data1,
    )
github almarklein / wgpu-py / tests / renderutils.py View on Github external
vbo_views=None,
    indirect_buffer=None,
    color_attachment=None,
    depth_stencil_state=None,
    depth_stencil_attachment=None,
    renderpass_callback=lambda *args: None,
):

    # https://github.com/gfx-rs/wgpu-rs/blob/master/examples/capture/main.rs

    vbos = vbos or []
    vbo_views = vbo_views or []

    # Select texture format. The srgb norm maps to the srgb colorspace which
    # appears to be the default for render pipelines https://en.wikipedia.org/wiki/SRGB
    texture_format = wgpu.TextureFormat.rgba8unorm  # rgba8unorm or bgra8unorm_srgb

    # Create texture to render to
    nx, ny, bpp = size[0], size[1], 4
    nbytes = nx * ny * bpp
    texture = device.create_texture(
        size=(nx, ny, 1),
        dimension=wgpu.TextureDimension.d2,
        format=texture_format,
        usage=wgpu.TextureUsage.OUTPUT_ATTACHMENT | wgpu.TextureUsage.COPY_SRC,
    )
    current_texture_view = texture.create_view()

    # Also a buffer to read the data to CPU
    buffer = device.create_buffer(
        size=nbytes, usage=wgpu.BufferUsage.MAP_READ | wgpu.BufferUsage.COPY_DST
    )
github almarklein / wgpu-py / tests / test_rs_render_tex.py View on Github external
tex: ("texture", 0, "2d i32"),
        sampler: ("sampler", 1, ""),
        tcoord: ("input", 0, vec2),
        out_color: ("output", 0, vec4),
    ):
        val = vec2(tex.sample(sampler, tcoord).rg)
        out_color = vec4(val.rg / 255.0, 0.0, 1.0)  # noqa

    # Create texture data
    nx, ny, nz = 2, 2, 1
    x = [50, 50, 100, 100, 150, 150, 200, 200]
    texture_data = (ctypes.c_int16 * (2 * nx * ny))(*x)

    # Render and validate
    render_textured_square(
        fragment_shader, wgpu.TextureFormat.rg16sint, (nx, ny, nz), texture_data
    )
github almarklein / wgpu-py / tests / test_rs_render.py View on Github external
bind_group = device.create_bind_group(layout=bind_group_layout, entries=[])
    pipeline_layout = device.create_pipeline_layout(
        bind_group_layouts=[bind_group_layout]
    )

    # Create dept-stencil texture
    depth_stencil_texture = device.create_texture(
        size=(64, 64, 1),  # when rendering to texture
        # size=(640, 480, 1),  # when rendering to screen
        dimension=wgpu.TextureDimension.d2,
        format=wgpu.TextureFormat.depth24plus_stencil8,
        usage=wgpu.TextureUsage.OUTPUT_ATTACHMENT,
    )

    depth_stencil_state = dict(
        format=wgpu.TextureFormat.depth24plus_stencil8,
        depth_write_enabled=True,
        depth_compare=wgpu.CompareFunction.less_equal,
        stencil_front={
            "compare": wgpu.CompareFunction.equal,
            "fail_op": wgpu.StencilOperation.keep,
            "depth_fail_op": wgpu.StencilOperation.keep,
            "pass_op": wgpu.StencilOperation.keep,
        },
        stencil_back={
            "compare": wgpu.CompareFunction.equal,
            "fail_op": wgpu.StencilOperation.keep,
            "depth_fail_op": wgpu.StencilOperation.keep,
            "pass_op": wgpu.StencilOperation.keep,
        },
        stencil_read_mask=0,
        stencil_write_mask=0,
github almarklein / wgpu-py / examples / cube_glfw.py View on Github external
render_pipeline = device.create_render_pipeline(
    layout=pipeline_layout,
    vertex_stage={"module": vshader, "entry_point": "main"},
    fragment_stage={"module": fshader, "entry_point": "main"},
    primitive_topology=wgpu.PrimitiveTopology.triangle_list,
    rasterization_state={
        "front_face": wgpu.FrontFace.ccw,
        "cull_mode": wgpu.CullMode.back,
        "depth_bias": 0,
        "depth_bias_slope_scale": 0.0,
        "depth_bias_clamp": 0.0,
    },
    color_states=[
        {
            "format": wgpu.TextureFormat.bgra8unorm_srgb,
            "alpha_blend": (
                wgpu.BlendFactor.one,
                wgpu.BlendFactor.zero,
                wgpu.BlendOperation.add,
            ),
            "color_blend": (
                wgpu.BlendFactor.one,
                wgpu.BlendFactor.zero,
                wgpu.BlendOperation.add,
            ),
        }
    ],
    vertex_state={
        "index_format": wgpu.IndexFormat.uint32,
        "vertex_buffers": [
            {
github almarklein / wgpu-py / examples / triangle.py View on Github external
render_pipeline = device.create_render_pipeline(
        layout=pipeline_layout,
        vertex_stage={"module": vshader, "entry_point": "main"},
        fragment_stage={"module": fshader, "entry_point": "main"},
        primitive_topology=wgpu.PrimitiveTopology.triangle_list,
        rasterization_state={
            "front_face": wgpu.FrontFace.ccw,
            "cull_mode": wgpu.CullMode.none,
            "depth_bias": 0,
            "depth_bias_slope_scale": 0.0,
            "depth_bias_clamp": 0.0,
        },
        color_states=[
            {
                "format": wgpu.TextureFormat.bgra8unorm_srgb,
                "alpha_blend": (
                    wgpu.BlendFactor.one,
                    wgpu.BlendFactor.zero,
                    wgpu.BlendOperation.add,
                ),
                "color_blend": (
                    wgpu.BlendFactor.one,
                    wgpu.BlendFactor.zero,
                    wgpu.BlendOperation.add,
                ),
            }
        ],
        vertex_state={"index_format": wgpu.IndexFormat.uint32, "vertex_buffers": []},
        sample_count=1,
        sample_mask=0xFFFFFFFF,
        alpha_to_coverage_enabled=False,
github almarklein / wgpu-py / examples / cube_glfw.py View on Github external
index_buffer = device.create_buffer_with_data(
    data=index_data, usage=wgpu.BufferUsage.INDEX
)

# Create uniform buffer - data is uploaded each frame
uniform_buffer = device.create_buffer(
    size=uniform_data.nbytes, usage=wgpu.BufferUsage.UNIFORM | wgpu.BufferUsage.COPY_DST
)


# Create texture, and upload data
texture = device.create_texture(
    size=texture_size,
    usage=wgpu.TextureUsage.COPY_DST | wgpu.TextureUsage.SAMPLED,
    dimension=wgpu.TextureDimension.d2,
    format=wgpu.TextureFormat.r8uint,
    mip_level_count=1,
    sample_count=1,
)
texture_view = texture.create_view()
tmp_buffer = device.create_buffer_with_data(
    data=texture_data, usage=wgpu.BufferUsage.COPY_SRC
)
command_encoder = device.create_command_encoder()
command_encoder.copy_buffer_to_texture(
    {
        "buffer": tmp_buffer,
        "offset": 0,
        "bytes_per_row": texture_data.strides[0],
        "rows_per_image": 0,
    },
    {"texture": texture_view, "mip_level": 0, "origin": (0, 0, 0),},