From e3b660121cf51a1be2afbf31cb676a19eb714a2c Mon Sep 17 00:00:00 2001 From: Ravbug Date: Thu, 4 Jul 2024 14:45:33 -0700 Subject: [PATCH] Fix the indices by reading from a buffer --- 09-BindlessTexturing/bindlesstexturing.cpp | 29 +++++++++++++++++++--- 09-BindlessTexturing/btex.frag | 2 +- 09-BindlessTexturing/btex.vert | 8 ++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/09-BindlessTexturing/bindlesstexturing.cpp b/09-BindlessTexturing/bindlesstexturing.cpp index 04d63e1..1af79c2 100644 --- a/09-BindlessTexturing/bindlesstexturing.cpp +++ b/09-BindlessTexturing/bindlesstexturing.cpp @@ -19,7 +19,7 @@ struct BindlessTexturing : public ExampleFramework { } comp_ubo; RGLCommandBufferPtr commandBuffer; - RGLBufferPtr vertexBuffer, indexBuffer; + RGLBufferPtr vertexBuffer, indexBuffer, instanceDataBuffer; RGLTexturePtr depthTexture, tx1, tx2, tx3; RGLSamplerPtr sampler; RGLRenderPipelinePtr renderPipeline; @@ -64,12 +64,19 @@ struct BindlessTexturing : public ExampleFramework { .type = RGL::BindingType::Sampler, .stageFlags = RGL::BindingVisibility::Fragment, }, - { + { .binding = 1, .count = 512, + .type = RGL::BindingType::StorageBuffer, + .stageFlags = RGL::BindingVisibility::Vertex, + }, + { + .binding = 2, + .count = 512, .type = RGL::BindingType::SampledImage, .stageFlags = RGL::BindingVisibility::Fragment, }, + }, .constants = {{ ubo, 0, RGL::StageVisibility::Vertex}} }); @@ -188,6 +195,20 @@ struct BindlessTexturing : public ExampleFramework { loadTx("tx29.png",tx1); loadTx("tx34.png",tx2); loadTx("tx39.png",tx3); + + uint32_t indices[] = { + tx1->GetDefaultView().texture.dx.srvIDX, + tx2->GetDefaultView().texture.dx.srvIDX, + tx3->GetDefaultView().texture.dx.srvIDX + }; + + instanceDataBuffer = device->CreateBuffer({ + {.StorageBuffer = true}, + sizeof(decltype(indices[0])), + indices, + RGL::BufferAccess::Private + }); + instanceDataBuffer->SetBufferData(indices); } @@ -224,7 +245,8 @@ struct BindlessTexturing : public ExampleFramework { commandBuffer->SetVertexBuffer(vertexBuffer); commandBuffer->SetIndexBuffer(indexBuffer); commandBuffer->SetFragmentSampler(sampler,0); - commandBuffer->SetFragmentTexture(heapStart, 1); // expose all the textures + commandBuffer->SetFragmentTexture(heapStart, 2); // expose all the textures + commandBuffer->BindBuffer(instanceDataBuffer, 1); commandBuffer->DrawIndexed(std::size(BasicObjects::Quad::indices), {.nInstances = 3}); commandBuffer->EndRendering(); @@ -240,6 +262,7 @@ struct BindlessTexturing : public ExampleFramework { } void sampleshutdown() final { + instanceDataBuffer.reset(); tx1.reset(); tx2.reset(); tx3.reset(); diff --git a/09-BindlessTexturing/btex.frag b/09-BindlessTexturing/btex.frag index be6445d..8716d64 100644 --- a/09-BindlessTexturing/btex.frag +++ b/09-BindlessTexturing/btex.frag @@ -1,7 +1,7 @@ #extension GL_EXT_nonuniform_qualifier : enable layout(binding = 0) uniform sampler g_sampler; -layout(binding = 1) uniform texture2D textures[]; +layout(binding = 2) uniform texture2D textures[]; layout(location = 0) in vec2 uv; layout(location = 1) in flat uint instance; diff --git a/09-BindlessTexturing/btex.vert b/09-BindlessTexturing/btex.vert index d5791d9..35621b4 100644 --- a/09-BindlessTexturing/btex.vert +++ b/09-BindlessTexturing/btex.vert @@ -9,6 +9,10 @@ layout(location = 1) in vec2 inUV; layout(location = 0) out vec2 outUV; layout(location = 1) out flat uint instance; +layout (binding = 1) readonly buffer bindlessSSBO{ + uint indices[]; +}; + vec3 positions[] = { vec3(-3,0,0), vec3(0,0,0), @@ -17,9 +21,9 @@ vec3 positions[] = { void main(){ - instance = gl_InstanceID; + instance = indices[gl_InstanceID]; - vec3 pos = vec3(inPosition, 0) + positions[gl_InstanceID]; + vec3 pos = vec3(inPosition, 0) + positions[instance]; gl_Position = ubo.viewProj * vec4(pos, 1); outUV = inUV;