code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string_view>

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/frontend/ir/value.h"

namespace Shader::Backend::GLSL {
namespace {
constexpr char cas_loop[]{
    "for (;;){{uint old={};{}=atomicCompSwap({},old,{}({},{}));if({}==old){{break;}}}}"};

void SharedCasFunction(EmitContext& ctx, IR::Inst& inst, std::string_view offset,
                       std::string_view value, std::string_view function) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
    const std::string smem{fmt::format("smem[{}>>2]", offset)};
    ctx.Add(cas_loop, smem, ret, smem, function, smem, value, ret);
}

void SsboCasFunction(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                     const IR::Value& offset, std::string_view value, std::string_view function) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
    const std::string ssbo{fmt::format("{}_ssbo{}[{}>>2]", ctx.stage_name, binding.U32(),
                                       ctx.var_alloc.Consume(offset))};
    ctx.Add(cas_loop, ssbo, ret, ssbo, function, ssbo, value, ret);
}

void SsboCasFunctionF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                        const IR::Value& offset, std::string_view value,
                        std::string_view function) {
    const std::string ssbo{fmt::format("{}_ssbo{}[{}>>2]", ctx.stage_name, binding.U32(),
                                       ctx.var_alloc.Consume(offset))};
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
    ctx.Add(cas_loop, ssbo, ret, ssbo, function, ssbo, value, ret);
    ctx.AddF32("{}=utof({});", inst, ret);
}
} // Anonymous namespace

void EmitSharedAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                            std::string_view value) {
    ctx.AddU32("{}=atomicAdd(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicSMin32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                            std::string_view value) {
    const std::string u32_value{fmt::format("uint({})", value)};
    SharedCasFunction(ctx, inst, pointer_offset, u32_value, "CasMinS32");
}

void EmitSharedAtomicUMin32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                            std::string_view value) {
    ctx.AddU32("{}=atomicMin(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicSMax32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                            std::string_view value) {
    const std::string u32_value{fmt::format("uint({})", value)};
    SharedCasFunction(ctx, inst, pointer_offset, u32_value, "CasMaxS32");
}

void EmitSharedAtomicUMax32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                            std::string_view value) {
    ctx.AddU32("{}=atomicMax(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicInc32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                           std::string_view value) {
    SharedCasFunction(ctx, inst, pointer_offset, value, "CasIncrement");
}

void EmitSharedAtomicDec32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                           std::string_view value) {
    SharedCasFunction(ctx, inst, pointer_offset, value, "CasDecrement");
}

