From b552a098855654e34640f3cbdce3c1f13fb0cf4a Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sun, 15 Sep 2024 15:34:05 +0200 Subject: [PATCH] Include older iterations as separate applications. --- .../sdl_gpu_test/0_clear_swapchain/app.cpp | 24 +++++ .../sdl_gpu_test/0_clear_swapchain/app.hpp | 18 ++++ private/sdl_gpu_test/1_green_triangle/app.cpp | 91 +++++++++++++++++++ private/sdl_gpu_test/1_green_triangle/app.hpp | 21 +++++ .../app.cpp | 0 .../app.hpp | 6 +- .../app.cpp | 0 .../app.hpp | 6 +- .../app.cpp | 0 .../app.hpp | 6 +- private/sdl_gpu_test/SModule | 8 +- private/sdl_gpu_test/main.cpp | 10 +- 12 files changed, 175 insertions(+), 15 deletions(-) create mode 100644 private/sdl_gpu_test/0_clear_swapchain/app.cpp create mode 100644 private/sdl_gpu_test/0_clear_swapchain/app.hpp create mode 100644 private/sdl_gpu_test/1_green_triangle/app.cpp create mode 100644 private/sdl_gpu_test/1_green_triangle/app.hpp rename private/sdl_gpu_test/{0_triangle_with_texcoords => 2_triangle_with_texcoords}/app.cpp (100%) rename private/sdl_gpu_test/{0_triangle_with_texcoords => 2_triangle_with_texcoords}/app.hpp (69%) rename private/sdl_gpu_test/{1_textured_quad => 3_textured_quad}/app.cpp (100%) rename private/sdl_gpu_test/{1_textured_quad => 3_textured_quad}/app.hpp (70%) rename private/sdl_gpu_test/{2_textured_cube => 4_textured_cube}/app.cpp (100%) rename private/sdl_gpu_test/{2_textured_cube => 4_textured_cube}/app.hpp (75%) diff --git a/private/sdl_gpu_test/0_clear_swapchain/app.cpp b/private/sdl_gpu_test/0_clear_swapchain/app.cpp new file mode 100644 index 0000000..6c996f8 --- /dev/null +++ b/private/sdl_gpu_test/0_clear_swapchain/app.cpp @@ -0,0 +1,24 @@ + +#include "./app.hpp" + +namespace sdl_gpu_test +{ +void ClearSwapchainApp::update(const AppUpdateArgs& args) +{ + Application::update(args); + + sdlpp::GPUCommandBuffer cmdBuffer = mDevice.acquireCommandBuffer(); + Uint32 swapchainWidth = 0, swapchainHeight = 0; + sdlpp::GPUTexture swapchainTexture = cmdBuffer.acquireSwapchainTexture(mWindow, swapchainWidth, swapchainHeight); + std::array colorTargets = {sdlpp::GPUColorTargetInfo{ + .texture = swapchainTexture, + .clearColor = {.r = 1.f, .g = 0.f, .b = 0.f, .a = 1.f}, + .loadOp = sdlpp::GPULoadOp::CLEAR, + }}; + sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({ + .colorTargetInfos = colorTargets + }); + renderPass.end(); + cmdBuffer.submit(); +} +} diff --git a/private/sdl_gpu_test/0_clear_swapchain/app.hpp b/private/sdl_gpu_test/0_clear_swapchain/app.hpp new file mode 100644 index 0000000..6e60238 --- /dev/null +++ b/private/sdl_gpu_test/0_clear_swapchain/app.hpp @@ -0,0 +1,18 @@ + +#pragma once + +#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_0_CLEAR_SWAPCHAIN_APP_HPP_INCLUDED) +#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_0_CLEAR_SWAPCHAIN_APP_HPP_INCLUDED 1 + +#include "../application.hpp" + +namespace sdl_gpu_test +{ +class ClearSwapchainApp : public Application +{ +public: + void update(const AppUpdateArgs& args) override; +}; +} // namespace sdl_gpu_test + +#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_0_CLEAR_SWAPCHAIN_APP_HPP_INCLUDED) diff --git a/private/sdl_gpu_test/1_green_triangle/app.cpp b/private/sdl_gpu_test/1_green_triangle/app.cpp new file mode 100644 index 0000000..612215e --- /dev/null +++ b/private/sdl_gpu_test/1_green_triangle/app.cpp @@ -0,0 +1,91 @@ + +#include "./app.hpp" + +namespace sdl_gpu_test +{ +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); +} +)"; +} + +void GreenTriangleApp::init(const AppInitArgs& args) +{ + Application::init(args); + + // create vertex shader + mijin::TypelessBuffer vertexSpv = getFileContentsBinary("shaders/glsl/triangle.vert.spv"); + sdlpp::GPUShader vertexShader; + vertexShader.create(mDevice, { + .code = {static_cast(vertexSpv.data()), vertexSpv.byteSize()}, + .format = sdlpp::GPUShaderFormat::SPIRV, + .stage = sdlpp::GPUShaderStage::VERTEX + }); + + // create fragment shader + mijin::TypelessBuffer fragmentSpv = getFileContentsBinary("shaders/glsl/green.frag.spv"); + sdlpp::GPUShader fragmentShader; + fragmentShader.create(mDevice, { + .code = {static_cast(fragmentSpv.data()), fragmentSpv.byteSize()}, + .format = sdlpp::GPUShaderFormat::SPIRV, + .stage = sdlpp::GPUShaderStage::FRAGMENT + }); + + // create graphics pipeline + std::array colorTargetsDescs = { + sdlpp::GPUColorTargetDescription{ + .format = mDevice.getSwapchainTextureFormat(mWindow) + } + }; + mPipeline.create(mDevice, { + .vertexShader = vertexShader, + .fragmentShader = fragmentShader, + .targetInfo = { + .colorTargetDescriptions = colorTargetsDescs + } + }); +} + +void GreenTriangleApp::update(const AppUpdateArgs& args) +{ + Application::update(args); + + sdlpp::GPUCommandBuffer cmdBuffer = mDevice.acquireCommandBuffer(); + Uint32 swapchainWidth = 0, swapchainHeight = 0; + sdlpp::GPUTexture swapchainTexture = cmdBuffer.acquireSwapchainTexture(mWindow, swapchainWidth, swapchainHeight); + std::array colorTargets = {sdlpp::GPUColorTargetInfo{ + .texture = swapchainTexture, + .clearColor = {.r = 1.f, .g = 0.f, .b = 0.f, .a = 1.f}, + .loadOp = sdlpp::GPULoadOp::CLEAR, + }}; + sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({ + .colorTargetInfos = colorTargets + }); + renderPass.bindGraphicsPipeline(mPipeline); + renderPass.drawPrimitives({.numVertices = 3}); + renderPass.end(); + cmdBuffer.submit(); +} +} diff --git a/private/sdl_gpu_test/1_green_triangle/app.hpp b/private/sdl_gpu_test/1_green_triangle/app.hpp new file mode 100644 index 0000000..cb198c9 --- /dev/null +++ b/private/sdl_gpu_test/1_green_triangle/app.hpp @@ -0,0 +1,21 @@ + +#pragma once + +#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_1_GREEN_TRIANGLE_APP_HPP_INCLUDED) +#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_1_GREEN_TRIANGLE_APP_HPP_INCLUDED 1 + +#include "../application.hpp" + +namespace sdl_gpu_test +{ +class GreenTriangleApp : public Application +{ +private: + sdlpp::GPUGraphicsPipeline mPipeline; +public: + void init(const AppInitArgs& args) override; + void update(const AppUpdateArgs& args) override; +}; +} // namespace sdl_gpu_test + +#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_1_GREEN_TRIANGLE_APP_HPP_INCLUDED) diff --git a/private/sdl_gpu_test/0_triangle_with_texcoords/app.cpp b/private/sdl_gpu_test/2_triangle_with_texcoords/app.cpp similarity index 100% rename from private/sdl_gpu_test/0_triangle_with_texcoords/app.cpp rename to private/sdl_gpu_test/2_triangle_with_texcoords/app.cpp diff --git a/private/sdl_gpu_test/0_triangle_with_texcoords/app.hpp b/private/sdl_gpu_test/2_triangle_with_texcoords/app.hpp similarity index 69% rename from private/sdl_gpu_test/0_triangle_with_texcoords/app.hpp rename to private/sdl_gpu_test/2_triangle_with_texcoords/app.hpp index c952273..bf11bc3 100644 --- a/private/sdl_gpu_test/0_triangle_with_texcoords/app.hpp +++ b/private/sdl_gpu_test/2_triangle_with_texcoords/app.hpp @@ -1,8 +1,8 @@ #pragma once -#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_0_TRIANGLE_WITH_TEXCOORDS_APP_HPP_INCLUDED) -#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_0_TRIANGLE_WITH_TEXCOORDS_APP_HPP_INCLUDED 1 +#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_2_TRIANGLE_WITH_TEXCOORDS_APP_HPP_INCLUDED) +#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_2_TRIANGLE_WITH_TEXCOORDS_APP_HPP_INCLUDED 1 #include "../application.hpp" @@ -19,4 +19,4 @@ public: }; } // namespace sdl_gpu_test -#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_0_TRIANGLE_WITH_TEXCOORDS_APP_HPP_INCLUDED) +#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_2_TRIANGLE_WITH_TEXCOORDS_APP_HPP_INCLUDED) diff --git a/private/sdl_gpu_test/1_textured_quad/app.cpp b/private/sdl_gpu_test/3_textured_quad/app.cpp similarity index 100% rename from private/sdl_gpu_test/1_textured_quad/app.cpp rename to private/sdl_gpu_test/3_textured_quad/app.cpp diff --git a/private/sdl_gpu_test/1_textured_quad/app.hpp b/private/sdl_gpu_test/3_textured_quad/app.hpp similarity index 70% rename from private/sdl_gpu_test/1_textured_quad/app.hpp rename to private/sdl_gpu_test/3_textured_quad/app.hpp index 3debb8c..538f375 100644 --- a/private/sdl_gpu_test/1_textured_quad/app.hpp +++ b/private/sdl_gpu_test/3_textured_quad/app.hpp @@ -1,8 +1,8 @@ #pragma once -#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_1_TEXTURED_QUAD_APP_HPP_INCLUDED) -#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_1_TEXTURED_QUAD_APP_HPP_INCLUDED 1 +#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_3_TEXTURED_QUAD_APP_HPP_INCLUDED) +#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_3_TEXTURED_QUAD_APP_HPP_INCLUDED 1 #include "../application.hpp" @@ -21,4 +21,4 @@ public: }; } // namespace sdl_gpu_test -#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_1_TEXTURED_QUAD_APP_HPP_INCLUDED) +#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_3_TEXTURED_QUAD_APP_HPP_INCLUDED) diff --git a/private/sdl_gpu_test/2_textured_cube/app.cpp b/private/sdl_gpu_test/4_textured_cube/app.cpp similarity index 100% rename from private/sdl_gpu_test/2_textured_cube/app.cpp rename to private/sdl_gpu_test/4_textured_cube/app.cpp diff --git a/private/sdl_gpu_test/2_textured_cube/app.hpp b/private/sdl_gpu_test/4_textured_cube/app.hpp similarity index 75% rename from private/sdl_gpu_test/2_textured_cube/app.hpp rename to private/sdl_gpu_test/4_textured_cube/app.hpp index 3ffb38e..e2c7964 100644 --- a/private/sdl_gpu_test/2_textured_cube/app.hpp +++ b/private/sdl_gpu_test/4_textured_cube/app.hpp @@ -1,8 +1,8 @@ #pragma once -#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_2_TEXTURED_CUBE_APP_HPP_INCLUDED) -#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_2_TEXTURED_CUBE_APP_HPP_INCLUDED 1 +#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_4_TEXTURED_CUBE_APP_HPP_INCLUDED) +#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_4_TEXTURED_CUBE_APP_HPP_INCLUDED 1 #include "../application.hpp" @@ -25,4 +25,4 @@ public: }; } // namespace sdl_gpu_test -#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_2_TEXTURED_CUBE_APP_HPP_INCLUDED) +#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_4_TEXTURED_CUBE_APP_HPP_INCLUDED) diff --git a/private/sdl_gpu_test/SModule b/private/sdl_gpu_test/SModule index b3a17d8..e69aa64 100644 --- a/private/sdl_gpu_test/SModule +++ b/private/sdl_gpu_test/SModule @@ -8,9 +8,11 @@ src_files = Split(""" util/bitmap.cpp util/mesh.cpp - 0_triangle_with_texcoords/app.cpp - 1_textured_quad/app.cpp - 2_textured_cube/app.cpp + 0_clear_swapchain/app.cpp + 1_green_triangle/app.cpp + 2_triangle_with_texcoords/app.cpp + 3_textured_quad/app.cpp + 4_textured_cube/app.cpp """) shader_files = env.Glob("#assets/shaders/glsl/*.frag") \ diff --git a/private/sdl_gpu_test/main.cpp b/private/sdl_gpu_test/main.cpp index 89cd426..7222380 100644 --- a/private/sdl_gpu_test/main.cpp +++ b/private/sdl_gpu_test/main.cpp @@ -1,7 +1,9 @@ -#include "./0_triangle_with_texcoords/app.hpp" -#include "./1_textured_quad/app.hpp" -#include "./2_textured_cube/app.hpp" +#include "./0_clear_swapchain/app.hpp" +#include "./1_green_triangle/app.hpp" +#include "./2_triangle_with_texcoords/app.hpp" +#include "./3_textured_quad/app.hpp" +#include "./4_textured_cube/app.hpp" #include #include @@ -23,6 +25,8 @@ int main(int argc, char* argv[]) try { // make sure app is destructed before shutting down SDL + // std::unique_ptr app = std::make_unique(); + // std::unique_ptr app = std::make_unique(); // std::unique_ptr app = std::make_unique(); // std::unique_ptr app = std::make_unique(); std::unique_ptr app = std::make_unique();