Skip to content

Commit

Permalink
More shader infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 28, 2024
1 parent dcdcecf commit 664ba55
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
66 changes: 66 additions & 0 deletions core/rend/metal/metal_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,69 @@ vertex VertexOut vs_main(VertexIn in [[stage_in]], constant VertexShaderUniforms
return out;
}
)";

MetalShaders::MetalShaders(MTL::Device *device) {
this->device = device;

NS::Error *error = nullptr;
fragmentShaderLibrary = device->newLibrary(NS::String::string(FragmentShaderSource, NS::UTF8StringEncoding), nullptr, &error);
fragmentShaderConstants = MTL::FunctionConstantValues::alloc()->init();

if (!fragmentShaderLibrary) {
ERROR_LOG(RENDERER, "%s", error->localizedDescription()->utf8String());
assert(false);
}

vertexShaderLibrary = device->newLibrary(NS::String::string(VertexShaderSource, NS::UTF8StringEncoding), nullptr, &error);
vertexShaderConstants = MTL::FunctionConstantValues::alloc()->init();

if (!vertexShaderLibrary) {
ERROR_LOG(RENDERER, "%s", error->localizedDescription()->utf8String());
assert(false);
}
}

MTL::Function *MetalShaders::compileShader(const VertexShaderParams &params) {
vertexShaderConstants->setConstantValue(&params.gouraud, MTL::DataTypeBool, static_cast<NS::UInteger>(0));
vertexShaderConstants->setConstantValue(&params.divPosZ, MTL::DataTypeBool, 1);

NS::Error *error = nullptr;

MTL::Function *function = vertexShaderLibrary->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding), &vertexShaderConstants, &error);

if (!function) {
ERROR_LOG(RENDERER, "%s", error->localizedDescription()->utf8String());
assert(false);
}

return function;
}

MTL::Function *MetalShaders::compileShader(const FragmentShaderParams &params) {
vertexShaderConstants->setConstantValue(&params.alphaTest, MTL::DataTypeBool, static_cast<NS::UInteger>(0));
vertexShaderConstants->setConstantValue(&params.insideClipTest, MTL::DataTypeBool, 1);
vertexShaderConstants->setConstantValue(&params.useAlpha, MTL::DataTypeBool, 2);
vertexShaderConstants->setConstantValue(&params.texture, MTL::DataTypeBool, 3);
vertexShaderConstants->setConstantValue(&params.ignoreTexAlpha, MTL::DataTypeBool, 4);
vertexShaderConstants->setConstantValue(&params.shaderInstr, MTL::DataTypeInt, 5);
vertexShaderConstants->setConstantValue(&params.offset, MTL::DataTypeBool, 6);
vertexShaderConstants->setConstantValue(&params.fog, MTL::DataTypeInt, 7);
vertexShaderConstants->setConstantValue(&params.gouraud, MTL::DataTypeBool, 8);
vertexShaderConstants->setConstantValue(&params.bumpmap, MTL::DataTypeBool, 9);
vertexShaderConstants->setConstantValue(&params.clamping, MTL::DataTypeBool, 10);
vertexShaderConstants->setConstantValue(&params.trilinear, MTL::DataTypeBool, 11);
vertexShaderConstants->setConstantValue(&params.palette, MTL::DataTypeInt, 12);
vertexShaderConstants->setConstantValue(&params.divPosZ, MTL::DataTypeBool, 13);
vertexShaderConstants->setConstantValue(&params.dithering, MTL::DataTypeBool, 14);

NS::Error *error = nullptr;

MTL::Function *function = fragmentShaderLibrary->newFunction(NS::String::string("fs_main", NS::UTF8StringEncoding), &vertexShaderConstants, &error);

if (!function) {
ERROR_LOG(RENDERER, "%s", error->localizedDescription()->utf8String());
assert(false);
}

return function;
}
14 changes: 13 additions & 1 deletion core/rend/metal/metal_shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct FragmentShaderParams
class MetalShaders
{
public:
MetalShaders(MTL::Device *device);

MTL::Function *GetVertexShader(const VertexShaderParams& params) { return getShader(vertexShaders, params); }

void term()
Expand All @@ -78,10 +80,20 @@ class MetalShaders
}

fragmentShaders.clear();

vertexShaderLibrary->release();
fragmentShaderLibrary->release();

vertexShaderConstants->release();
fragmentShaderConstants->release();
}

private:
MTL::Device device;
MTL::Device *device;
MTL::Library *vertexShaderLibrary;
MTL::Library *fragmentShaderLibrary;
MTL::FunctionConstantValues *vertexShaderConstants;
MTL::FunctionConstantValues *fragmentShaderConstants;

template<typename T>
MTL::Function *getShader(std::map<u32, MTL::Function*> &map, T params)
Expand Down

0 comments on commit 664ba55

Please sign in to comment.