void EmitSharedAtomicAnd32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                           std::string_view value) {
    ctx.AddU32("{}=atomicAnd(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicOr32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                          std::string_view value) {
    ctx.AddU32("{}=atomicOr(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicXor32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                           std::string_view value) {
    ctx.AddU32("{}=atomicXor(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicExchange32(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                                std::string_view value) {
    ctx.AddU32("{}=atomicExchange(smem[{}>>2],{});", inst, pointer_offset, value);
}

void EmitSharedAtomicExchange64(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                                std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU64("{}=packUint2x32(uvec2(smem[{}>>2],smem[({}+4)>>2]));", inst, pointer_offset,
               pointer_offset);
    ctx.Add("smem[{}>>2]=unpackUint2x32({}).x;smem[({}+4)>>2]=unpackUint2x32({}).y;",
            pointer_offset, value, pointer_offset, value);
}

void EmitSharedAtomicExchange32x2(EmitContext& ctx, IR::Inst& inst, std::string_view pointer_offset,
                                  std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU32x2("{}=uvec2(smem[{}>>2],smem[({}+4)>>2]);", inst, pointer_offset, pointer_offset);
    ctx.Add("smem[{}>>2]={}.x;smem[({}+4)>>2]={}.y;", pointer_offset, value, pointer_offset, value);
}

void EmitStorageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicAdd({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicSMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    const std::string u32_value{fmt::format("uint({})", value)};
    SsboCasFunction(ctx, inst, binding, offset, u32_value, "CasMinS32");
}

void EmitStorageAtomicUMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicMin({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicSMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    const std::string u32_value{fmt::format("uint({})", value)};
    SsboCasFunction(ctx, inst, binding, offset, u32_value, "CasMaxS32");
}

void EmitStorageAtomicUMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicMax({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicInc32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                            const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasIncrement");
}

void EmitStorageAtomicDec32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                            const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasDecrement");
}

void EmitStorageAtomicAnd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                            const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicAnd({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicOr32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                           const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicOr({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicXor32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                            const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicXor({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                                 const IR::Value& offset, std::string_view value) {
    ctx.AddU32("{}=atomicExchange({}_ssbo{}[{}>>2],{});", inst, ctx.stage_name, binding.U32(),
               ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicIAdd64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU64("{}=packUint2x32(uvec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]));", inst,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
               binding.U32(), ctx.var_alloc.Consume(offset));
    ctx.Add("{}_ssbo{}[{}>>2]+=unpackUint2x32({}).x;{}_ssbo{}[({}>>2)+1]+=unpackUint2x32({}).y;",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value, ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicSMin64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU64("{}=packInt2x32(ivec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]));", inst,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
               binding.U32(), ctx.var_alloc.Consume(offset));
    ctx.Add("for(int i=0;i<2;++i){{ "
            "{}_ssbo{}[({}>>2)+i]=uint(min(int({}_ssbo{}[({}>>2)+i]),unpackInt2x32(int64_t({}))[i])"
            ");}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicUMin64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU64("{}=packUint2x32(uvec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]));", inst,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
               binding.U32(), ctx.var_alloc.Consume(offset));
    ctx.Add("for(int i=0;i<2;++i){{ "
            "{}_ssbo{}[({}>>2)+i]=min({}_ssbo{}[({}>>2)+i],unpackUint2x32(uint64_t({}))[i]);}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicSMax64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU64("{}=packInt2x32(ivec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]));", inst,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
               binding.U32(), ctx.var_alloc.Consume(offset));
    ctx.Add("for(int i=0;i<2;++i){{ "
            "{}_ssbo{}[({}>>2)+i]=uint(max(int({}_ssbo{}[({}>>2)+i]),unpackInt2x32(int64_t({}))[i])"
            ");}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicUMax64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU64("{}=packUint2x32(uvec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]));", inst,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
               binding.U32(), ctx.var_alloc.Consume(offset));
    ctx.Add("for(int "
            "i=0;i<2;++i){{{}_ssbo{}[({}>>2)+i]=max({}_ssbo{}[({}>>2)+i],unpackUint2x32(uint64_t({}"
            "))[i]);}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicAnd64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                            const IR::Value& offset, std::string_view value) {
    ctx.AddU64(
        "{}=packUint2x32(uvec2(atomicAnd({}_ssbo{}[{}>>2],unpackUint2x32({}).x),atomicAnd({}_"
        "ssbo{}[({}>>2)+1],unpackUint2x32({}).y)));",
        inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value, ctx.stage_name,
        binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicOr64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                           const IR::Value& offset, std::string_view value) {
    ctx.AddU64("{}=packUint2x32(uvec2(atomicOr({}_ssbo{}[{}>>2],unpackUint2x32({}).x),atomicOr({}_"
               "ssbo{}[({}>>2)+1],unpackUint2x32({}).y)));",
               inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicXor64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                            const IR::Value& offset, std::string_view value) {
    ctx.AddU64(
        "{}=packUint2x32(uvec2(atomicXor({}_ssbo{}[{}>>2],unpackUint2x32({}).x),atomicXor({}_"
        "ssbo{}[({}>>2)+1],unpackUint2x32({}).y)));",
        inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value, ctx.stage_name,
        binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicExchange64(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                                 const IR::Value& offset, std::string_view value) {
    ctx.AddU64("{}=packUint2x32(uvec2(atomicExchange({}_ssbo{}[{}>>2],unpackUint2x32({}).x),"
               "atomicExchange({}_ssbo{}[({}>>2)+1],unpackUint2x32({}).y)));",
               inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value,
               ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicIAdd32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU32x2("{}=uvec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]);", inst, ctx.stage_name,
                 binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name, binding.U32(),
                 ctx.var_alloc.Consume(offset));
    ctx.Add("{}_ssbo{}[{}>>2]+={}.x;{}_ssbo{}[({}>>2)+1]+={}.y;", ctx.stage_name, binding.U32(),
            ctx.var_alloc.Consume(offset), value, ctx.stage_name, binding.U32(),
            ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicSMin32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU32x2("{}=ivec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]);", inst, ctx.stage_name,
                 binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name, binding.U32(),
                 ctx.var_alloc.Consume(offset));
    ctx.Add("for(int "
            "i=0;i<2;++i){{{}_ssbo{}[({}>>2)+i]=uint(min(int({}_ssbo{}[({}>>2)+i]),int({}[i])));}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicUMin32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU32x2("{}=uvec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]);", inst, ctx.stage_name,
                 binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name, binding.U32(),
                 ctx.var_alloc.Consume(offset));
    ctx.Add("for(int i=0;i<2;++i){{ "
            "{}_ssbo{}[({}>>2)+i]=min({}_ssbo{}[({}>>2)+i],{}[i]);}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicSMax32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU32x2("{}=ivec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]);", inst, ctx.stage_name,
                 binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name, binding.U32(),
                 ctx.var_alloc.Consume(offset));
    ctx.Add("for(int "
            "i=0;i<2;++i){{{}_ssbo{}[({}>>2)+i]=uint(max(int({}_ssbo{}[({}>>2)+i]),int({}[i])));}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicUMax32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to non-atomic");
    ctx.AddU32x2("{}=uvec2({}_ssbo{}[{}>>2],{}_ssbo{}[({}>>2)+1]);", inst, ctx.stage_name,
                 binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name, binding.U32(),
                 ctx.var_alloc.Consume(offset));
    ctx.Add("for(int i=0;i<2;++i){{{}_ssbo{}[({}>>2)+i]=max({}_ssbo{}[({}>>2)+i],{}[i]);}}",
            ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), ctx.stage_name,
            binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicAnd32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                              const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to 32x2");
    ctx.AddU32x2("{}=uvec2(atomicAnd({}_ssbo{}[{}>>2],{}.x),atomicAnd({}_ssbo{}[({}>>2)+1],{}.y));",
                 inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value,
                 ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicOr32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to 32x2");
    ctx.AddU32x2("{}=uvec2(atomicOr({}_ssbo{}[{}>>2],{}.x),atomicOr({}_ssbo{}[({}>>2)+1],{}.y));",
                 inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value,
                 ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicXor32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                              const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to 32x2");
    ctx.AddU32x2("{}=uvec2(atomicXor({}_ssbo{}[{}>>2],{}.x),atomicXor({}_ssbo{}[({}>>2)+1],{}.y));",
                 inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value,
                 ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicExchange32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                                   const IR::Value& offset, std::string_view value) {
    LOG_WARNING(Shader_GLSL, "Int64 atomics not supported, fallback to 32x2");
    ctx.AddU32x2("{}=uvec2(atomicExchange({}_ssbo{}[{}>>2],{}.x),atomicExchange({}_ssbo{}[({}>>2)+"
                 "1],{}.y));",
                 inst, ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value,
                 ctx.stage_name, binding.U32(), ctx.var_alloc.Consume(offset), value);
}

void EmitStorageAtomicAddF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                             const IR::Value& offset, std::string_view value) {
    SsboCasFunctionF32(ctx, inst, binding, offset, value, "CasFloatAdd");
}

void EmitStorageAtomicAddF16x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasFloatAdd16x2");
}

void EmitStorageAtomicAddF32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasFloatAdd32x2");
}

void EmitStorageAtomicMinF16x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasFloatMin16x2");
}

void EmitStorageAtomicMinF32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasFloatMin32x2");
}

void EmitStorageAtomicMaxF16x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasFloatMax16x2");
}

void EmitStorageAtomicMaxF32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                               const IR::Value& offset, std::string_view value) {
    SsboCasFunction(ctx, inst, binding, offset, value, "CasFloatMax32x2");
}

void EmitGlobalAtomicIAdd32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicSMin32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicUMin32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicSMax32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicUMax32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicInc32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicDec32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicAnd32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicOr32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicXor32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicExchange32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicIAdd64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicSMin64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicUMin64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicSMax64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicUMax64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicInc64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicDec64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicAnd64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicOr64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicXor64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicExchange64(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicIAdd32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicSMin32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicUMin32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicSMax32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicUMax32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicInc32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicDec32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicAnd32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicOr32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicXor32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicExchange32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicAddF32(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicAddF16x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicAddF32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicMinF16x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicMinF32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicMaxF16x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}

void EmitGlobalAtomicMaxF32x2(EmitContext&) {
    throw NotImplementedException("GLSL Instruction");
}
} // namespace Shader::Backend::GLSL


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"

namespace Shader::Backend::GLSL {
void EmitBarrier(EmitContext& ctx) {
    ctx.Add("barrier();");
}

void EmitWorkgroupMemoryBarrier(EmitContext& ctx) {
    ctx.Add("groupMemoryBarrier();");
}

void EmitDeviceMemoryBarrier(EmitContext& ctx) {
    ctx.Add("memoryBarrier();");
}
} // namespace Shader::Backend::GLSL

repo_name: litucks/torzu

license: mit


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string_view>

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/profile.h"

namespace Shader::Backend::GLSL {
namespace {
void Alias(IR::Inst& inst, const IR::Value& value) {
    if (value.IsImmediate()) {
        return;
    }
    IR::Inst& value_inst{*value.InstRecursive()};
    value_inst.DestructiveAddUsage(inst.UseCount());
    value_inst.DestructiveRemoveUsage();
    inst.SetDefinition(value_inst.Definition<Id>());
}
} // Anonymous namespace

void EmitIdentity(EmitContext&, IR::Inst& inst, const IR::Value& value) {
    Alias(inst, value);
}

void EmitConditionRef(EmitContext& ctx, IR::Inst& inst, const IR::Value& value) {
    // Fake one usage to get a real variable out of the condition
    inst.DestructiveAddUsage(1);
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U1)};
    const auto input{ctx.var_alloc.Consume(value)};
    const auto suffix{ctx.profile.has_gl_bool_ref_bug ? "?true:false" : ""};
    if (ret != input) {
        ctx.Add("{}={}{};", ret, input, suffix);
    }
}

void EmitBitCastU16F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) {
    NotImplemented();
}

void EmitBitCastU32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=ftou({});", inst, value);
}

void EmitBitCastU64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=doubleBitsToUint64({});", inst, value);
}

void EmitBitCastF16U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst) {
    NotImplemented();
}

