expipiplus1 / vulkan

Haskell bindings for Vulkan
https://hackage.haskell.org/package/vulkan
BSD 3-Clause "New" or "Revised" License
139 stars 31 forks source link

Howto write shader input uniform value #127

Open TristanCacqueray opened 4 years ago

TristanCacqueray commented 4 years ago

Would it be possible to add some documentations and/or a function to update an uniform value? If I understand correctly the code in resize/Main.hs, the Julia shader Frame uniform is updated using:

          allocaBytes constantBytes $ \p -> do
            liftIO $ poke (p `plusPtr` 0) frameScale
            liftIO $ poke (p `plusPtr` 8) frameOffset
            liftIO $ poke (p `plusPtr` 16) c
            liftIO $ poke (p `plusPtr` 24) escapeRadius
            cmdPushConstants' fJuliaPipelineLayout
                              SHADER_STAGE_COMPUTE_BIT
                              0
                              constantBytes
                              p

I'm looking for replacing opengl (example here), and I am not sure what is the vulkan equivalent of Graphics.GL.Core31.glUniform2f.

Thank you!

expipiplus1 commented 4 years ago

Yeah, that code is updating the push constant. I'd recommend looking up the appropriate section in any vulkan tutorial as the C commands will map 1-1 with the functions in this library.

I do think that some utilities to go from haskell records to the following would be an excellent fit for the utils library in this repo.

expipiplus1 commented 4 years ago

https://vulkan-tutorial.com/Uniform_buffers/Descriptor_layout_and_buffer, this page and the next one deal with using UBOs to pass data to shaders. Perhaps another valuable addition to this repo would be going through a page like that and making note of where the haskell code and c++ differ non-trivially. It should be a small diff

Also: if you use a UBO instead of push constants you should check out the VulkanMemoryAllocator library which makes allocating buffers a lot easier.

dpwiz commented 4 years ago

Updating UBOs is really just pokeing things at correct offsets/alignment. The big deal is setting up all the necessary preparations and wrapping your head over what exactly happens when.

You can inspect my playground for buffer code and descriptor setup, but I expect it to be overwhelming until you plow through the tutorial mentioned. Or you may cheat and try not become confused by reading my haskell rendition of it :sweat_smile:

dpwiz commented 4 years ago

linear instead of glm

Btw, linear matrices appear to be in wrong order (row vs column major) and have to be transposed. I made a library to deal with projections and preparing tons of mat4 as fast as possible.

TristanCacqueray commented 4 years ago

That is amazing, thank you for the details and tutorial links. That seems like valuable information for users of this library, perhaps it would be good enough to add those reference to the README, at least to help user like me who only know OpenGL.