From ba49a1e47a3c071cdc72bb548cae84c5f40ac7d7 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Fri, 13 Sep 2024 09:02:18 +0200 Subject: [PATCH] A triangle. --- private/sdl_gpu_test/main.cpp | 25 ++++++++++++++++++++++--- private/sdl_gpu_test/sdlpp/gpu.hpp | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/private/sdl_gpu_test/main.cpp b/private/sdl_gpu_test/main.cpp index ef4366d..185bfae 100644 --- a/private/sdl_gpu_test/main.cpp +++ b/private/sdl_gpu_test/main.cpp @@ -14,18 +14,27 @@ namespace const char* const VERTEX_SOURCE = R"( #version 460 +const vec2 vertices[3] = vec2[3]( + vec2(-0.5, 0.5), + vec2( 0.5, 0.5), + vec2( 0.0, -0.5) +); + void main() { - + gl_Position = vec4(vertices[gl_VertexIndex], 0.0, 1.0); } )"; const char* const FRAGMENT_SOURCE = R"( #version 460 +layout(location = 0) +out vec4 o_color; + void main() { - + o_color = vec4(0.0, 1.0, 0.0, 1.0); } )"; } @@ -82,10 +91,18 @@ int main(int, char**) }); // create graphics pipeline + std::array colorTargetsDescs = { + sdlpp::GPUColorTargetDescription{ + .format = gpuDevice.getSwapchainTextureFormat(window) + } + }; sdlpp::GPUGraphicsPipeline pipeline; pipeline.create(gpuDevice, { .vertexShader = vertexShader, - .fragmentShader = fragmentShader + .fragmentShader = fragmentShader, + .targetInfo = { + .colorTargetDescriptions = colorTargetsDescs + } }); bool running = true; @@ -111,6 +128,8 @@ int main(int, char**) sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({ .colorTargetInfos = colorTargets }); + renderPass.bindGraphicsPipeline(pipeline); + renderPass.drawPrimitives({.numVertices = 3}); renderPass.end(); cmdBuffer.submit(); diff --git a/private/sdl_gpu_test/sdlpp/gpu.hpp b/private/sdl_gpu_test/sdlpp/gpu.hpp index 09aa304..ae92438 100644 --- a/private/sdl_gpu_test/sdlpp/gpu.hpp +++ b/private/sdl_gpu_test/sdlpp/gpu.hpp @@ -400,6 +400,14 @@ static_assert(sizeof(GPUDepthStencilTargetInfo) == sizeof(SDL_GPUDepthStencilTar // classes // +struct GPUDrawPrimitivesArgs +{ + Uint32 numVertices = 0; + Uint32 numInstances = 1; + Uint32 firstVertex = 0; + Uint32 firstInstance = 0; +}; + class GPURenderPass : public Base { public: @@ -426,6 +434,16 @@ public: MIJIN_ASSERT(mHandle == nullptr, "Renderpass has not been ended."); } + void bindGraphicsPipeline(SDL_GPUGraphicsPipeline* pipeline) const noexcept + { + SDL_BindGPUGraphicsPipeline(mHandle, pipeline); + } + + void drawPrimitives(const GPUDrawPrimitivesArgs& args) const noexcept + { + SDL_DrawGPUPrimitives(mHandle, args.numVertices, args.numInstances, args.firstVertex, args.firstInstance); + } + friend class GPUCommandBuffer; }; @@ -568,6 +586,12 @@ public: } } + [[nodiscard]] + GPUTextureFormat getSwapchainTextureFormat(SDL_Window* window) const + { + return static_cast(SDL_GetGPUSwapchainTextureFormat(mHandle, window)); + } + [[nodiscard]] GPUCommandBuffer acquireCommandBuffer() const noexcept {