void EmitBitCastF32U32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=utof({});", inst, value);
}

void EmitBitCastF64U64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=uint64BitsToDouble({});", inst, value);
}

void EmitPackUint2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=packUint2x32({});", inst, value);
}

void EmitUnpackUint2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32x2("{}=unpackUint2x32({});", inst, value);
}

void EmitPackFloat2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=packFloat2x16({});", inst, value);
}

void EmitUnpackFloat2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF16x2("{}=unpackFloat2x16({});", inst, value);
}

void EmitPackHalf2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=packHalf2x16({});", inst, value);
}

void EmitUnpackHalf2x16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32x2("{}=unpackHalf2x16({});", inst, value);
}

void EmitPackDouble2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=packDouble2x32({});", inst, value);
}

void EmitUnpackDouble2x32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32x2("{}=unpackDouble2x32({});", inst, value);
}

} // namespace Shader::Backend::GLSL


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string_view>

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/frontend/ir/value.h"

namespace Shader::Backend::GLSL {
namespace {
constexpr std::string_view SWIZZLE{"xyzw"};
void CompositeInsert(EmitContext& ctx, std::string_view result, std::string_view composite,
                     std::string_view object, u32 index) {
    if (result == composite) {
        // The result is aliased with the composite
        ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
    } else {
        ctx.Add("{}={};{}.{}={};", result, composite, result, SWIZZLE[index], object);
    }
}
} // Anonymous namespace

void EmitCompositeConstructU32x2(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
                                 std::string_view e2) {
    ctx.AddU32x2("{}=uvec2({},{});", inst, e1, e2);
}

void EmitCompositeConstructU32x3(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
                                 std::string_view e2, std::string_view e3) {
    ctx.AddU32x3("{}=uvec3({},{},{});", inst, e1, e2, e3);
}

void EmitCompositeConstructU32x4(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
                                 std::string_view e2, std::string_view e3, std::string_view e4) {
    ctx.AddU32x4("{}=uvec4({},{},{},{});", inst, e1, e2, e3, e4);
}

void EmitCompositeExtractU32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                               u32 index) {
    ctx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);
}

void EmitCompositeExtractU32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                               u32 index) {
    ctx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);
}

void EmitCompositeExtractU32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                               u32 index) {
    ctx.AddU32("{}={}.{};", inst, composite, SWIZZLE[index]);
}

void EmitCompositeInsertU32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                              std::string_view object, u32 index) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x2)};
    CompositeInsert(ctx, ret, composite, object, index);
}

void EmitCompositeInsertU32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                              std::string_view object, u32 index) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x3)};
    CompositeInsert(ctx, ret, composite, object, index);
}

void EmitCompositeInsertU32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                              std::string_view object, u32 index) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x4)};
    CompositeInsert(ctx, ret, composite, object, index);
}

void EmitCompositeConstructF16x2([[maybe_unused]] EmitContext& ctx,
                                 [[maybe_unused]] std::string_view e1,
                                 [[maybe_unused]] std::string_view e2) {
    NotImplemented();
}

void EmitCompositeConstructF16x3([[maybe_unused]] EmitContext& ctx,
                                 [[maybe_unused]] std::string_view e1,
                                 [[maybe_unused]] std::string_view e2,
                                 [[maybe_unused]] std::string_view e3) {
    NotImplemented();
}

void EmitCompositeConstructF16x4([[maybe_unused]] EmitContext& ctx,
                                 [[maybe_unused]] std::string_view e1,
                                 [[maybe_unused]] std::string_view e2,
                                 [[maybe_unused]] std::string_view e3,
                                 [[maybe_unused]] std::string_view e4) {
    NotImplemented();
}

void EmitCompositeExtractF16x2([[maybe_unused]] EmitContext& ctx,
                               [[maybe_unused]] std::string_view composite,
                               [[maybe_unused]] u32 index) {
    NotImplemented();
}

void EmitCompositeExtractF16x3([[maybe_unused]] EmitContext& ctx,
                               [[maybe_unused]] std::string_view composite,
                               [[maybe_unused]] u32 index) {
    NotImplemented();
}

void EmitCompositeExtractF16x4([[maybe_unused]] EmitContext& ctx,
                               [[maybe_unused]] std::string_view composite,
                               [[maybe_unused]] u32 index) {
    NotImplemented();
}

void EmitCompositeInsertF16x2([[maybe_unused]] EmitContext& ctx,
                              [[maybe_unused]] std::string_view composite,
                              [[maybe_unused]] std::string_view object,
                              [[maybe_unused]] u32 index) {
    NotImplemented();
}

void EmitCompositeInsertF16x3([[maybe_unused]] EmitContext& ctx,
                              [[maybe_unused]] std::string_view composite,
                              [[maybe_unused]] std::string_view object,
                              [[maybe_unused]] u32 index) {
    NotImplemented();
}

