generate shader output in msl

This commit is contained in:
Samuliak 2024-05-04 12:13:52 +02:00
parent 16986bf42f
commit 042788cd71
5 changed files with 14 additions and 12 deletions

View file

@ -215,7 +215,7 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
case IR::Attribute::PositionW: { case IR::Attribute::PositionW: {
const bool is_array{IsInputArray(ctx.stage)}; const bool is_array{IsInputArray(ctx.stage)};
const auto input_decorator{is_array ? fmt::format("gl_in[{}].", vertex) : ""}; const auto input_decorator{is_array ? fmt::format("gl_in[{}].", vertex) : ""};
ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle); ctx.AddF32("{}={}{}.{};", inst, input_decorator, "__out.position", swizzle);
break; break;
} }
case IR::Attribute::PointSpriteS: case IR::Attribute::PointSpriteS:
@ -326,7 +326,7 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
case IR::Attribute::PositionY: case IR::Attribute::PositionY:
case IR::Attribute::PositionZ: case IR::Attribute::PositionZ:
case IR::Attribute::PositionW: case IR::Attribute::PositionW:
ctx.Add("gl_Position.{}={};", swizzle, value); ctx.Add("__out.position.{}={};", swizzle, value);
break; break;
case IR::Attribute::ClipDistance0: case IR::Attribute::ClipDistance0:
case IR::Attribute::ClipDistance1: case IR::Attribute::ClipDistance1:
@ -337,7 +337,7 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
case IR::Attribute::ClipDistance6: case IR::Attribute::ClipDistance6:
case IR::Attribute::ClipDistance7: { case IR::Attribute::ClipDistance7: {
const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)}; const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
ctx.Add("gl_ClipDistance[{}]={};", index, value); ctx.Add("IMPLEMENT(gl_ClipDistance)[{}]={};", index, value);
break; break;
} }
default: default:

View file

@ -9,16 +9,18 @@
namespace Shader::Backend::MSL { namespace Shader::Backend::MSL {
namespace { namespace {
// TODO
std::string_view OutputVertexIndex(EmitContext& ctx) { std::string_view OutputVertexIndex(EmitContext& ctx) {
return ctx.stage == Stage::TessellationControl ? "[gl_InvocationID]" : ""; return ctx.stage == Stage::TessellationControl ? "[IMPLEMENT(gl_InvocationID)]" : "";
} }
void InitializeOutputVaryings(EmitContext& ctx) { void InitializeOutputVaryings(EmitContext& ctx) {
if (ctx.uses_geometry_passthrough) { if (ctx.uses_geometry_passthrough) {
return; return;
} }
ctx.Add("__Output __out;");
if (ctx.stage == Stage::VertexB || ctx.stage == Stage::Geometry) { if (ctx.stage == Stage::VertexB || ctx.stage == Stage::Geometry) {
ctx.Add("gl_Position=vec4(0,0,0,1);"); ctx.Add("__out.position=vec4(0,0,0,1);");
} }
for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
if (!ctx.info.stores.Generic(index)) { if (!ctx.info.stores.Generic(index)) {
@ -29,7 +31,7 @@ void InitializeOutputVaryings(EmitContext& ctx) {
size_t element{}; size_t element{};
while (element < info_array.size()) { while (element < info_array.size()) {
const auto& info{info_array.at(element)}; const auto& info{info_array.at(element)};
const auto varying_name{fmt::format("{}{}", info.name, output_decorator)}; const auto varying_name{fmt::format("__out.{}{}", info.name, output_decorator)};
switch (info.num_components) { switch (info.num_components) {
case 1: { case 1: {
const char value{element == 3 ? '1' : '0'}; const char value{element == 3 ? '1' : '0'};
@ -39,15 +41,15 @@ void InitializeOutputVaryings(EmitContext& ctx) {
case 2: case 2:
case 3: case 3:
if (element + info.num_components < 4) { if (element + info.num_components < 4) {
ctx.Add("{}=vec{}(0);", varying_name, info.num_components); ctx.Add("{}=vec<float, {}>(0);", varying_name, info.num_components);
} else { } else {
// last element is the w component, must be initialized to 1 // last element is the w component, must be initialized to 1
const auto zeros{info.num_components == 3 ? "0,0," : "0,"}; const auto zeros{info.num_components == 3 ? "0,0," : "0,"};
ctx.Add("{}=vec{}({}1);", varying_name, info.num_components, zeros); ctx.Add("{}=vec<float, {}>({}1);", varying_name, info.num_components, zeros);
} }
break; break;
case 4: case 4:
ctx.Add("{}=vec4(0,0,0,1);", varying_name); ctx.Add("{}=float4(0,0,0,1);", varying_name);
break; break;
default: default:
break; break;

View file

@ -253,7 +253,6 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
break; break;
case Stage::Fragment: case Stage::Fragment:
stage_name = "fs"; stage_name = "fs";
position_name = "gl_FragCoord";
if (runtime_info.force_early_z) { if (runtime_info.force_early_z) {
header += "layout(early_fragment_tests)in;"; header += "layout(early_fragment_tests)in;";
} }
@ -461,7 +460,7 @@ void EmitContext::DefineHelperFunctions() {
const auto position_idx{is_array ? "gl_in[vertex]." : ""}; const auto position_idx{is_array ? "gl_in[vertex]." : ""};
func += fmt::format("case {}:return {}{}[masked_index];", func += fmt::format("case {}:return {}{}[masked_index];",
static_cast<u32>(IR::Attribute::PositionX) >> 2, position_idx, static_cast<u32>(IR::Attribute::PositionX) >> 2, position_idx,
position_name); "__out.position");
} }
const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2; const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
for (u32 index = 0; index < IR::NUM_GENERICS; ++index) { for (u32 index = 0; index < IR::NUM_GENERICS; ++index) {

View file

@ -144,7 +144,6 @@ public:
Stage stage{}; Stage stage{};
std::string_view stage_name = "invalid"; std::string_view stage_name = "invalid";
std::string_view position_name = "gl_Position";
std::vector<TextureImageDefinition> texture_buffers; std::vector<TextureImageDefinition> texture_buffers;
std::vector<TextureImageDefinition> image_buffers; std::vector<TextureImageDefinition> image_buffers;

View file

@ -277,6 +277,8 @@ std::unique_ptr<GraphicsPipeline> PipelineCache::CreateGraphicsPipeline(
LOG_ERROR(Render_Metal, "failed to create library: {}", LOG_ERROR(Render_Metal, "failed to create library: {}",
error->description()->cString(NS::ASCIIStringEncoding)); error->description()->cString(NS::ASCIIStringEncoding));
// HACK // HACK
std::cout << error->description()->cString(NS::ASCIIStringEncoding) << std::endl;
// HACK
throw; throw;
} }