Git Product home page Git Product logo

Comments (2)

almarklein avatar almarklein commented on June 14, 2024

Hi! Great to see someone using this in practice!

Nethertheless, the bind groups, bind groups layouts are still making me puzzled.

That only means you're human :) I tried to explain it here. Any suggestions for improving this text are very welcome indeed. I think an image would also help a lot.

I think the main issue in your code is that in the shader you are defining one buffer object for each matrix, while you've actually combined the matrices into one buffer object (which is good practice in general). So your shader should be more like:

@python2shader
def tex_vertex_shader(
    in_pos: ("input", 0, vec4),
    in_texcoord: ("input", 1, vec2),
    out_pos: ("output", "Position", vec4),
    v_texcoord: ("output", 0, vec2),
    uni: ("uniform", (0, 2), uniform_type),   
):
    ndc = uni.projection * uni.view * uni.model * uni.transform * in_pos
    out_pos = vec4(ndc.xy, 0, 1)  # noqa - shader output
    v_texcoord = in_texcoord  # noqa - shader output

In other words, you have one uniform buffer object, which is a struct, that contains multiple matrices. These lines are key:

uniform_type = Struct(transform=mat4, projection=mat4, view=mat4, model=mat4)
uniform_data = np.asarray(shadertype_as_ctype(uniform_type)())

Where the uniform_type is something that python_shader understands, so it can generate the correct SpirV. And by transforming that to a numpy struct, you have a matching data struct. (There is a step via ctypes there because we do not want to depend on numpy in wgpu-py.)

Could you tell me how to complete the pipeline initialization ?

That bind group looks good. That 2 in "binding": 2, #? should match the 2 in uni: ("uniform", (0, 2), uniform_type),.

and update them:

In the code you showed, you upload the new data to a new buffer. Now you need to copy that buffer to the existing buffer.

            command_encoder = self._device.create_command_encoder()
            command_encoder.copy_buffer_to_buffer(tmp_buffer, 0, actual_buffer, boffset, bsize)
            self._device.default_queue.submit([command_encoder.finish()])

This may seem a bit odd, but it seems to be the preferred way. The alternative is to map the existing buffer for writing, but this is slower.

Note that we may change the API a bit, so that creation of the tmp bufer may soon look something like create_buffer_from_data(...). See #98

from wgpu-py.

shazz avatar shazz commented on June 14, 2024

Ah ah! Thanks, so just my shader declaration was wrong, got it. Works perfectly now!
So I have a first version of my pygame-wgpu integration, if you'd like to have a look, tell me!

I'll review the docs about the bind groups. But from a first pass, yeah, a good drawing will save thousands of words :)
Not that clear to me what is the benefit of grouping bindings (vs independent ones, best practice?) and what is the value brought by layouts. I need to think about it :)

from wgpu-py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.