void EmitCompositeInsertF16x4([[maybe_unused]] EmitContext& ctx,
                              [[maybe_unused]] std::string_view composite,
                              [[maybe_unused]] std::string_view object,
                              [[maybe_unused]] u32 index) {
    NotImplemented();
}

void EmitCompositeConstructF32x2(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
                                 std::string_view e2) {
    ctx.AddF32x2("{}=vec2({},{});", inst, e1, e2);
}

void EmitCompositeConstructF32x3(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
                                 std::string_view e2, std::string_view e3) {
    ctx.AddF32x3("{}=vec3({},{},{});", inst, e1, e2, e3);
}

void EmitCompositeConstructF32x4(EmitContext& ctx, IR::Inst& inst, std::string_view e1,
                                 std::string_view e2, std::string_view e3, std::string_view e4) {
    ctx.AddF32x4("{}=vec4({},{},{},{});", inst, e1, e2, e3, e4);
}

void EmitCompositeExtractF32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                               u32 index) {
    ctx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);
}

void EmitCompositeExtractF32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                               u32 index) {
    ctx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);
}

void EmitCompositeExtractF32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                               u32 index) {
    ctx.AddF32("{}={}.{};", inst, composite, SWIZZLE[index]);
}

void EmitCompositeInsertF32x2(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                              std::string_view object, u32 index) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32x2)};
    CompositeInsert(ctx, ret, composite, object, index);
}

void EmitCompositeInsertF32x3(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                              std::string_view object, u32 index) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32x3)};
    CompositeInsert(ctx, ret, composite, object, index);
}

void EmitCompositeInsertF32x4(EmitContext& ctx, IR::Inst& inst, std::string_view composite,
                              std::string_view object, u32 index) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
    CompositeInsert(ctx, ret, composite, object, index);
}

void EmitCompositeConstructF64x2([[maybe_unused]] EmitContext& ctx) {
    NotImplemented();
}

void EmitCompositeConstructF64x3([[maybe_unused]] EmitContext& ctx) {
    NotImplemented();
}

void EmitCompositeConstructF64x4([[maybe_unused]] EmitContext& ctx) {
    NotImplemented();
}

void EmitCompositeExtractF64x2([[maybe_unused]] EmitContext& ctx) {
    NotImplemented();
}

void EmitCompositeExtractF64x3([[maybe_unused]] EmitContext& ctx) {
    NotImplemented();
}

void EmitCompositeExtractF64x4([[maybe_unused]] EmitContext& ctx) {
    NotImplemented();
}

void EmitCompositeInsertF64x2(EmitContext& ctx, std::string_view composite, std::string_view object,
                              u32 index) {
    ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
}

void EmitCompositeInsertF64x3(EmitContext& ctx, std::string_view composite, std::string_view object,
                              u32 index) {
    ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
}

void EmitCompositeInsertF64x4(EmitContext& ctx, std::string_view composite, std::string_view object,
                              u32 index) {
    ctx.Add("{}.{}={};", composite, SWIZZLE[index], object);
}
} // namespace Shader::Backend::GLSL


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string_view>

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/frontend/ir/value.h"
#include "shader_recompiler/profile.h"
#include "shader_recompiler/runtime_info.h"

namespace Shader::Backend::GLSL {
namespace {
constexpr char SWIZZLE[]{"xyzw"};

u32 CbufIndex(u32 offset) {
    return (offset / 4) % 4;
}

char OffsetSwizzle(u32 offset) {
    return SWIZZLE[CbufIndex(offset)];
}

bool IsInputArray(Stage stage) {
    return stage == Stage::Geometry || stage == Stage::TessellationControl ||
           stage == Stage::TessellationEval;
}

std::string InputVertexIndex(EmitContext& ctx, std::string_view vertex) {
    return IsInputArray(ctx.stage) ? fmt::format("[{}]", vertex) : "";
}

std::string_view OutputVertexIndex(EmitContext& ctx) {
    return ctx.stage == Stage::TessellationControl ? "[gl_InvocationID]" : "";
}

std::string ChooseCbuf(EmitContext& ctx, const IR::Value& binding, std::string_view index) {
    if (binding.IsImmediate()) {
        return fmt::format("{}_cbuf{}[{}]", ctx.stage_name, binding.U32(), index);
    } else {
        const auto binding_var{ctx.var_alloc.Consume(binding)};
        return fmt::format("GetCbufIndirect({},{})", binding_var, index);
    }
}

void GetCbuf(EmitContext& ctx, std::string_view ret, const IR::Value& binding,
             const IR::Value& offset, u32 num_bits, std::string_view cast = {},
             std::string_view bit_offset = {}) {
    const bool is_immediate{offset.IsImmediate()};
    const bool component_indexing_bug{!is_immediate && ctx.profile.has_gl_component_indexing_bug};
    if (is_immediate) {
        const s32 signed_offset{static_cast<s32>(offset.U32())};
        static constexpr u32 cbuf_size{0x10000};
        if (signed_offset < 0 || offset.U32() > cbuf_size) {
            LOG_WARNING(Shader_GLSL, "Immediate constant buffer offset is out of bounds");
            ctx.Add("{}=0u;", ret);
            return;
        }
    }
    const auto offset_var{ctx.var_alloc.Consume(offset)};
    const auto index{is_immediate ? fmt::format("{}", offset.U32() / 16)
                                  : fmt::format("{}>>4", offset_var)};
    const auto swizzle{is_immediate ? fmt::format(".{}", OffsetSwizzle(offset.U32()))
                                    : fmt::format("[({}>>2)%4]", offset_var)};

    const auto cbuf{ChooseCbuf(ctx, binding, index)};
    const auto cbuf_cast{fmt::format("{}({}{{}})", cast, cbuf)};
    const auto extraction{num_bits == 32 ? cbuf_cast
                                         : fmt::format("bitfieldExtract({},int({}),{})", cbuf_cast,
                                                       bit_offset, num_bits)};
    if (!component_indexing_bug) {
        const auto result{fmt::format(fmt::runtime(extraction), swizzle)};
        ctx.Add("{}={};", ret, result);
        return;
    }
    const auto cbuf_offset{fmt::format("{}>>2", offset_var)};
    for (u32 i = 0; i < 4; ++i) {
        const auto swizzle_string{fmt::format(".{}", "xyzw"[i])};
        const auto result{fmt::format(fmt::runtime(extraction), swizzle_string)};
        ctx.Add("if(({}&3)=={}){}={};", cbuf_offset, i, ret, result);
    }
}

void GetCbuf8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, const IR::Value& offset,
              std::string_view cast) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
    if (offset.IsImmediate()) {
        const auto bit_offset{fmt::format("{}", (offset.U32() % 4) * 8)};
        GetCbuf(ctx, ret, binding, offset, 8, cast, bit_offset);
    } else {
        const auto offset_var{ctx.var_alloc.Consume(offset)};
        const auto bit_offset{fmt::format("({}%4)*8", offset_var)};
        GetCbuf(ctx, ret, binding, offset, 8, cast, bit_offset);
    }
}

