A triangle.

This commit is contained in:
Patrick 2024-09-13 09:02:18 +02:00
parent 3a42f7cc36
commit ba49a1e47a
2 changed files with 46 additions and 3 deletions

View File

@ -14,18 +14,27 @@ namespace
const char* const VERTEX_SOURCE = R"( const char* const VERTEX_SOURCE = R"(
#version 460 #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() void main()
{ {
gl_Position = vec4(vertices[gl_VertexIndex], 0.0, 1.0);
} }
)"; )";
const char* const FRAGMENT_SOURCE = R"( const char* const FRAGMENT_SOURCE = R"(
#version 460 #version 460
layout(location = 0)
out vec4 o_color;
void main() void main()
{ {
o_color = vec4(0.0, 1.0, 0.0, 1.0);
} }
)"; )";
} }
@ -82,10 +91,18 @@ int main(int, char**)
}); });
// create graphics pipeline // create graphics pipeline
std::array colorTargetsDescs = {
sdlpp::GPUColorTargetDescription{
.format = gpuDevice.getSwapchainTextureFormat(window)
}
};
sdlpp::GPUGraphicsPipeline pipeline; sdlpp::GPUGraphicsPipeline pipeline;
pipeline.create(gpuDevice, { pipeline.create(gpuDevice, {
.vertexShader = vertexShader, .vertexShader = vertexShader,
.fragmentShader = fragmentShader .fragmentShader = fragmentShader,
.targetInfo = {
.colorTargetDescriptions = colorTargetsDescs
}
}); });
bool running = true; bool running = true;
@ -111,6 +128,8 @@ int main(int, char**)
sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({ sdlpp::GPURenderPass renderPass = cmdBuffer.beginRenderPass({
.colorTargetInfos = colorTargets .colorTargetInfos = colorTargets
}); });
renderPass.bindGraphicsPipeline(pipeline);
renderPass.drawPrimitives({.numVertices = 3});
renderPass.end(); renderPass.end();
cmdBuffer.submit(); cmdBuffer.submit();

View File

@ -400,6 +400,14 @@ static_assert(sizeof(GPUDepthStencilTargetInfo) == sizeof(SDL_GPUDepthStencilTar
// classes // classes
// //
struct GPUDrawPrimitivesArgs
{
Uint32 numVertices = 0;
Uint32 numInstances = 1;
Uint32 firstVertex = 0;
Uint32 firstInstance = 0;
};
class GPURenderPass : public Base<SDL_GPURenderPass, GPURenderPass> class GPURenderPass : public Base<SDL_GPURenderPass, GPURenderPass>
{ {
public: public:
@ -426,6 +434,16 @@ public:
MIJIN_ASSERT(mHandle == nullptr, "Renderpass has not been ended."); 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; friend class GPUCommandBuffer;
}; };
@ -568,6 +586,12 @@ public:
} }
} }
[[nodiscard]]
GPUTextureFormat getSwapchainTextureFormat(SDL_Window* window) const
{
return static_cast<GPUTextureFormat>(SDL_GetGPUSwapchainTextureFormat(mHandle, window));
}
[[nodiscard]] [[nodiscard]]
GPUCommandBuffer acquireCommandBuffer() const noexcept GPUCommandBuffer acquireCommandBuffer() const noexcept
{ {