Skip to content

Commit

Permalink
Starting cleanup of shader binding names
Browse files Browse the repository at this point in the history
  • Loading branch information
colincornaby committed Sep 12, 2023
1 parent 8799c1a commit bd21483
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ typedef struct {
} vs_BiasNormalsOut;

vertex vs_BiasNormalsOut vs_BiasNormals(Vertex in [[stage_in]],
constant vs_BiasNormalsUniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_BiasNormalsUniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
vs_BiasNormalsOut out;

out.position = float4(in.position, 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct {
} vs_CompCosinesnInOut;

vertex vs_CompCosinesnInOut vs_CompCosines(Vertex in [[stage_in]],
constant vs_CompCosinesUniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_CompCosinesUniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
vs_CompCosinesnInOut out;

out.position = float4(in.position, 1.0);
Expand Down Expand Up @@ -94,7 +94,7 @@ fragment float4 ps_CompCosines(vs_CompCosinesnInOut in [[stage_in]],
texture2d<float> t1 [[ texture(1) ]],
texture2d<float> t2 [[ texture(2) ]],
texture2d<float> t3 [[ texture(3) ]],
constant ps_CompCosinesUniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant ps_CompCosinesUniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
// Composite the cosines together.
// Input map is cosine(pix) for each of
// the 4 waves.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ typedef struct {
texturecube<half> cubicTexture6 [[ texture(FragmentShaderArgumentAttributeCubicTextures + 5), function_constant(hasCubicTexture6) ]];
texturecube<half> cubicTexture7 [[ texture(FragmentShaderArgumentAttributeCubicTextures + 6), function_constant(hasCubicTexture7) ]];
texturecube<half> cubicTexture8 [[ texture(FragmentShaderArgumentAttributeCubicTextures + 7), function_constant(hasCubicTexture8) ]];
const constant plMetalFragmentShaderArgumentBuffer* bufferedUniforms [[ buffer(BufferIndexFragArgBuffer) ]];
const constant plMetalFragmentShaderArgumentBuffer* bufferedUniforms [[ buffer(FragmentShaderArgumentUniforms) ]];
half4 sampleLayer(const size_t index, const half4 vertexColor, const uint8_t passType, float3 sampleCoord) const;
//number of layers is variable, so have to declare these samplers the ugly way
sampler samplers [[ sampler(0), function_constant(hasLayer1) ]];
Expand Down Expand Up @@ -174,9 +174,9 @@ typedef struct
} ShadowCasterInOut;

vertex ColorInOut pipelineVertexShader(Vertex in [[stage_in]],
constant VertexUniforms & uniforms [[ buffer(BufferIndexState) ]],
constant plMetalLights & lights [[ buffer(BufferIndexLights) ]],
constant float4x4 & blendMatrix1 [[ buffer(BufferIndexBlendMatrix1), function_constant(temp_hasOnlyWeight1) ]])
constant VertexUniforms & uniforms [[ buffer( VertexShaderArgumentFixedFunctionUniforms) ]],
constant plMetalLights & lights [[ buffer(VertexShaderArgumentLights) ]],
constant float4x4 & blendMatrix1 [[ buffer(VertexShaderArgumentBlendMatrix1), function_constant(temp_hasOnlyWeight1) ]])
{
ColorInOut out;
//we should have been able to swizzle, but it didn't work in Xcode beta? Try again later.
Expand Down Expand Up @@ -577,7 +577,7 @@ constexpr void blend(half4 srcSample, thread half4 &destSample, const uint32_t b
}

vertex ShadowCasterInOut shadowVertexShader(Vertex in [[stage_in]],
constant VertexUniforms & uniforms [[ buffer(BufferIndexState) ]])
constant VertexUniforms & uniforms [[ buffer( VertexShaderArgumentFixedFunctionUniforms) ]])
{
ShadowCasterInOut out;

Expand Down Expand Up @@ -613,8 +613,8 @@ fragment half4 shadowFragmentShader(ShadowCasterInOut in [[stage_in]])
*/

vertex ColorInOut shadowCastVertexShader(Vertex in [[stage_in]],
constant VertexUniforms & uniforms [[ buffer(BufferIndexState) ]],
constant plShadowState & shadowState [[ buffer(VertexShaderArgumentIndexShadowState) ]])
constant VertexUniforms & uniforms [[ buffer( VertexShaderArgumentFixedFunctionUniforms) ]],
constant plShadowState & shadowState [[ buffer(VertexShaderArgumentShadowState) ]])
{
ColorInOut out;

Expand Down Expand Up @@ -658,9 +658,9 @@ vertex ColorInOut shadowCastVertexShader(Vertex in [[stage_in]],

fragment half4 shadowCastFragmentShader(ColorInOut in [[stage_in]],
texture2d<half> texture [[ texture(16) ]],
constant plMetalShadowCastFragmentShaderArgumentBuffer & fragmentUniforms [[ buffer(BufferIndexShadowCastFragArgBuffer) ]],
constant plMetalShadowCastFragmentShaderArgumentBuffer & fragmentUniforms [[ buffer(FragmentShaderArgumentShadowCastUniforms) ]],
FragmentShaderArguments layers,
constant int & alphaSrc [[ buffer(FragmentShaderArgumentShadowAlphaSrc) ]])
constant int & alphaSrc [[ buffer(FragmentShaderArgumentShadowCastAlphaSrc) ]])
{
float3 sampleCoords = in.texCoord1;
if(fragmentUniforms.pointLightCast) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct {
} vs_GrassInOut;

vertex vs_GrassInOut vs_GrassShader(Vertex in [[stage_in]],
constant vs_GrassUniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_GrassUniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
vs_GrassInOut out;

float4 r0 = (in.position.x * uniforms.waveDirX) + (in.position.y * uniforms.waveDirX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef struct
} ColorInOut;

vertex ColorInOut plateVertexShader(PlateVertex in [[stage_in]],
constant VertexUniforms & uniforms [[ buffer(BufferIndexState) ]],
constant VertexUniforms & uniforms [[ buffer(VertexShaderArgumentFixedFunctionUniforms) ]],
uint v_id [[vertex_id]])
{
ColorInOut out;
Expand All @@ -88,9 +88,9 @@ vertex ColorInOut plateVertexShader(PlateVertex in [[stage_in]],
}

fragment float4 fragmentShader(ColorInOut in [[stage_in]],
constant VertexUniforms & uniforms [[ buffer(BufferIndexState) ]],
constant VertexUniforms & uniforms [[ buffer(VertexShaderArgumentFixedFunctionUniforms) ]],
constant float & alpha [[ buffer(6) ]],
texture2d<half> colorMap [[ texture(Texture) ]])
texture2d<half> colorMap [[ texture( FragmentShaderArgumentTexture) ]])
{
constexpr sampler colorSampler(mip_filter::linear,
mag_filter::linear,
Expand Down
51 changes: 29 additions & 22 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/ShaderSrc/ShaderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,62 @@ typedef __attribute__((__ext_vector_type__(3))) half half3;
typedef __attribute__((__ext_vector_type__(4))) half half4;
#endif

enum plMetalVertexShaderArgumentIndex
enum plMetalVertexShaderArgument
{
/// Material State
BufferIndexState = 2,
VertexShaderArgumentFixedFunctionUniforms = 2,
/// Uniform table for Plasma dynamic shaders
BufferIndexUniforms = 3,
VertexShaderArgumentMaterialShaderUniforms = 3,
/// Light Table
BufferIndexLights = 4,
VertexShaderArgumentLights = 4,
/// Blend matrix for GPU side animation blending
BufferIndexBlendMatrix1 = 6,
VertexShaderArgumentBlendMatrix1 = 6,
/// Describes the state of a shadow caster for shadow cast shader
VertexShaderArgumentIndexShadowState = 9
VertexShaderArgumentShadowState = 9
};

enum plMetalFragmentShaderArgumentIndex
{
/// Texture is a legacy argument for the simpler plate shader
Texture = 1,
FragmentShaderArgumentTexture = 1,
/// Fragment uniforms
BufferIndexShadowCastFragArgBuffer = 4,
FragmentShaderArgumentShadowCastUniforms = 4,
/// Legacy argument buffer
BufferIndexFragArgBuffer = 5,
FragmentShaderArgumentUniforms = 5,
/// Layer index of alpha for shadow fragment shader
FragmentShaderArgumentShadowAlphaSrc = 8
FragmentShaderArgumentShadowCastAlphaSrc = 8
};

enum plMetalVertexAttribute
{
/// position of a vertex
VertexAttributePosition = 0,
VertexAttributePosition = 0,
/// UV of a vertex. Reserves IDs 1-8.
VertexAttributeTexcoord = 1,
VertexAttributeTexcoord = 1,
/// Normal attribute of a vertex
VertexAttributeNormal = 9,
VertexAttributeNormal = 9,
/// Color attribute of a vertex
VertexAttributeColor = 10,
VertexAttributeColor = 10,
/// Animation weight of a vertex
VertexAttributeWeights = 11,
VertexAttributeWeights = 11,
};

/// Arguments to the shader compiler to control output
enum plMetalFunctionConstant
{
FunctionConstantNumUVs = 0,
FunctionConstantNumLayers = 1,
FunctionConstantSources = 2,
FunctionConstantBlendModes = 10,
FunctionConstantLayerFlags = 18,
FunctionConstantNumWeights = 26,
FunctionConstantSampleTypes = 34,
/// Numbrer of UVs in the FVF vertex layout.
FunctionConstantNumUVs = 0,
/// Number of layers the shader will need to render
FunctionConstantNumLayers = 1,
/// Source type of the material texture. Metal needs to know if the texture will
/// be cubic or 2D in advance. Eight values reserved.
FunctionConstantSources = 2,
/// Blend modes for each of the layers.
FunctionConstantBlendModes = 10,
/// Render flags for each layer. Eight values reserved.
FunctionConstantLayerFlags = 18,
/// Numbrer of weights in the FVF vertex layout.
FunctionConstantNumWeights = 26,
};

enum plMetalLayerPassType: uint8_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ typedef struct {
} vs_WaveDev1Lay_7InOut;

vertex vs_WaveDev1Lay_7InOut vs_WaveDec1Lay_7(Vertex in [[stage_in]],
constant vs_WaveDev1Lay_7Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_WaveDev1Lay_7Uniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
vs_WaveDev1Lay_7InOut out;
// Store our input position in world space in r6
float4 worldPosition = float4(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ typedef struct {
} vs_WaveDecEnv7InOut;

vertex vs_WaveDecEnv7InOut vs_WaveDecEnv_7(Vertex in [[stage_in]],
constant vs_WaveDecEnv7Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_WaveDecEnv7Uniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
vs_WaveDecEnv7InOut out;

// Store our input position in world space in r6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ typedef struct {
} waveRipInOut;

vertex waveRipInOut vs_WaveRip7(Vertex in [[stage_in]],
constant vs_WaveRip7Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_WaveRip7Uniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
waveRipInOut out;

// Store our input position in world space in r6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ typedef struct {
} vs_WaveFixedFin7InOut;

vertex vs_WaveFixedFin7InOut vs_WaveFixedFin7(Vertex in [[stage_in]],
constant vs_WaveFixedFin7Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) {
constant vs_WaveFixedFin7Uniforms & uniforms [[ buffer(VertexShaderArgumentMaterialShaderUniforms) ]]) {
vs_WaveFixedFin7InOut out;

// Store our input position in world space in r6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool plMetalFragmentShader::ISetConstants(plMetalPipeline* pipe)
if( fOwner->GetNumConsts() )
{
float *ptr = (float *)fOwner->GetConstBasePtr();
pipe->GetMetalDevice()->CurrentRenderCommandEncoder()->setFragmentBytes(ptr, fOwner->GetNumConsts() * sizeof(float) * 4, BufferIndexUniforms);
pipe->GetMetalDevice()->CurrentRenderCommandEncoder()->setFragmentBytes(ptr, fOwner->GetNumConsts() * sizeof(float) * 4, VertexShaderArgumentMaterialShaderUniforms);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void plMetalMaterialShaderRef::FastEncodeArguments(MTL::RenderCommandEncoder *en
IBuildLayerTexture(encoder, i - GetPassIndex(pass), layer);
}

encoder->setFragmentBuffer(fPassArgumentBuffers[pass], 0, BufferIndexFragArgBuffer);
encoder->setFragmentBuffer(fPassArgumentBuffers[pass], 0, FragmentShaderArgumentUniforms);
}

void plMetalMaterialShaderRef::EncodeArguments(MTL::RenderCommandEncoder *encoder, VertexUniforms *vertexUniforms, uint pass, plMetalFragmentShaderDescription* passDescription, std::vector<plLayerInterface*> *piggyBacks, std::function<plLayerInterface* (plLayerInterface*, uint32_t)> preEncodeTransform, std::function<plLayerInterface* (plLayerInterface*, uint32_t)> postEncodeTransform)
Expand Down Expand Up @@ -180,7 +180,7 @@ void plMetalMaterialShaderRef::EncodeArguments(MTL::RenderCommandEncoder *encode
return layer;
});

encoder->setFragmentBytes(&uniforms, sizeof(plMetalFragmentShaderArgumentBuffer), BufferIndexFragArgBuffer);
encoder->setFragmentBytes(&uniforms, sizeof(plMetalFragmentShaderArgumentBuffer), FragmentShaderArgumentUniforms);
}

void plMetalMaterialShaderRef::EncodeTransform(plLayerInterface* layer, UVOutDescriptor *transform) {
Expand Down Expand Up @@ -216,7 +216,7 @@ void plMetalMaterialShaderRef::prepareTextures(MTL::RenderCommandEncoder *encode

if (plCubicEnvironmap::ConvertNoRef(layer->GetTexture()) != nullptr) {
} else if (plMipmap::ConvertNoRef(layer->GetTexture()) != nullptr || plRenderTarget::ConvertNoRef(layer->GetTexture()) != nullptr) {
encoder->setFragmentTexture(texRef->fTexture, Texture);
encoder->setFragmentTexture(texRef->fTexture, FragmentShaderArgumentTexture);
}
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/plMetalPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ bool plRenderTriListFunc::RenderPrims() const
plProfile_Inc(DrawPrimStatic);

size_t uniformsSize = offsetof(VertexUniforms, uvTransforms) + sizeof(UVOutDescriptor) * fDevice->fPipeline->fCurrNumLayers;
fDevice->CurrentRenderCommandEncoder()->setVertexBytes(fDevice->fPipeline->fCurrentRenderPassUniforms, sizeof(VertexUniforms), BufferIndexState);
fDevice->CurrentRenderCommandEncoder()->setVertexBytes(fDevice->fPipeline->fCurrentRenderPassUniforms, sizeof(VertexUniforms), VertexShaderArgumentFixedFunctionUniforms);

plMetalLights* lights = &fDevice->fPipeline->fLights;
size_t lightSize = offsetof(plMetalLights, lampSources) + (sizeof(plMetalShaderLightSource) * lights->count);

fDevice->CurrentRenderCommandEncoder()->setVertexBytes(lights, sizeof(plMetalLights), BufferIndexLights);
fDevice->CurrentRenderCommandEncoder()->setVertexBytes(lights, sizeof(plMetalLights), VertexShaderArgumentLights);
fDevice->CurrentRenderCommandEncoder()->drawIndexedPrimitives(MTL::PrimitiveTypeTriangle, fNumTris, MTL::IndexTypeUInt16, fDevice->fCurrentIndexBuffer, (sizeof(uint16_t) * fIStart));
}

Expand Down Expand Up @@ -1133,7 +1133,7 @@ void plMetalPipeline::ISetupTransforms(plDrawableSpans* drawable, const plSpan&
{
matrix_float4x4 mat;
hsMatrix2SIMD(drawable->GetPaletteMatrix(span.fBaseMatrix+1), &mat);
fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&mat, sizeof(matrix_float4x4), BufferIndexBlendMatrix1);
fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&mat, sizeof(matrix_float4x4), VertexShaderArgumentBlendMatrix1);
}

fCurrentRenderPassUniforms->projectionMatrix = fDevice.fMatrixProj;
Expand Down Expand Up @@ -2615,7 +2615,7 @@ void plMetalPipeline::IDrawPlate(plPlate* plate)
//FIXME: Hacking the old texture drawing into the plate path
mRef->prepareTextures(fDevice.CurrentRenderCommandEncoder(), 0);

fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&uniforms, sizeof(VertexUniforms), BufferIndexState);
fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&uniforms, sizeof(VertexUniforms), VertexShaderArgumentFixedFunctionUniforms);

pm->EncodeDraw(fDevice.CurrentRenderCommandEncoder());

Expand Down Expand Up @@ -4009,7 +4009,7 @@ void plMetalPipeline::IRenderShadowsOntoSpan(const plRenderPrimFunc& render, con
fShadows[i]->fSelfShadowOn = selfShadowNow;
}

fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&shadowState, sizeof(shadowState), VertexShaderArgumentIndexShadowState);
fDevice.CurrentRenderCommandEncoder()->setVertexBytes(&shadowState, sizeof(shadowState), VertexShaderArgumentShadowState);

#ifndef PLASMA_EXTERNAL_RELEASE
if (!IsDebugFlagSet(plPipeDbg::kFlagNoShadowApply))
Expand Down Expand Up @@ -4081,7 +4081,7 @@ void plMetalPipeline::ISetupShadowRcvTextureStages(hsGMaterial* mat)
fCurrentRenderPassUniforms->uvTransforms[2].transform = tXfm;
}

fDevice.CurrentRenderCommandEncoder()->setFragmentBytes(&layerIndex, sizeof(int), FragmentShaderArgumentShadowAlphaSrc);
fDevice.CurrentRenderCommandEncoder()->setFragmentBytes(&layerIndex, sizeof(int), FragmentShaderArgumentShadowCastAlphaSrc);
}

// ISetShadowLightState //////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4144,7 +4144,7 @@ void plMetalPipeline::ISetupShadowSlaveTextures(plShadowSlave* slave)

plMetalShadowCastFragmentShaderArgumentBuffer uniforms;
uniforms.pointLightCast = slave->fView.GetOrthogonal() ? false : true;
fDevice.CurrentRenderCommandEncoder()->setFragmentBytes(&uniforms, sizeof(plMetalShadowCastFragmentShaderArgumentBuffer), BufferIndexShadowCastFragArgBuffer);
fDevice.CurrentRenderCommandEncoder()->setFragmentBytes(&uniforms, sizeof(plMetalShadowCastFragmentShaderArgumentBuffer), FragmentShaderArgumentShadowCastUniforms);

hsMatrix44 cameraToTexture = slave->fWorldToTexture * c2w;
simd_float4x4 tXfm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool plMetalVertexShader::ISetConstants(plMetalPipeline* pipe)
if( fOwner->GetNumConsts() )
{
float *ptr = (float *)fOwner->GetConstBasePtr();
pipe->GetMetalDevice()->CurrentRenderCommandEncoder()->setVertexBytes(ptr, fOwner->GetNumConsts() * sizeof(float) * 4, BufferIndexUniforms);
pipe->GetMetalDevice()->CurrentRenderCommandEncoder()->setVertexBytes(ptr, fOwner->GetNumConsts() * sizeof(float) * 4, VertexShaderArgumentMaterialShaderUniforms);
}

return true;
Expand Down

0 comments on commit bd21483

Please sign in to comment.