void GetCbuf16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, const IR::Value& offset,
               std::string_view cast) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
    if (offset.IsImmediate()) {
        const auto bit_offset{fmt::format("{}", ((offset.U32() / 2) % 2) * 16)};
        GetCbuf(ctx, ret, binding, offset, 16, cast, bit_offset);
    } else {
        const auto offset_var{ctx.var_alloc.Consume(offset)};
        const auto bit_offset{fmt::format("(({}>>1)%2)*16", offset_var)};
        GetCbuf(ctx, ret, binding, offset, 16, cast, bit_offset);
    }
}
} // Anonymous namespace

void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                   const IR::Value& offset) {
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
    GetCbuf8(ctx, inst, binding, offset, cast);
}

void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                   const IR::Value& offset) {
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "int" : "ftoi"};
    GetCbuf8(ctx, inst, binding, offset, cast);
}

void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                    const IR::Value& offset) {
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
    GetCbuf16(ctx, inst, binding, offset, cast);
}

void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                    const IR::Value& offset) {
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "int" : "ftoi"};
    GetCbuf16(ctx, inst, binding, offset, cast);
}

void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                    const IR::Value& offset) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
    GetCbuf(ctx, ret, binding, offset, 32, cast);
}

void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                    const IR::Value& offset) {
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32)};
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "utof" : ""};
    GetCbuf(ctx, ret, binding, offset, 32, cast);
}

void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
                      const IR::Value& offset) {
    const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
    if (offset.IsImmediate()) {
        const auto cbuf{fmt::format("{}_cbuf{}", ctx.stage_name, binding.U32())};
        static constexpr u32 cbuf_size{0x10000};
        const u32 u32_offset{offset.U32()};
        const s32 signed_offset{static_cast<s32>(offset.U32())};
        if (signed_offset < 0 || u32_offset > cbuf_size) {
            LOG_WARNING(Shader_GLSL, "Immediate constant buffer offset is out of bounds");
            ctx.AddU32x2("{}=uvec2(0u);", inst);
            return;
        }
        if (u32_offset % 2 == 0) {
            ctx.AddU32x2("{}={}({}[{}].{}{});", inst, cast, cbuf, u32_offset / 16,
                         OffsetSwizzle(u32_offset), OffsetSwizzle(u32_offset + 4));
        } else {
            ctx.AddU32x2("{}=uvec2({}({}[{}].{}),{}({}[{}].{}));", inst, cast, cbuf,
                         u32_offset / 16, OffsetSwizzle(u32_offset), cast, cbuf,
                         (u32_offset + 4) / 16, OffsetSwizzle(u32_offset + 4));
        }
        return;
    }
    const auto offset_var{ctx.var_alloc.Consume(offset)};
    const auto cbuf{ChooseCbuf(ctx, binding, fmt::format("{}>>4", offset_var))};
    if (!ctx.profile.has_gl_component_indexing_bug) {
        ctx.AddU32x2("{}=uvec2({}({}[({}>>2)%4]),{}({}[(({}+4)>>2)%4]));", inst, cast, cbuf,
                     offset_var, cast, cbuf, offset_var);
        return;
    }
    const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x2)};
    const auto cbuf_offset{fmt::format("{}>>2", offset_var)};
    for (u32 swizzle = 0; swizzle < 4; ++swizzle) {
        ctx.Add("if(({}&3)=={}){}=uvec2({}({}.{}),{}({}.{}));", cbuf_offset, swizzle, ret, cast,
                cbuf, "xyzw"[swizzle], cast, cbuf, "xyzw"[(swizzle + 1) % 4]);
    }
}

void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
                      std::string_view vertex) {
    const u32 element{static_cast<u32>(attr) % 4};
    const char swizzle{"xyzw"[element]};
    if (IR::IsGeneric(attr)) {
        const u32 index{IR::GenericAttributeIndex(attr)};
        if (!ctx.runtime_info.previous_stage_stores.Generic(index, element)) {
            if (element == 3) {
                ctx.AddF32("{}=1.f;", inst, attr);
            } else {
                ctx.AddF32("{}=0.f;", inst, attr);
            }
            return;
        }
        ctx.AddF32("{}=in_attr{}{}.{};", inst, index, InputVertexIndex(ctx, vertex), swizzle);
        return;
    }
    switch (attr) {
    case IR::Attribute::PrimitiveId:
        ctx.AddF32("{}=itof(gl_PrimitiveID);", inst);
        break;
    case IR::Attribute::Layer:
        ctx.AddF32("{}=itof(gl_Layer);", inst);
        break;
    case IR::Attribute::PositionX:
    case IR::Attribute::PositionY:
    case IR::Attribute::PositionZ:
    case IR::Attribute::PositionW: {
        const bool is_array{IsInputArray(ctx.stage)};
        const auto input_decorator{is_array ? fmt::format("gl_in[{}].", vertex) : ""};
        ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle);
        break;
    }
    case IR::Attribute::PointSpriteS:
    case IR::Attribute::PointSpriteT:
        ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle);
        break;
    case IR::Attribute::TessellationEvaluationPointU:
    case IR::Attribute::TessellationEvaluationPointV:
        ctx.AddF32("{}=gl_TessCoord.{};", inst, swizzle);
        break;
    case IR::Attribute::InstanceId:
        ctx.AddF32("{}=itof(gl_InstanceID);", inst);
        break;
    case IR::Attribute::VertexId:
        ctx.AddF32("{}=itof(gl_VertexID);", inst);
        break;
    case IR::Attribute::FrontFace:
        ctx.AddF32("{}=itof(gl_FrontFacing?-1:0);", inst);
        break;
    case IR::Attribute::BaseInstance:
        ctx.AddF32("{}=itof(gl_BaseInstance);", inst);
        break;
    case IR::Attribute::BaseVertex:
        ctx.AddF32("{}=itof(gl_BaseVertex);", inst);
        break;
    case IR::Attribute::DrawID:
        ctx.AddF32("{}=itof(gl_DrawID);", inst);
        break;
    default:
        throw NotImplementedException("Get attribute {}", attr);
    }
}

