Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw param order + non-float vertex input fixup #208

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MojoShader
77 changes: 45 additions & 32 deletions src/FNA3D_Driver_SDL.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ typedef struct GraphicsPipelineHash
FNA3D_PrimitiveType primitiveType;
SDL_GpuSampleCount sampleCount;
uint32_t sampleMask;
MOJOSHADER_sdlShaderData *vertShader;
MOJOSHADER_sdlShaderData *fragShader;
SDL_GpuShader *vertShader;
SDL_GpuShader *fragShader;
SDL_GpuTextureFormat colorFormats[MAX_RENDERTARGET_BINDINGS];
uint32_t colorFormatCount;
SDL_bool hasDepthStencilAttachment;
Expand Down Expand Up @@ -1222,6 +1222,7 @@ static void SDLGPU_INTERNAL_GenerateVertexInputInfo(
FNA3D_VertexDeclaration vertexDeclaration;
FNA3D_VertexElement element;
FNA3D_VertexElementUsage usage;
MOJOSHADER_sdlVertexAttribute mojoshaderVertexAttributes[16];
int32_t index, attribLoc;

MOJOSHADER_sdlGetBoundShaderData(renderer->mojoshaderContext, &vertexShader, &blah);
Expand Down Expand Up @@ -1278,6 +1279,10 @@ static void SDLGPU_INTERNAL_GenerateVertexInputInfo(
attributes[attributeDescriptionCounter].offset = element.offset;
attributes[attributeDescriptionCounter].binding = i;

mojoshaderVertexAttributes[attributeDescriptionCounter].usage = VertexAttribUsage(element.vertexElementUsage);
mojoshaderVertexAttributes[attributeDescriptionCounter].vertexElementFormat = element.vertexElementFormat;
mojoshaderVertexAttributes[attributeDescriptionCounter].usageIndex = index;

attributeDescriptionCounter += 1;
}

Expand All @@ -1299,6 +1304,12 @@ static void SDLGPU_INTERNAL_GenerateVertexInputInfo(
}

*attributeCount = attributeDescriptionCounter;

MOJOSHADER_sdlLinkProgram(
renderer->mojoshaderContext,
mojoshaderVertexAttributes,
attributeDescriptionCounter
);
}

static SDL_GpuGraphicsPipeline* SDLGPU_INTERNAL_FetchGraphicsPipeline(
Expand All @@ -1313,6 +1324,31 @@ static SDL_GpuGraphicsPipeline* SDLGPU_INTERNAL_FetchGraphicsPipeline(
SDL_GpuVertexAttribute *vertexAttributes;
int32_t i;

vertexBindings = SDL_malloc(
renderer->numVertexBindings *
sizeof(SDL_GpuVertexBinding)
);
vertexAttributes = SDL_malloc(
renderer->numVertexBindings *
MAX_VERTEX_ATTRIBUTES *
sizeof(SDL_GpuVertexAttribute)
);

/* We have to do this to link the vertex attribute modified shader program */
SDLGPU_INTERNAL_GenerateVertexInputInfo(
renderer,
vertexBindings,
vertexAttributes,
&createInfo.vertexInputState.vertexAttributeCount
);

/* Shaders */
MOJOSHADER_sdlGetShaders(
renderer->mojoshaderContext,
&createInfo.vertexShader,
&createInfo.fragmentShader
);

hash.blendState = GetPackedBlendState(renderer->fnaBlendState);

hash.depthStencilState = GetPackedDepthStencilState(
Expand All @@ -1324,8 +1360,8 @@ static SDL_GpuGraphicsPipeline* SDLGPU_INTERNAL_FetchGraphicsPipeline(
hash.sampleCount = renderer->nextRenderPassMultisampleCount;
hash.sampleMask = renderer->multisampleMask;
MOJOSHADER_sdlGetBoundShaderData(renderer->mojoshaderContext, &vertShader, &fragShader);
hash.vertShader = vertShader;
hash.fragShader = fragShader;
hash.vertShader = createInfo.vertexShader;
hash.fragShader = createInfo.fragmentShader;

hash.colorFormatCount = renderer->nextRenderPassColorAttachmentCount;
hash.colorFormats[0] = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
Expand Down Expand Up @@ -1358,30 +1394,15 @@ static SDL_GpuGraphicsPipeline* SDLGPU_INTERNAL_FetchGraphicsPipeline(

if (pipeline != NULL)
{
SDL_free(vertexBindings);
SDL_free(vertexAttributes);
return pipeline;
}

createInfo.primitiveType = XNAToSDL_PrimitiveType[renderer->fnaPrimitiveType];

/* Vertex Input State */

vertexBindings = SDL_malloc(
renderer->numVertexBindings *
sizeof(SDL_GpuVertexBinding)
);
vertexAttributes = SDL_malloc(
renderer->numVertexBindings *
MAX_VERTEX_ATTRIBUTES *
sizeof(SDL_GpuVertexAttribute)
);

SDLGPU_INTERNAL_GenerateVertexInputInfo(
renderer,
vertexBindings,
vertexAttributes,
&createInfo.vertexInputState.vertexAttributeCount
);

createInfo.vertexInputState.vertexBindings = vertexBindings;
createInfo.vertexInputState.vertexBindingCount = renderer->numVertexBindings;
createInfo.vertexInputState.vertexAttributes = vertexAttributes;
Expand Down Expand Up @@ -1521,14 +1542,6 @@ static SDL_GpuGraphicsPipeline* SDLGPU_INTERNAL_FetchGraphicsPipeline(
createInfo.depthStencilState.reference =
renderer->fnaDepthStencilState.referenceStencil;

/* Shaders */

MOJOSHADER_sdlGetShaders(
renderer->mojoshaderContext,
&createInfo.vertexShader,
&createInfo.fragmentShader
);

/* Finally, after 1000 years, create the pipeline! */

createInfo.props = 0;
Expand Down Expand Up @@ -2204,10 +2217,10 @@ static void SDLGPU_DrawInstancedPrimitives(

SDL_GpuDrawIndexedPrimitives(
renderer->renderPass,
baseVertex,
startIndex,
PrimitiveVerts(primitiveType, primitiveCount),
instanceCount,
startIndex,
baseVertex,
0
);
}
Expand Down Expand Up @@ -2254,9 +2267,9 @@ static void SDLGPU_DrawPrimitives(

SDL_GpuDrawPrimitives(
renderer->renderPass,
vertexStart,
PrimitiveVerts(primitiveType, primitiveCount),
1,
vertexStart,
0
);
}
Expand Down
Loading