Skip to content

Commit

Permalink
Fix the indices by reading from a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jul 4, 2024
1 parent 9506c1b commit e3b6601
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
29 changes: 26 additions & 3 deletions 09-BindlessTexturing/bindlesstexturing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}}
});
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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();
Expand All @@ -240,6 +262,7 @@ struct BindlessTexturing : public ExampleFramework {
}

void sampleshutdown() final {
instanceDataBuffer.reset();
tx1.reset();
tx2.reset();
tx3.reset();
Expand Down
2 changes: 1 addition & 1 deletion 09-BindlessTexturing/btex.frag
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 6 additions & 2 deletions 09-BindlessTexturing/btex.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
Expand Down

0 comments on commit e3b6601

Please sign in to comment.