void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, std::string_view) {
    switch (attr) {
    case IR::Attribute::PrimitiveId:
        ctx.AddU32("{}=uint(gl_PrimitiveID);", inst);
        break;
    case IR::Attribute::InstanceId:
        ctx.AddU32("{}=uint(gl_InstanceID);", inst);
        break;
    case IR::Attribute::VertexId:
        ctx.AddU32("{}=uint(gl_VertexID);", inst);
        break;
    case IR::Attribute::BaseInstance:
        ctx.AddU32("{}=uint(gl_BaseInstance);", inst);
        break;
    case IR::Attribute::BaseVertex:
        ctx.AddU32("{}=uint(gl_BaseVertex);", inst);
        break;
    case IR::Attribute::DrawID:
        ctx.AddU32("{}=uint(gl_DrawID);", inst);
        break;
    default:
        throw NotImplementedException("Get U32 attribute {}", attr);
    }
}

void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value,
                      [[maybe_unused]] std::string_view vertex) {
    if (IR::IsGeneric(attr)) {
        const u32 index{IR::GenericAttributeIndex(attr)};
        const u32 attr_element{IR::GenericAttributeElement(attr)};
        const GenericElementInfo& info{ctx.output_generics.at(index).at(attr_element)};
        const auto output_decorator{OutputVertexIndex(ctx)};
        if (info.num_components == 1) {
            ctx.Add("{}{}={};", info.name, output_decorator, value);
        } else {
            const u32 index_element{attr_element - info.first_element};
            ctx.Add("{}{}.{}={};", info.name, output_decorator, "xyzw"[index_element], value);
        }
        return;
    }
    const u32 element{static_cast<u32>(attr) % 4};
    const char swizzle{"xyzw"[element]};
    switch (attr) {
    case IR::Attribute::Layer:
        if (ctx.stage != Stage::Geometry &&
            !ctx.profile.support_viewport_index_layer_non_geometry) {
            LOG_WARNING(Shader_GLSL, "Shader stores viewport layer but device does not support "
                                     "viewport layer extension");
            break;
        }
        ctx.Add("gl_Layer=ftoi({});", value);
        break;
    case IR::Attribute::ViewportIndex:
        if (ctx.stage != Stage::Geometry &&
            !ctx.profile.support_viewport_index_layer_non_geometry) {
            LOG_WARNING(Shader_GLSL, "Shader stores viewport index but device does not support "
                                     "viewport layer extension");
            break;
        }
        ctx.Add("gl_ViewportIndex=ftoi({});", value);
        break;
    case IR::Attribute::ViewportMask:
        if (ctx.stage != Stage::Geometry && !ctx.profile.support_viewport_mask) {
            LOG_WARNING(
                Shader_GLSL,
                "Shader stores viewport mask but device does not support viewport mask extension");
            break;
        }
        ctx.Add("gl_ViewportMask[0]=ftoi({});", value);
        break;
    case IR::Attribute::PointSize:
        ctx.Add("gl_PointSize={};", value);
        break;
    case IR::Attribute::PositionX:
    case IR::Attribute::PositionY:
    case IR::Attribute::PositionZ:
    case IR::Attribute::PositionW:
        ctx.Add("gl_Position.{}={};", swizzle, value);
        break;
    case IR::Attribute::ClipDistance0:
    case IR::Attribute::ClipDistance1:
    case IR::Attribute::ClipDistance2:
    case IR::Attribute::ClipDistance3:
    case IR::Attribute::ClipDistance4:
    case IR::Attribute::ClipDistance5:
    case IR::Attribute::ClipDistance6:
    case IR::Attribute::ClipDistance7: {
        const u32 index{static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::ClipDistance0)};
        ctx.Add("gl_ClipDistance[{}]={};", index, value);
        break;
    }
    default:
        throw NotImplementedException("Set attribute {}", attr);
    }
}

void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, std::string_view offset,
                             std::string_view vertex) {
    const bool is_array{ctx.stage == Stage::Geometry};
    const auto vertex_arg{is_array ? fmt::format(",{}", vertex) : ""};
    ctx.AddF32("{}=IndexedAttrLoad(int({}){});", inst, offset, vertex_arg);
}

void EmitSetAttributeIndexed([[maybe_unused]] EmitContext& ctx,
                             [[maybe_unused]] std::string_view offset,
                             [[maybe_unused]] std::string_view value,
                             [[maybe_unused]] std::string_view vertex) {
    NotImplemented();
}

void EmitGetPatch(EmitContext& ctx, IR::Inst& inst, IR::Patch patch) {
    if (!IR::IsGeneric(patch)) {
        throw NotImplementedException("Non-generic patch load");
    }
    const u32 index{IR::GenericPatchIndex(patch)};
    const u32 element{IR::GenericPatchElement(patch)};
    const char swizzle{"xyzw"[element]};
    ctx.AddF32("{}=patch{}.{};", inst, index, swizzle);
}

void EmitSetPatch(EmitContext& ctx, IR::Patch patch, std::string_view value) {
    if (IR::IsGeneric(patch)) {
        const u32 index{IR::GenericPatchIndex(patch)};
        const u32 element{IR::GenericPatchElement(patch)};
        ctx.Add("patch{}.{}={};", index, "xyzw"[element], value);
        return;
    }
    switch (patch) {
    case IR::Patch::TessellationLodLeft:
    case IR::Patch::TessellationLodRight:
    case IR::Patch::TessellationLodTop:
    case IR::Patch::TessellationLodBottom: {
        const u32 index{static_cast<u32>(patch) - u32(IR::Patch::TessellationLodLeft)};
        ctx.Add("gl_TessLevelOuter[{}]={};", index, value);
        break;
    }
    case IR::Patch::TessellationLodInteriorU:
        ctx.Add("gl_TessLevelInner[0]={};", value);
        break;
    case IR::Patch::TessellationLodInteriorV:
        ctx.Add("gl_TessLevelInner[1]={};", value);
        break;
    default:
        throw NotImplementedException("Patch {}", patch);
    }
}

void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, std::string_view value) {
    const char swizzle{"xyzw"[component]};
    ctx.Add("frag_color{}.{}={};", index, swizzle, value);
}

void EmitSetSampleMask(EmitContext& ctx, std::string_view value) {
    ctx.Add("gl_SampleMask[0]=int({});", value);
}

void EmitSetFragDepth(EmitContext& ctx, std::string_view value) {
    ctx.Add("gl_FragDepth={};", value);
}

void EmitLocalInvocationId(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddU32x3("{}=gl_LocalInvocationID;", inst);
}

void EmitWorkgroupId(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddU32x3("{}=gl_WorkGroupID;", inst);
}

