Texcoords

This commit is contained in:
Patrick 2024-09-14 00:26:19 +02:00
parent 660a6cedea
commit 65dd335859
5 changed files with 92 additions and 35 deletions

View File

@ -11,3 +11,9 @@ Descriptor sets
- 0 are read-only resources (samplers, storage images, storage buffers) - 0 are read-only resources (samplers, storage images, storage buffers)
- 1 are write-only resources (storage images and storage buffers) - 1 are write-only resources (storage images and storage buffers)
- 2 are uniform buffers - 2 are uniform buffers
Cycling
===
- due to the nature of in-flight frames, resources might still be in use in a previous frames
- to make up for this, many SDL_gpu APIs have a `cycle` parameter
- if set to true, SDL will provide an unused resource if the previous one is still in use

View File

@ -0,0 +1,18 @@
#version 460
layout(set = 3, binding = 0)
uniform Parameters
{
vec4 u_color;
};
layout(location = 0)
in vec2 i_texCoord;
layout(location = 0)
out vec4 o_color;
void main()
{
o_color = vec4(i_texCoord, 1, 1) * u_color;
}

View File

@ -0,0 +1,16 @@
#version 460
layout(location = 0)
in vec2 i_position;
layout(location = 1)
in vec2 i_texCoord;
layout(location = 0)
out vec2 o_texCoord;
void main()
{
gl_Position = vec4(i_position, 0.0, 1.0);
o_texCoord = i_texCoord;
}

View File

