#include "VertexInputStateBuilder.h" #include #include VertexInputStateBuilder& VertexInputStateBuilder::AddAttribute(Uint32 location, SDL_GPUVertexElementFormat type) { Uint32 offset = 0; for (auto e : elements) { const Uint32 floatSize = sizeof(float); switch (e.format) { case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT: offset += floatSize; break; case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2: offset += floatSize * 2; break; case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3: offset += floatSize * 3; break; case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4: offset += floatSize * 4; break; default: throw std::runtime_error("Unknown vertex element format"); break; } } SDL_GPUVertexAttribute element = {}; element.format = type; element.location = location; element.offset = offset; element.buffer_slot = 0; elements.push_back(element); return *this; } VertexInputStateBuilder& VertexInputStateBuilder::Instanced() { isInstanced = true; return *this; } VertexInputStateBuilder& VertexInputStateBuilder::BuildDeferred() { Build(); return *this; } SDL_GPUVertexInputState VertexInputStateBuilder::Build() { assert(elements.size()); Uint32 pitch = 0; for (auto e : elements) { //pitch += e.format == SDL_GPU_VERTEXELEMENTFORMAT_FLOAT ? 4 : 16; const Uint32 floatSize = sizeof(float); switch (e.format) { case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT: pitch += floatSize; break; case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2: pitch += floatSize * 2; break; case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3: pitch += floatSize * 3; break; case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4: pitch += floatSize * 4; break; default: throw std::runtime_error("Unknown vertex element format"); break; } } description = {}; description.slot = 0; description.pitch = pitch; description.input_rate = isInstanced ? SDL_GPU_VERTEXINPUTRATE_INSTANCE : SDL_GPU_VERTEXINPUTRATE_VERTEX; description.instance_step_rate = 0; state = {}; state.vertex_buffer_descriptions = &description; state.num_vertex_buffers = 1; state.vertex_attributes = elements.data(); state.num_vertex_attributes = elements.size(); return state; // description is on stack and therefore this shit doesn't work becuase it gets overwritten later ughhhhhhh } SDL_GPUVertexInputState VertexInputStateBuilder::Use() { return state; } VertexInputStateBuilder::VertexInputStateBuilder() { } VertexInputStateBuilder::VertexInputStateBuilder(const VertexInputStateBuilder& other) : elements(other.elements), description(other.description), state(other.state) { // Fix up the pointer — description was copied to a new address state.vertex_buffer_descriptions = &description; state.vertex_attributes = elements.data(); } VertexInputStateBuilder::VertexInputStateBuilder(VertexInputStateBuilder&& other) noexcept : elements(std::move(other.elements)), description(other.description), state(other.state) { state.vertex_buffer_descriptions = &description; state.vertex_attributes = elements.data(); }