void EmitInvocationId(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddU32("{}=uint(gl_InvocationID);", inst);
}

void EmitInvocationInfo(EmitContext& ctx, IR::Inst& inst) {
    switch (ctx.stage) {
    case Stage::TessellationControl:
    case Stage::TessellationEval:
        ctx.AddU32("{}=uint(gl_PatchVerticesIn)<<16;", inst);
        break;
    default:
        LOG_WARNING(Shader, "(STUBBED) called");
        ctx.AddU32("{}=uint(0x00ff0000);", inst);
    }
}

void EmitSampleId(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddU32("{}=uint(gl_SampleID);", inst);
}

void EmitIsHelperInvocation(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddU1("{}=gl_HelperInvocation;", inst);
}

void EmitSR_WScaleFactorXY(EmitContext& ctx, IR::Inst& inst) {
    LOG_WARNING(Shader, "(STUBBED) called");
}

void EmitSR_WScaleFactorZ(EmitContext& ctx, IR::Inst& inst) {
    LOG_WARNING(Shader, "(STUBBED) called");
}

void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {
    ctx.uses_y_direction = true;
    ctx.AddF32("{}=gl_FrontMaterial.ambient.a;", inst);
}

void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddF32("{}=scaling.z;", inst);
}

void EmitRenderArea(EmitContext& ctx, IR::Inst& inst) {
    ctx.AddF32x4("{}=render_area;", inst);
}

void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) {
    ctx.AddU32("{}=lmem[{}];", inst, word_offset);
}

void EmitWriteLocal(EmitContext& ctx, std::string_view word_offset, std::string_view value) {
    ctx.Add("lmem[{}]={};", word_offset, value);
}

} // namespace Shader::Backend::GLSL


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/exception.h"

namespace Shader::Backend::GLSL {

void EmitJoin(EmitContext&) {
    throw NotImplementedException("Join shouldn't be emitted");
}

void EmitDemoteToHelperInvocation(EmitContext& ctx) {
    ctx.Add("discard;");
}

} // namespace Shader::Backend::GLSL


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string_view>

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/frontend/ir/value.h"

namespace Shader::Backend::GLSL {
void EmitConvertS16F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertS16F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=(int({})&0xffff)|(bitfieldExtract(int({}),31,1)<<15);", inst, value, value);
}

void EmitConvertS16F64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertS32F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertS32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=int({});", inst, value);
}

void EmitConvertS32F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=int({});", inst, value);
}

void EmitConvertS64F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertS64F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=int64_t({});", inst, value);
}

void EmitConvertS64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=int64_t({});", inst, value);
}

void EmitConvertU16F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertU16F32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertU16F64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertU32F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertU32F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=uint({});", inst, value);
}

void EmitConvertU32F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=uint({});", inst, value);
}

void EmitConvertU64F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertU64F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=uint64_t({});", inst, value);
}

void EmitConvertU64F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=uint64_t({});", inst, value);
}

void EmitConvertU64U32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU64("{}=uint64_t({});", inst, value);
}

void EmitConvertU32U64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU32("{}=uint({});", inst, value);
}

void EmitConvertF16F32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF32F16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF32F64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=float({});", inst, value);
}

void EmitConvertF64F32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=double({});", inst, value);
}

void EmitConvertF16S8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16S16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16S32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16S64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16U8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16U32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF16U64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF32S8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF32S16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF32S32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=float(int({}));", inst, value);
}

void EmitConvertF32S64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=float(int64_t({}));", inst, value);
}

void EmitConvertF32U8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF32U16(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=float({}&0xffff);", inst, value);
}

void EmitConvertF32U32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=float({});", inst, value);
}

void EmitConvertF32U64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=float({});", inst, value);
}

void EmitConvertF64S8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF64S16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF64S32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=double(int({}));", inst, value);
}

void EmitConvertF64S64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=double(int64_t({}));", inst, value);
}

void EmitConvertF64U8([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF64U16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitConvertF64U32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=double({});", inst, value);
}

void EmitConvertF64U64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=double({});", inst, value);
}

} // namespace Shader::Backend::GLSL


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string_view>

#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
#include "shader_recompiler/frontend/ir/modifiers.h"
#include "shader_recompiler/frontend/ir/value.h"

namespace Shader::Backend::GLSL {
namespace {
void Compare(EmitContext& ctx, IR::Inst& inst, std::string_view lhs, std::string_view rhs,
             std::string_view op, bool ordered) {
    const auto nan_op{ordered ? "&&!" : "||"};
    ctx.AddU1("{}={}{}{}"
              "{}isnan({}){}isnan({});",
              inst, lhs, op, rhs, nan_op, lhs, nan_op, rhs);
}

bool IsPrecise(const IR::Inst& inst) {
    return inst.Flags<IR::FpControl>().no_contraction;
}
} // Anonymous namespace

void EmitFPAbs16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                 [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPAbs32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=abs({});", inst, value);
}

void EmitFPAbs64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=abs({});", inst, value);
}

void EmitFPAdd16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                 [[maybe_unused]] std::string_view a, [[maybe_unused]] std::string_view b) {
    NotImplemented();
}

void EmitFPAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    if (IsPrecise(inst)) {
        ctx.AddPrecF32("{}={}+{};", inst, a, b);
    } else {
        ctx.AddF32("{}={}+{};", inst, a, b);
    }
}

void EmitFPAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    if (IsPrecise(inst)) {
        ctx.AddPrecF64("{}={}+{};", inst, a, b);
    } else {
        ctx.AddF64("{}={}+{};", inst, a, b);
    }
}

void EmitFPFma16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                 [[maybe_unused]] std::string_view a, [[maybe_unused]] std::string_view b,
                 [[maybe_unused]] std::string_view c) {
    NotImplemented();
}

void EmitFPFma32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b,
                 std::string_view c) {
    if (IsPrecise(inst)) {
        ctx.AddPrecF32("{}=fma({},{},{});", inst, a, b, c);
    } else {
        ctx.AddF32("{}=fma({},{},{});", inst, a, b, c);
    }
}

void EmitFPFma64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b,
                 std::string_view c) {
    if (IsPrecise(inst)) {
        ctx.AddPrecF64("{}=fma({},{},{});", inst, a, b, c);
    } else {
        ctx.AddF64("{}=fma({},{},{});", inst, a, b, c);
    }
}

void EmitFPMax32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    ctx.AddF32("{}=max({},{});", inst, a, b);
}

void EmitFPMax64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    ctx.AddF64("{}=max({},{});", inst, a, b);
}