@ -7,9 +7,11 @@ src_files = Split("""
shader_files = Split(""" shader_files = Split("""
#assets/shaders/glsl/color_from_uniform.frag #assets/shaders/glsl/color_from_uniform.frag
#assets/shaders/glsl/color_from_texcoord.frag
#assets/shaders/glsl/green.frag #assets/shaders/glsl/green.frag
#assets/shaders/glsl/triangle.vert #assets/shaders/glsl/triangle.vert
#assets/shaders/glsl/triangles_from_buffer.vert #assets/shaders/glsl/triangles_from_buffer.vert
#assets/shaders/glsl/textured_triangles_from_buffer.vert
""") """)
env.Append(CPPDEFINES = ['GLM_FORCE_DEPTH_ZERO_TO_ONE', 'GLM_FORCE_RADIANS']) env.Append(CPPDEFINES = ['GLM_FORCE_DEPTH_ZERO_TO_ONE', 'GLM_FORCE_RADIANS'])

View File

@ -15,6 +15,12 @@
namespace namespace
{ {
struct Vertex
{
glm::vec2 pos;
glm::vec2 texCoord;
};
mijin::StackedFileSystemAdapter gFileSystem; mijin::StackedFileSystemAdapter gFileSystem;
void initFileSystem(const fs::path& executablePath) noexcept void initFileSystem(const fs::path& executablePath) noexcept
@ -43,6 +49,33 @@ mijin::TypelessBuffer getFileContentsBinary(const fs::path& path)
mijin::throwOnError(stream->readRest(content), "Error reading file contents."); mijin::throwOnError(stream->readRest(content), "Error reading file contents.");
return content; return content;
} }
template<typename TVertex>
void uploadVertexData(const sdlpp::GPUDevice& gpuDevice, const sdlpp::GPUBuffer& vertexBuffer, std::span<TVertex> vertices)
{
sdlpp::GPUTransferBuffer transferBuffer;
transferBuffer.create(gpuDevice, {
.usage = sdlpp::GPUTransferBufferUsage::UPLOAD,
.size = static_cast<Uint32>(vertices.size_bytes())
});
void* ptr = transferBuffer.map();
std::memcpy(ptr, vertices.data(), vertices.size_bytes());
transferBuffer.unmap();
sdlpp::GPUCommandBuffer cmdBuffer = gpuDevice.acquireCommandBuffer();
sdlpp::GPUCopyPass copyPass = cmdBuffer.beginCopyPass();
copyPass.uploadToGPUBuffer(
/* source = */ {
.transferBuffer = transferBuffer
},
/* destination = */ {
.buffer = vertexBuffer,
.size = static_cast<Uint32>(vertices.size_bytes())
}
);
copyPass.end();
cmdBuffer.submit();
}
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@ -67,7 +100,7 @@ int main(int argc, char* argv[])
sdlpp::Window window; sdlpp::Window window;
window.create({ window.create({
.title = "SDL_gpu Test", .title = "SDL_gpu Test",
.flags = {.vulkan = true} .flags = {.resizable = true}
}); });
sdlpp::GPUDevice gpuDevice; sdlpp::GPUDevice gpuDevice;
@ -79,7 +112,7 @@ int main(int argc, char* argv[])
gpuDevice.claimWindow(window); gpuDevice.claimWindow(window);
// create vertex shader // create vertex shader
mijin::TypelessBuffer vertexSpv = getFileContentsBinary("shaders/glsl/triangles_from_buffer.vert.spv"); mijin::TypelessBuffer vertexSpv = getFileContentsBinary("shaders/glsl/textured_triangles_from_buffer.vert.spv");
sdlpp::GPUShader vertexShader; sdlpp::GPUShader vertexShader;
vertexShader.create(gpuDevice, { vertexShader.create(gpuDevice, {
.code = {static_cast<const Uint8*>(vertexSpv.data()), vertexSpv.byteSize()}, .code = {static_cast<const Uint8*>(vertexSpv.data()), vertexSpv.byteSize()},
@ -88,7 +121,7 @@ int main(int argc, char* argv[])
}); });
// create fragment shader // create fragment shader
mijin::TypelessBuffer fragmentSpv = getFileContentsBinary("shaders/glsl/color_from_uniform.frag.spv"); mijin::TypelessBuffer fragmentSpv = getFileContentsBinary("shaders/glsl/color_from_texcoord.frag.spv");
sdlpp::GPUShader fragmentShader; sdlpp::GPUShader fragmentShader;
fragmentShader.create(gpuDevice, { fragmentShader.create(gpuDevice, {
.code = {static_cast<const Uint8*>(fragmentSpv.data()), fragmentSpv.byteSize()}, .code = {static_cast<const Uint8*>(fragmentSpv.data()), fragmentSpv.byteSize()},
@ -107,7 +140,7 @@ int main(int argc, char* argv[])
std::array vertexBindings = { std::array vertexBindings = {
sdlpp::GPUVertexBinding{ sdlpp::GPUVertexBinding{
.index = 0, .index = 0,
.pitch = sizeof(glm::vec2) .pitch = sizeof(Vertex)
} }
}; };
std::array vertexAttributes = { std::array vertexAttributes = {
@ -115,7 +148,13 @@ int main(int argc, char* argv[])
.location = 0, .location = 0,
.bindingIndex = 0, .bindingIndex = 0,
.format = sdlpp::GPUVertexElementFormat::FLOAT2, .format = sdlpp::GPUVertexElementFormat::FLOAT2,
.offset = 0 .offset = offsetof(Vertex, pos)
},
sdlpp::GPUVertexAttribute{
.location = 1,
.bindingIndex = 0,
.format = sdlpp::GPUVertexElementFormat::FLOAT2,
.offset = offsetof(Vertex, texCoord)
} }
}; };
pipeline.create(gpuDevice, { pipeline.create(gpuDevice, {
@ -132,9 +171,9 @@ int main(int argc, char* argv[])
std::array vertices = std::array vertices =
{ {
glm::vec2{-1.f, -1.f}, Vertex{.pos = {-1.f, -1.f}, .texCoord = {0.f, 0.f}},
glm::vec2{ 1.f, -1.f}, Vertex{.pos = { 1.f, -1.f}, .texCoord = {1.f, 0.f}},
glm::vec2{ 0.f, 1.f} Vertex{.pos = { 0.f, 1.f}, .texCoord = {0.5f, 1.f}}
}; };
// create vertex buffer // create vertex buffer
@ -143,31 +182,7 @@ int main(int argc, char* argv[])
.usage = {.vertex = true}, .usage = {.vertex = true},
.size = sizeof(vertices) .size = sizeof(vertices)
}); });
{ uploadVertexData(gpuDevice, vertexBuffer, std::span(vertices.begin(), vertices.end()));
sdlpp::GPUTransferBuffer transferBuffer;
transferBuffer.create(gpuDevice, {
.usage = sdlpp::GPUTransferBufferUsage::UPLOAD,
.size = sizeof(vertices)
});
void* ptr = transferBuffer.map();
std::memcpy(ptr, vertices.data(), sizeof(vertices));
transferBuffer.unmap();
sdlpp::GPUCommandBuffer cmdBuffer = gpuDevice.acquireCommandBuffer();
sdlpp::GPUCopyPass copyPass = cmdBuffer.beginCopyPass();
copyPass.uploadToGPUBuffer(
/* source = */ {
.transferBuffer = transferBuffer
},
/* destination = */ {
.buffer = vertexBuffer,
.size = sizeof(vertices)
}
);
copyPass.end();
cmdBuffer.submit();
}
bool running = true; bool running = true;
while(running) while(running)
@ -192,8 +207,8 @@ int main(int argc, char* argv[])
sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({ sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({
.colorTargetInfos = colorTargets .colorTargetInfos = colorTargets
}); });
static const glm::vec4 BLUE(0.f, 0.f, 1.f, 1.f); static const glm::vec4 WHITE(1.f, 1.f, 1.f, 1.f);
cmdBuffer.pushFragmentUniformData(0, std::span(&BLUE, 1)); cmdBuffer.pushFragmentUniformData(0, std::span(&WHITE, 1));
renderPass.bindGraphicsPipeline(pipeline); renderPass.bindGraphicsPipeline(pipeline);
renderPass.bindVertexBuffer({.buffer = vertexBuffer}); renderPass.bindVertexBuffer({.buffer = vertexBuffer});
renderPass.drawPrimitives({.numVertices = 3}); renderPass.drawPrimitives({.numVertices = 3});