Skip to content

Commit

Permalink
MCShader: use transformed attribute names in VertexFormat for Shader
Browse files Browse the repository at this point in the history
This ensures that shader attributes are bound correctly. Shader calls
glBindAttributeLocation using the names in the VertexFormat, not the
names in the shader json. By constructing a custom VertexFormat, we can
get around this.

GitHub: #62
  • Loading branch information
DJtheRedstoner authored Oct 23, 2023
1 parent c43acdd commit d6937c4
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,17 @@ internal class MCShader(
//#endif
}

val shaderVertexFormat = vertexFormat?.mc
val shaderVertexFormat = if (vertexFormat != null) {
// Shader calls glBindAttribLocation using the names in the VertexFormat, not the shader json...
// Easiest way to work around this is to construct a custom VertexFormat with our prefixed names.
VertexFormat(ImmutableMap.copyOf(
transformer.attributes.withIndex()
.associate { it.value to vertexFormat.mc.elements[it.index] }))
} else {
// Legacy fallback: The actual element doesn't matter here, Shader only cares about the names
?: VertexFormat(ImmutableMap.copyOf(transformer.attributes.associateWith { VertexFormats.POSITION_ELEMENT }))
VertexFormat(ImmutableMap.copyOf(transformer.attributes.associateWith { VertexFormats.POSITION_ELEMENT }))
}


val name = DigestUtils.sha1Hex(json).lowercase()
return MCShader(Shader(factory, name, shaderVertexFormat), blendState)
Expand Down

0 comments on commit d6937c4

Please sign in to comment.