Skip to content

Commit

Permalink
Skeleton for bindless texturing sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jun 30, 2024
1 parent 0cf64fe commit 3bd715e
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 1 deletion.
251 changes: 251 additions & 0 deletions 09-BindlessTexturing/bindlesstexturing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
#include "Common/ExampleFramework.hpp"
#include <RGL/Buffer.hpp>
#include <RGL/Texture.hpp>
#include <RGL/Sampler.hpp>
#include <RGL/Pipeline.hpp>
#include <RGL/RenderPass.hpp>
#include <RGL/CommandBuffer.hpp>


struct BindlessTexturing : public ExampleFramework {

struct {
glm::mat4 viewProj;
} ubo;

struct {
glm::vec4 color;
glm::ivec2 dim;
} comp_ubo;

RGLCommandBufferPtr commandBuffer;
RGLBufferPtr vertexBuffer, indexBuffer;
RGLTexturePtr depthTexture;
RGLSamplerPtr sampler;
RGLRenderPipelinePtr renderPipeline;
RGLRenderPassPtr renderPass;

constexpr static auto num_mips = 4;
constexpr static auto img_size = 1024;
constexpr static glm::vec4 colors[] = {
{195 / 255.f,88 / 255.f,49 / 255.f, 1},
{96 / 255.f,111 / 255.f,140 / 255.f, 1},
{88 / 255.f,114 / 255.f,70 / 255.f, 1},
{34 / 255.f,230 / 255.f,202 / 255.f, 1},
};

void sampleinit(int argc, char** argv) final {
vertexBuffer = device->CreateBuffer({
{.VertexBuffer = true},
sizeof(BasicObjects::Quad::Vertex),
BasicObjects::Quad::vertices,
RGL::BufferAccess::Private
});
vertexBuffer->SetBufferData(BasicObjects::Quad::vertices);

indexBuffer = device->CreateBuffer({
{.IndexBuffer = true},
sizeof(BasicObjects::Quad::indices[0]),
BasicObjects::Quad::indices,
RGL::BufferAccess::Private
});
indexBuffer->SetBufferData(BasicObjects::Quad::indices);

createDepthTexture();


// set sampler to trilinear filtering
sampler = device->CreateSampler({ });

auto renderPipelineLayout = device->CreatePipelineLayout({
.bindings = {
{
.binding = 0,
.type = RGL::BindingType::Sampler,
.stageFlags = RGL::BindingVisibility::Fragment,
},
{
.binding = 1,
.type = RGL::BindingType::SampledImage,
.stageFlags = RGL::BindingVisibility::Fragment,
},
},
.constants = {{ ubo, 0, RGL::StageVisibility::Vertex}}
});

auto vertexShaderLibrary = GetShader("btex.vert");
auto fragmentShaderLibrary = GetShader("btex.frag");

renderPipeline = device->CreateRenderPipeline({
.stages = {
{
.type = RGL::ShaderStageDesc::Type::Vertex,
.shaderModule = vertexShaderLibrary,
},
{
.type = RGL::ShaderStageDesc::Type::Fragment,
.shaderModule = fragmentShaderLibrary,
}
},
.vertexConfig = {
.vertexBindings = {{
.binding = 0,
.stride = sizeof(BasicObjects::Quad::Vertex),
}},
.attributeDescs = {
{
.location = 0,
.binding = 0,
.offset = offsetof(BasicObjects::Quad::Vertex,position),
.format = RGL::VertexAttributeFormat::R32G32_SignedFloat,
},
{
.location = 1,
.binding = 0,
.offset = offsetof(BasicObjects::Quad::Vertex,uv),
.format = RGL::VertexAttributeFormat::R32G32_SignedFloat,
}
}
},
.inputAssembly = {
.topology = RGL::PrimitiveTopology::TriangleList,
},
.viewport = {
.width = (float)width,
.height = (float)height
},
.scissor = {
.extent = {width, height}
},
.rasterizerConfig = {
.windingOrder = RGL::WindingOrder::Counterclockwise,
},
.colorBlendConfig{
.attachments = {
{
.format = RGL::TextureFormat::BGRA8_Unorm // specify attachment data
}
}
},
.depthStencilConfig = {
.depthFormat = RGL::TextureFormat::D32SFloat,
.depthTestEnabled = true,
.depthWriteEnabled = true,
.depthFunction = RGL::DepthCompareFunction::Less,
},
.pipelineLayout = renderPipelineLayout,
});

renderPass = RGL::CreateRenderPass({
.attachments = {
{
.format = RGL::TextureFormat::BGRA8_Unorm,
.loadOp = RGL::LoadAccessOperation::Clear,
.storeOp = RGL::StoreAccessOperation::Store,
.clearColor = { 0.4f, 0.6f, 0.9f, 1.0f},
}
},
.depthAttachment = RGL::RenderPassConfig::AttachmentDesc{
.format = RGL::TextureFormat::D32SFloat,
.loadOp = RGL::LoadAccessOperation::Clear,
.storeOp = RGL::StoreAccessOperation::Store,
.clearColor = {1,1,1,1}
}
});

auto computePipelineLayout = device->CreatePipelineLayout({
.bindings = {
{
.binding = 0,
.type = RGL::BindingType::StorageImage,
.stageFlags = RGL::BindingVisibility::Compute,
.writable = true
},
},
.constants = {
{{ comp_ubo, 0, RGL::StageVisibility::Compute}}
}
});

commandBuffer = commandQueue->CreateCommandBuffer();
camera.position.z = 10;
camera.position.y = 0.3;
}

void tick() final {
ubo.viewProj = camera.GenerateViewProjMatrix(width, height);

RGL::SwapchainPresentConfig presentConfig{
};

swapchain->GetNextImage(&presentConfig.imageIndex);
swapchainFence->Wait();
swapchainFence->Reset();
commandBuffer->Reset();
commandBuffer->Begin();
auto nextimg = swapchain->ImageAtIndex(presentConfig.imageIndex);
auto nextImgSize = nextimg->GetSize();

renderPass->SetAttachmentTexture(0, nextimg->GetDefaultView());
renderPass->SetDepthAttachmentTexture(depthTexture->GetDefaultView());

commandBuffer->BeginRendering(renderPass);
commandBuffer->SetViewport({
.width = static_cast<float>(nextImgSize.width),
.height = static_cast<float>(nextImgSize.height),
});
commandBuffer->SetScissor({
.extent = {nextImgSize.width, nextImgSize.height}
});

commandBuffer->BindRenderPipeline(renderPipeline);
commandBuffer->SetVertexBytes(ubo, 0);
commandBuffer->SetVertexBuffer(vertexBuffer);
commandBuffer->SetIndexBuffer(indexBuffer);
commandBuffer->DrawIndexed(std::size(BasicObjects::Quad::indices));

commandBuffer->EndRendering();
commandBuffer->End();

RGL::CommitConfig commitconfig{
.signalFence = swapchainFence,
};
commandBuffer->Commit(commitconfig);
commandBuffer->BlockUntilCompleted();

swapchain->Present(presentConfig);
}

void sampleshutdown() final {
renderPass.reset();
depthTexture.reset();
indexBuffer.reset();
vertexBuffer.reset();
renderPipeline.reset();
commandBuffer.reset();
}

void createDepthTexture()
{
// create the depth buffer
depthTexture = device->CreateTexture({
.usage = {.DepthStencilAttachment = true},
.aspect = {.HasDepth = true},
.width = (uint32_t)width,
.height = (uint32_t)height,
.format = RGL::TextureFormat::D32SFloat,
.debugName = "Depth Texture"
}
);
}

const char* SampleName() final {
return "Mipmap";
}

void onresize(int width, int height) final {
createDepthTexture();
}
};

START_SAMPLE(BindlessTexturing);
8 changes: 8 additions & 0 deletions 09-BindlessTexturing/btex.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


layout(location = 0) in vec2 uv;
layout(location = 0) out vec4 outcolor;
void main(){

outcolor = vec4(1,1,0,1);
}
14 changes: 14 additions & 0 deletions 09-BindlessTexturing/btex.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

layout(push_constant) uniform UniformBufferObject{
mat4 viewProj;
} ubo;

layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec2 inUV;

layout(location = 0) out vec2 outUV;

void main(){
gl_Position = ubo.viewProj * vec4(inPosition, 0, 1);
outUV = inUV;
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ endif()
add_sample(06-Asteroids)
add_sample(07-Mipmap)
add_sample(08-Cubemap)
add_sample(09-BindlessTexturing)

target_link_libraries(06-Asteroids
PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion deps/RGL

0 comments on commit 3bd715e

Please sign in to comment.