2021-03-24 04:33:45 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV {
|
2021-04-12 22:41:22 +00:00
|
|
|
namespace {
|
|
|
|
void ConvertDepthMode(EmitContext& ctx) {
|
|
|
|
const Id type{ctx.F32[1]};
|
|
|
|
const Id position{ctx.OpLoad(ctx.F32[4], ctx.output_position)};
|
|
|
|
const Id z{ctx.OpCompositeExtract(type, position, 2u)};
|
|
|
|
const Id w{ctx.OpCompositeExtract(type, position, 3u)};
|
|
|
|
const Id screen_depth{ctx.OpFMul(type, ctx.OpFAdd(type, z, w), ctx.Constant(type, 0.5f))};
|
|
|
|
const Id vector{ctx.OpCompositeInsert(ctx.F32[4], screen_depth, position, 2u)};
|
|
|
|
ctx.OpStore(ctx.output_position, vector);
|
|
|
|
}
|
2021-04-13 01:26:15 +00:00
|
|
|
|
|
|
|
void SetFixedPipelinePointSize(EmitContext& ctx) {
|
|
|
|
if (ctx.profile.fixed_state_point_size) {
|
|
|
|
const float point_size{*ctx.profile.fixed_state_point_size};
|
2021-04-19 00:47:31 +00:00
|
|
|
ctx.OpStore(ctx.output_point_size, ctx.Const(point_size));
|
2021-04-13 01:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 04:04:59 +00:00
|
|
|
|
|
|
|
Id DefaultVarying(EmitContext& ctx, u32 num_components, u32 element, Id zero, Id one,
|
|
|
|
Id default_vector) {
|
|
|
|
switch (num_components) {
|
|
|
|
case 1:
|
|
|
|
return element == 3 ? one : zero;
|
|
|
|
case 2:
|
|
|
|
return ctx.ConstantComposite(ctx.F32[2], zero, element + 1 == 3 ? one : zero);
|
|
|
|
case 3:
|
|
|
|
return ctx.ConstantComposite(ctx.F32[3], zero, zero, element + 2 == 3 ? one : zero);
|
|
|
|
case 4:
|
|
|
|
return default_vector;
|
|
|
|
}
|
|
|
|
throw InvalidArgument("Bad element");
|
|
|
|
}
|
2021-04-14 04:32:18 +00:00
|
|
|
|
|
|
|
Id ComparisonFunction(EmitContext& ctx, CompareFunction comparison, Id operand_1, Id operand_2) {
|
|
|
|
switch (comparison) {
|
|
|
|
case CompareFunction::Never:
|
|
|
|
return ctx.false_value;
|
|
|
|
case CompareFunction::Less:
|
|
|
|
return ctx.OpFOrdLessThan(ctx.U1, operand_1, operand_2);
|
|
|
|
case CompareFunction::Equal:
|
|
|
|
return ctx.OpFOrdEqual(ctx.U1, operand_1, operand_2);
|
|
|
|
case CompareFunction::LessThanEqual:
|
|
|
|
return ctx.OpFOrdLessThanEqual(ctx.U1, operand_1, operand_2);
|
|
|
|
case CompareFunction::Greater:
|
|
|
|
return ctx.OpFOrdGreaterThan(ctx.U1, operand_1, operand_2);
|
|
|
|
case CompareFunction::NotEqual:
|
|
|
|
return ctx.OpFOrdNotEqual(ctx.U1, operand_1, operand_2);
|
|
|
|
case CompareFunction::GreaterThanEqual:
|
|
|
|
return ctx.OpFOrdGreaterThanEqual(ctx.U1, operand_1, operand_2);
|
|
|
|
case CompareFunction::Always:
|
|
|
|
return ctx.true_value;
|
|
|
|
}
|
|
|
|
throw InvalidArgument("Comparison function {}", comparison);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlphaTest(EmitContext& ctx) {
|
|
|
|
const auto comparison{*ctx.profile.alpha_test_func};
|
|
|
|
if (comparison == CompareFunction::Always) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-14 06:00:41 +00:00
|
|
|
if (!Sirit::ValidId(ctx.frag_color[0])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-14 04:32:18 +00:00
|
|
|
const Id type{ctx.F32[1]};
|
|
|
|
const Id rt0_color{ctx.OpLoad(ctx.F32[4], ctx.frag_color[0])};
|
|
|
|
const Id alpha{ctx.OpCompositeExtract(type, rt0_color, 3u)};
|
|
|
|
|
|
|
|
const Id true_label{ctx.OpLabel()};
|
|
|
|
const Id discard_label{ctx.OpLabel()};
|
2021-04-19 00:47:31 +00:00
|
|
|
const Id alpha_reference{ctx.Const(ctx.profile.alpha_test_reference)};
|
2021-04-14 04:32:18 +00:00
|
|
|
const Id condition{ComparisonFunction(ctx, comparison, alpha, alpha_reference)};
|
|
|
|
|
2021-04-14 06:00:41 +00:00
|
|
|
ctx.OpSelectionMerge(true_label, spv::SelectionControlMask::MaskNone);
|
2021-04-14 04:32:18 +00:00
|
|
|
ctx.OpBranchConditional(condition, true_label, discard_label);
|
|
|
|
ctx.AddLabel(discard_label);
|
|
|
|
ctx.OpKill();
|
|
|
|
ctx.AddLabel(true_label);
|
|
|
|
}
|
2021-04-12 22:41:22 +00:00
|
|
|
} // Anonymous namespace
|
2021-03-24 04:33:45 +00:00
|
|
|
|
|
|
|
void EmitPrologue(EmitContext& ctx) {
|
|
|
|
if (ctx.stage == Stage::VertexB) {
|
2021-04-19 00:47:31 +00:00
|
|
|
const Id zero{ctx.Const(0.0f)};
|
|
|
|
const Id one{ctx.Const(1.0f)};
|
2021-03-30 01:12:52 +00:00
|
|
|
const Id default_vector{ctx.ConstantComposite(ctx.F32[4], zero, zero, zero, one)};
|
|
|
|
ctx.OpStore(ctx.output_position, default_vector);
|
2021-04-14 04:04:59 +00:00
|
|
|
for (const auto& info : ctx.output_generics) {
|
|
|
|
if (info[0].num_components == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
u32 element{0};
|
|
|
|
while (element < 4) {
|
|
|
|
const auto& element_info{info[element]};
|
|
|
|
const u32 num{element_info.num_components};
|
|
|
|
const Id value{DefaultVarying(ctx, num, element, zero, one, default_vector)};
|
|
|
|
ctx.OpStore(element_info.id, value);
|
|
|
|
element += num;
|
2021-03-24 04:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 01:26:15 +00:00
|
|
|
}
|
|
|
|
if (ctx.stage == Stage::VertexB || ctx.stage == Stage::Geometry) {
|
|
|
|
SetFixedPipelinePointSize(ctx);
|
2021-03-24 04:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitEpilogue(EmitContext& ctx) {
|
2021-04-12 22:41:22 +00:00
|
|
|
if (ctx.stage == Stage::VertexB && ctx.profile.convert_depth_mode) {
|
|
|
|
ConvertDepthMode(ctx);
|
2021-03-24 04:33:45 +00:00
|
|
|
}
|
2021-04-14 04:32:18 +00:00
|
|
|
if (ctx.stage == Stage::Fragment) {
|
|
|
|
AlphaTest(ctx);
|
|
|
|
}
|
2021-03-24 04:33:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:41:22 +00:00
|
|
|
void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) {
|
|
|
|
if (ctx.profile.convert_depth_mode) {
|
|
|
|
ConvertDepthMode(ctx);
|
|
|
|
}
|
2021-04-13 01:26:15 +00:00
|
|
|
if (stream.IsImmediate()) {
|
|
|
|
ctx.OpEmitStreamVertex(ctx.Def(stream));
|
|
|
|
} else {
|
2021-04-12 22:41:22 +00:00
|
|
|
// LOG_WARNING(..., "EmitVertex's stream is not constant");
|
|
|
|
ctx.OpEmitStreamVertex(ctx.u32_zero_value);
|
|
|
|
}
|
2021-04-13 01:26:15 +00:00
|
|
|
// Restore fixed pipeline point size after emitting the vertex
|
|
|
|
SetFixedPipelinePointSize(ctx);
|
2021-04-12 06:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 22:41:22 +00:00
|
|
|
void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) {
|
2021-04-13 01:26:15 +00:00
|
|
|
if (stream.IsImmediate()) {
|
|
|
|
ctx.OpEndStreamPrimitive(ctx.Def(stream));
|
|
|
|
} else {
|
2021-04-12 22:41:22 +00:00
|
|
|
// LOG_WARNING(..., "EndPrimitive's stream is not constant");
|
|
|
|
ctx.OpEndStreamPrimitive(ctx.u32_zero_value);
|
|
|
|
}
|
2021-04-12 06:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 04:33:45 +00:00
|
|
|
} // namespace Shader::Backend::SPIRV
|