void EmitFPMin32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    ctx.AddF32("{}=min({},{});", inst, a, b);
}

void EmitFPMin64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    ctx.AddF64("{}=min({},{});", inst, a, b);
}

void EmitFPMul16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                 [[maybe_unused]] std::string_view a, [[maybe_unused]] std::string_view b) {
    NotImplemented();
}

void EmitFPMul32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    if (IsPrecise(inst)) {
        ctx.AddPrecF32("{}={}*{};", inst, a, b);
    } else {
        ctx.AddF32("{}={}*{};", inst, a, b);
    }
}

void EmitFPMul64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
    if (IsPrecise(inst)) {
        ctx.AddPrecF64("{}={}*{};", inst, a, b);
    } else {
        ctx.AddF64("{}={}*{};", inst, a, b);
    }
}

void EmitFPNeg16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                 [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPNeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=0.f-({});", inst, value);
}

void EmitFPNeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=double(0.)-({});", inst, value);
}

void EmitFPSin(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=sin({});", inst, value);
}

void EmitFPCos(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=cos({});", inst, value);
}

void EmitFPExp2(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=exp2({});", inst, value);
}

void EmitFPLog2(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=log2({});", inst, value);
}

void EmitFPRecip32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=(1.0f)/{};", inst, value);
}

void EmitFPRecip64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=1.0/{};", inst, value);
}

void EmitFPRecipSqrt32([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    ctx.AddF32("{}=inversesqrt({});", inst, value);
}

void EmitFPRecipSqrt64([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPSqrt(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=sqrt({});", inst, value);
}

void EmitFPSaturate16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                      [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPSaturate32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=min(max({},0.0),1.0);", inst, value);
}

void EmitFPSaturate64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=min(max({},0.0),1.0);", inst, value);
}

void EmitFPClamp16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                   [[maybe_unused]] std::string_view value,
                   [[maybe_unused]] std::string_view min_value,
                   [[maybe_unused]] std::string_view max_value) {
    NotImplemented();
}

void EmitFPClamp32(EmitContext& ctx, IR::Inst& inst, std::string_view value,
                   std::string_view min_value, std::string_view max_value) {
    // GLSL's clamp does not produce desirable results
    ctx.AddF32("{}=min(max({},float({})),float({}));", inst, value, min_value, max_value);
}

void EmitFPClamp64(EmitContext& ctx, IR::Inst& inst, std::string_view value,
                   std::string_view min_value, std::string_view max_value) {
    // GLSL's clamp does not produce desirable results
    ctx.AddF64("{}=min(max({},double({})),double({}));", inst, value, min_value, max_value);
}

void EmitFPRoundEven16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                       [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPRoundEven32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=roundEven({});", inst, value);
}

void EmitFPRoundEven64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=roundEven({});", inst, value);
}

void EmitFPFloor16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                   [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPFloor32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=floor({});", inst, value);
}

void EmitFPFloor64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=floor({});", inst, value);
}

void EmitFPCeil16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                  [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPCeil32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=ceil({});", inst, value);
}

void EmitFPCeil64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=ceil({});", inst, value);
}

void EmitFPTrunc16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                   [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPTrunc32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF32("{}=trunc({});", inst, value);
}

void EmitFPTrunc64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddF64("{}=trunc({});", inst, value);
}

void EmitFPOrdEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
                      [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPOrdEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                      std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "==", true);
}

void EmitFPOrdEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                      std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "==", true);
}

void EmitFPUnordEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
                        [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPUnordEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                        std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "==", false);
}

void EmitFPUnordEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                        std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "==", false);
}

void EmitFPOrdNotEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
                         [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPOrdNotEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                         std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "!=", true);
}

void EmitFPOrdNotEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                         std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "!=", true);
}

void EmitFPUnordNotEqual16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
                           [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPUnordNotEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                           std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "!=", false);
}

void EmitFPUnordNotEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                           std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "!=", false);
}

void EmitFPOrdLessThan16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
                         [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPOrdLessThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                         std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<", true);
}

void EmitFPOrdLessThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                         std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<", true);
}

void EmitFPUnordLessThan16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view lhs,
                           [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPUnordLessThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                           std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<", false);
}

void EmitFPUnordLessThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                           std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<", false);
}

void EmitFPOrdGreaterThan16([[maybe_unused]] EmitContext& ctx,
                            [[maybe_unused]] std::string_view lhs,
                            [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPOrdGreaterThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                            std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">", true);
}

void EmitFPOrdGreaterThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                            std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">", true);
}

void EmitFPUnordGreaterThan16([[maybe_unused]] EmitContext& ctx,
                              [[maybe_unused]] std::string_view lhs,
                              [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPUnordGreaterThan32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                              std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">", false);
}

void EmitFPUnordGreaterThan64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                              std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">", false);
}

void EmitFPOrdLessThanEqual16([[maybe_unused]] EmitContext& ctx,
                              [[maybe_unused]] std::string_view lhs,
                              [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPOrdLessThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                              std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<=", true);
}

void EmitFPOrdLessThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                              std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<=", true);
}

void EmitFPUnordLessThanEqual16([[maybe_unused]] EmitContext& ctx,
                                [[maybe_unused]] std::string_view lhs,
                                [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPUnordLessThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                                std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<=", false);
}

void EmitFPUnordLessThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                                std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, "<=", false);
}

void EmitFPOrdGreaterThanEqual16([[maybe_unused]] EmitContext& ctx,
                                 [[maybe_unused]] std::string_view lhs,
                                 [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPOrdGreaterThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                                 std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">=", true);
}

void EmitFPOrdGreaterThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                                 std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">=", true);
}

void EmitFPUnordGreaterThanEqual16([[maybe_unused]] EmitContext& ctx,
                                   [[maybe_unused]] std::string_view lhs,
                                   [[maybe_unused]] std::string_view rhs) {
    NotImplemented();
}

void EmitFPUnordGreaterThanEqual32(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                                   std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">=", false);
}

void EmitFPUnordGreaterThanEqual64(EmitContext& ctx, IR::Inst& inst, std::string_view lhs,
                                   std::string_view rhs) {
    Compare(ctx, inst, lhs, rhs, ">=", false);
}

void EmitFPIsNan16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,
                   [[maybe_unused]] std::string_view value) {
    NotImplemented();
}

void EmitFPIsNan32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU1("{}=isnan({});", inst, value);
}

void EmitFPIsNan64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
    ctx.AddU1("{}=isnan({});", inst, value);
}

} // namespace Shader::Backend::GLSL