fix: texture coords for texture arrays

This commit is contained in:
Samuliak 2024-10-05 13:51:32 +02:00
parent c921e32d48
commit 1b690aab26
No known key found for this signature in database
2 changed files with 112 additions and 95 deletions

View file

@ -207,11 +207,11 @@ void EmitFPRoundEven16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::I
} }
void EmitFPRoundEven32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { void EmitFPRoundEven32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
ctx.AddF32("{}=roundEven({});", inst, value); ctx.AddF32("{}=rint({});", inst, value);
} }
void EmitFPRoundEven64(EmitContext& ctx, IR::Inst& inst, std::string_view value) { void EmitFPRoundEven64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
ctx.AddF64("{}=roundEven({});", inst, value); ctx.AddF64("{}=rint({});", inst, value);
} }
void EmitFPFloor16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst, void EmitFPFloor16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& inst,

View file

@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project // SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include <iostream>
#include <string_view> #include <string_view>
#include "shader_recompiler/backend/msl/emit_msl_instructions.h" #include "shader_recompiler/backend/msl/emit_msl_instructions.h"
@ -18,17 +19,17 @@ std::string Texture(EmitContext& ctx, const IR::TextureInstInfo& info, const IR:
return fmt::format("tex{}{}", def.binding, index_offset); return fmt::format("tex{}{}", def.binding, index_offset);
} }
void TransformTextureCoords(const IR::TextureInstInfo& info, std::string_view& coords) { std::string TransformTextureCoords(const IR::TextureInstInfo& info, std::string_view coords) {
// If the texture is array, the last component of the coords is the array index // If the texture is array, the last component of the coords is the array index
if (info.type == TextureType::ColorArray1D) { if (info.type == TextureType::ColorArray1D) {
coords = fmt::format("{}.x,{}.y", coords, coords); return fmt::format("{}.x,{}.y", coords, coords);
} } else if (info.type == TextureType::ColorArray2D) {
if (info.type == TextureType::ColorArray2D) { return fmt::format("{}.xy,{}.z", coords, coords);
coords = fmt::format("{}.xy,{}.z", coords, coords); } else if (info.type == TextureType::ColorArrayCube) {
} return fmt::format("{}.xyz,{}.w", coords, coords);
if (info.type == TextureType::ColorArrayCube) {
coords = fmt::format("{}.xyz,{}.w", coords, coords);
} }
return std::string(coords);
} }
std::string Sampler(EmitContext& ctx, const IR::TextureInstInfo& info, const IR::Value& index) { std::string Sampler(EmitContext& ctx, const IR::TextureInstInfo& info, const IR::Value& index) {
@ -189,7 +190,6 @@ void EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Valu
throw NotImplementedException("EmitImageSampleImplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleImplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto sampler{Sampler(ctx, info, index)}; const auto sampler{Sampler(ctx, info, index)};
const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""}; const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)};
@ -203,26 +203,30 @@ void EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Valu
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
const auto offset_str{GetOffsetVec(ctx, offset)}; const auto offset_str{GetOffsetVec(ctx, offset)};
if (ctx.stage == Stage::Fragment) { if (ctx.stage == Stage::Fragment) {
ctx.Add("{}=textureOffset({},{},{}{});", texel, texture, coords, offset_str, bias); ctx.Add("{}=textureOffset({},{},{}{});", texel, texture,
TransformTextureCoords(info, coords), offset_str, bias);
} else { } else {
ctx.Add("{}=textureLodOffset({},{},0.0,{});", texel, texture, coords, offset_str); ctx.Add("{}=textureLodOffset({},{},0.0,{});", texel, texture,
TransformTextureCoords(info, coords), offset_str);
} }
} else { } else {
if (ctx.stage == Stage::Fragment) { if (ctx.stage == Stage::Fragment) {
// TODO: is the array index correct ctx.Add("{}={}.sample({},{}{});", texel, texture, sampler,
ctx.Add("{}={}.sample({},{}{});", texel, texture, sampler, coords, bias); TransformTextureCoords(info, coords), bias);
} else { } else {
ctx.Add("{}={}.sampleLod({},{},0.0);", texel, texture, sampler, coords); ctx.Add("{}={}.sample({},{},0.0);", texel, texture, sampler,
TransformTextureCoords(info, coords));
} }
} }
return; return;
} }
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureOffsetARB({},{},{},{}{}));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureOffsetARB({},{},{},{}{}));",
*sparse_inst, texture, coords, GetOffsetVec(ctx, offset), texel, bias); *sparse_inst, texture, TransformTextureCoords(info, coords),
GetOffsetVec(ctx, offset), texel, bias);
} else { } else {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureARB({},{},{}{}));", *sparse_inst, ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureARB({},{},{}{}));", *sparse_inst,
texture, coords, texel, bias); texture, TransformTextureCoords(info, coords), texel, bias);
} }
} }
@ -237,7 +241,6 @@ void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Valu
throw NotImplementedException("EmitImageSampleExplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleExplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
const bool supports_sparse{ctx.profile.support_gl_sparse_textures}; const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
@ -247,20 +250,21 @@ void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Valu
} }
if (!sparse_inst || !supports_sparse) { if (!sparse_inst || !supports_sparse) {
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
ctx.Add("{}=textureLodOffset({},{},{},{});", texel, texture, coords, lod_lc, ctx.Add("{}=textureLodOffset({},{},{},{});", texel, texture,
GetOffsetVec(ctx, offset)); TransformTextureCoords(info, coords), lod_lc, GetOffsetVec(ctx, offset));
} else { } else {
ctx.Add("{}=textureLod({},{},{});", texel, texture, coords, lod_lc); ctx.Add("{}=textureLod({},{},{});", texel, texture,
TransformTextureCoords(info, coords), lod_lc);
} }
return; return;
} }
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
*sparse_inst, texture, CastToIntVec(coords, info), lod_lc, *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)),
GetOffsetVec(ctx, offset), texel); lod_lc, GetOffsetVec(ctx, offset), texel);
} else { } else {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureLodARB({},{},{},{}));", *sparse_inst, ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureLodARB({},{},{},{}));", *sparse_inst,
texture, coords, lod_lc, texel); texture, TransformTextureCoords(info, coords), lod_lc, texel);
} }
} }
@ -279,7 +283,6 @@ void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::
throw NotImplementedException("EmitImageSampleDrefImplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleDrefImplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""}; const auto bias{info.has_bias ? fmt::format(",{}", bias_lc) : ""};
const bool needs_shadow_ext{NeedsShadowLodExt(info.type)}; const bool needs_shadow_ext{NeedsShadowLodExt(info.type)};
const auto cast{needs_shadow_ext ? "float4" : "float3"}; const auto cast{needs_shadow_ext ? "float4" : "float3"};
@ -294,28 +297,31 @@ void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::
return; return;
} }
const auto d_cast{info.type == TextureType::ColorArray2D ? "float2" : "float3"}; const auto d_cast{info.type == TextureType::ColorArray2D ? "float2" : "float3"};
ctx.AddF32("{}=textureGrad({},{}({},{}),{}(0),{}(0));", inst, texture, cast, coords, dref, ctx.AddF32("{}=textureGrad({},{}({},{}),{}(0),{}(0));", inst, texture, cast,
d_cast, d_cast); TransformTextureCoords(info, coords), dref, d_cast, d_cast);
return; return;
} }
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
const auto offset_str{GetOffsetVec(ctx, offset)}; const auto offset_str{GetOffsetVec(ctx, offset)};
if (ctx.stage == Stage::Fragment) { if (ctx.stage == Stage::Fragment) {
ctx.AddF32("{}=textureOffset({},{}({},{}),{}{});", inst, texture, cast, coords, dref, ctx.AddF32("{}=textureOffset({},{}({},{}),{}{});", inst, texture, cast,
offset_str, bias); TransformTextureCoords(info, coords), dref, offset_str, bias);
} else { } else {
ctx.AddF32("{}=textureLodOffset({},{}({},{}),0.0,{});", inst, texture, cast, coords, ctx.AddF32("{}=textureLodOffset({},{}({},{}),0.0,{});", inst, texture, cast,
dref, offset_str); TransformTextureCoords(info, coords), dref, offset_str);
} }
} else { } else {
if (ctx.stage == Stage::Fragment) { if (ctx.stage == Stage::Fragment) {
if (info.type == TextureType::ColorArrayCube) { if (info.type == TextureType::ColorArrayCube) {
ctx.AddF32("{}=texture({},float4({}),{});", inst, texture, coords, dref); ctx.AddF32("{}=texture({},float4({}),{});", inst, texture,
TransformTextureCoords(info, coords), dref);
} else { } else {
ctx.AddF32("{}=texture({},{}({},{}){});", inst, texture, cast, coords, dref, bias); ctx.AddF32("{}=texture({},{}({},{}){});", inst, texture, cast,
TransformTextureCoords(info, coords), dref, bias);
} }
} else { } else {
ctx.AddF32("{}=textureLod({},{}({},{}),0.0);", inst, texture, cast, coords, dref); ctx.AddF32("{}=textureLod({},{}({},{}),0.0);", inst, texture, cast,
TransformTextureCoords(info, coords), dref);
} }
} }
} }
@ -335,7 +341,6 @@ void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::
throw NotImplementedException("EmitImageSampleDrefExplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleDrefExplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const bool needs_shadow_ext{NeedsShadowLodExt(info.type)}; const bool needs_shadow_ext{NeedsShadowLodExt(info.type)};
const bool use_grad{!ctx.profile.support_gl_texture_shadow_lod && needs_shadow_ext}; const bool use_grad{!ctx.profile.support_gl_texture_shadow_lod && needs_shadow_ext};
const auto cast{needs_shadow_ext ? "float3" : "float3"}; const auto cast{needs_shadow_ext ? "float3" : "float3"};
@ -348,25 +353,26 @@ void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::
return; return;
} }
const auto d_cast{info.type == TextureType::ColorArray2D ? "float2" : "float3"}; const auto d_cast{info.type == TextureType::ColorArray2D ? "float2" : "float3"};
ctx.AddF32("{}=textureGrad({},{}({},{}),{}(0),{}(0));", inst, texture, cast, coords, dref, ctx.AddF32("{}=textureGrad({},{}({},{}),{}(0),{}(0));", inst, texture, cast,
d_cast, d_cast); TransformTextureCoords(info, coords), dref, d_cast, d_cast);
return; return;
} }
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
const auto offset_str{GetOffsetVec(ctx, offset)}; const auto offset_str{GetOffsetVec(ctx, offset)};
if (info.type == TextureType::ColorArrayCube) { if (info.type == TextureType::ColorArrayCube) {
ctx.AddF32("{}=textureLodOffset({},{},{},{},{});", inst, texture, coords, dref, lod_lc, ctx.AddF32("{}=textureLodOffset({},{},{},{},{});", inst, texture,
offset_str); TransformTextureCoords(info, coords), dref, lod_lc, offset_str);
} else { } else {
ctx.AddF32("{}=textureLodOffset({},{}({},{}),{},{});", inst, texture, cast, coords, ctx.AddF32("{}=textureLodOffset({},{}({},{}),{},{});", inst, texture, cast,
dref, lod_lc, offset_str); TransformTextureCoords(info, coords), dref, lod_lc, offset_str);
} }
} else { } else {
if (info.type == TextureType::ColorArrayCube) { if (info.type == TextureType::ColorArrayCube) {
ctx.AddF32("{}=textureLod({},{},{},{});", inst, texture, coords, dref, lod_lc); ctx.AddF32("{}=textureLod({},{},{},{});", inst, texture,
TransformTextureCoords(info, coords), dref, lod_lc);
} else { } else {
ctx.AddF32("{}=textureLod({},{}({},{}),{});", inst, texture, cast, coords, dref, ctx.AddF32("{}=textureLod({},{}({},{}),{});", inst, texture, cast,
lod_lc); TransformTextureCoords(info, coords), dref, lod_lc);
} }
} }
} }
@ -375,7 +381,6 @@ void EmitImageGather(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, const IR::Value& offset, const IR::Value& offset2) { std::string_view coords, const IR::Value& offset, const IR::Value& offset2) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
const bool supports_sparse{ctx.profile.support_gl_sparse_textures}; const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
@ -392,37 +397,39 @@ void EmitImageGather(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
} }
if (!sparse_inst || !supports_sparse) { if (!sparse_inst || !supports_sparse) {
if (offset.IsEmpty()) { if (offset.IsEmpty()) {
ctx.Add("{}=textureGather({},{},int({}));", texel, texture, coords, ctx.Add("{}=textureGather({},{},int({}));", texel, texture,
info.gather_component); TransformTextureCoords(info, coords), info.gather_component);
return; return;
} }
if (offset2.IsEmpty()) { if (offset2.IsEmpty()) {
ctx.Add("{}=textureGatherOffset({},{},{},int({}));", texel, texture, coords, ctx.Add("{}=textureGatherOffset({},{},{},int({}));", texel, texture,
GetOffsetVec(ctx, offset), info.gather_component); TransformTextureCoords(info, coords), GetOffsetVec(ctx, offset),
info.gather_component);
return; return;
} }
// PTP // PTP
const auto offsets{PtpOffsets(offset, offset2)}; const auto offsets{PtpOffsets(offset, offset2)};
ctx.Add("{}=textureGatherOffsets({},{},{},int({}));", texel, texture, coords, offsets, ctx.Add("{}=textureGatherOffsets({},{},{},int({}));", texel, texture,
info.gather_component); TransformTextureCoords(info, coords), offsets, info.gather_component);
return; return;
} }
if (offset.IsEmpty()) { if (offset.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherARB({},{},{},int({})));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherARB({},{},{},int({})));",
*sparse_inst, texture, coords, texel, info.gather_component); *sparse_inst, texture, TransformTextureCoords(info, coords), texel,
info.gather_component);
return; return;
} }
if (offset2.IsEmpty()) { if (offset2.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},{},int({})));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},{},int({})));",
*sparse_inst, texture, CastToIntVec(coords, info), GetOffsetVec(ctx, offset), *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)),
texel, info.gather_component); GetOffsetVec(ctx, offset), texel, info.gather_component);
return; return;
} }
// PTP // PTP
const auto offsets{PtpOffsets(offset, offset2)}; const auto offsets{PtpOffsets(offset, offset2)};
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},{},int({})));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},{},int({})));",
*sparse_inst, texture, CastToIntVec(coords, info), offsets, texel, *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)),
info.gather_component); offsets, texel, info.gather_component);
} }
void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
@ -430,7 +437,6 @@ void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, const IR::Value& inde
std::string_view dref) { std::string_view dref) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
const bool supports_sparse{ctx.profile.support_gl_sparse_textures}; const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
@ -447,34 +453,37 @@ void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, const IR::Value& inde
} }
if (!sparse_inst || !supports_sparse) { if (!sparse_inst || !supports_sparse) {
if (offset.IsEmpty()) { if (offset.IsEmpty()) {
ctx.Add("{}=textureGather({},{},{});", texel, texture, coords, dref); ctx.Add("{}=textureGather({},{},{});", texel, texture,
TransformTextureCoords(info, coords), dref);
return; return;
} }
if (offset2.IsEmpty()) { if (offset2.IsEmpty()) {
ctx.Add("{}=textureGatherOffset({},{},{},{});", texel, texture, coords, dref, ctx.Add("{}=textureGatherOffset({},{},{},{});", texel, texture,
GetOffsetVec(ctx, offset)); TransformTextureCoords(info, coords), dref, GetOffsetVec(ctx, offset));
return; return;
} }
// PTP // PTP
const auto offsets{PtpOffsets(offset, offset2)}; const auto offsets{PtpOffsets(offset, offset2)};
ctx.Add("{}=textureGatherOffsets({},{},{},{});", texel, texture, coords, dref, offsets); ctx.Add("{}=textureGatherOffsets({},{},{},{});", texel, texture,
TransformTextureCoords(info, coords), dref, offsets);
return; return;
} }
if (offset.IsEmpty()) { if (offset.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherARB({},{},{},{}));", *sparse_inst, ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherARB({},{},{},{}));", *sparse_inst,
texture, coords, dref, texel); texture, TransformTextureCoords(info, coords), dref, texel);
return; return;
} }
if (offset2.IsEmpty()) { if (offset2.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},,{},{}));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},,{},{}));",
*sparse_inst, texture, CastToIntVec(coords, info), dref, *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)),
GetOffsetVec(ctx, offset), texel); dref, GetOffsetVec(ctx, offset), texel);
return; return;
} }
// PTP // PTP
const auto offsets{PtpOffsets(offset, offset2)}; const auto offsets{PtpOffsets(offset, offset2)};
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},,{},{}));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTextureGatherOffsetARB({},{},{},,{},{}));",
*sparse_inst, texture, CastToIntVec(coords, info), dref, offsets, texel); *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)), dref,
offsets, texel);
} }
void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
@ -488,7 +497,6 @@ void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
throw NotImplementedException("EmitImageFetch Lod clamp samples"); throw NotImplementedException("EmitImageFetch Lod clamp samples");
} }
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)};
const bool supports_sparse{ctx.profile.support_gl_sparse_textures}; const bool supports_sparse{ctx.profile.support_gl_sparse_textures};
@ -497,7 +505,7 @@ void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
ctx.AddU1("{}=true;", *sparse_inst); ctx.AddU1("{}=true;", *sparse_inst);
} }
if (!sparse_inst || !supports_sparse) { if (!sparse_inst || !supports_sparse) {
const auto int_coords{CoordsCastToInt(coords, info)}; const auto int_coords{TransformTextureCoords(info, CoordsCastToInt(coords, info))};
if (!ms.empty()) { if (!ms.empty()) {
ctx.Add("{}=texelFetch({},{},int({}));", texel, texture, int_coords, ms); ctx.Add("{}=texelFetch({},{},int({}));", texel, texture, int_coords, ms);
} else if (!offset.IsEmpty()) { } else if (!offset.IsEmpty()) {
@ -505,7 +513,8 @@ void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
GetOffsetVec(ctx, offset)); GetOffsetVec(ctx, offset));
} else { } else {
if (info.type == TextureType::Buffer) { if (info.type == TextureType::Buffer) {
ctx.Add("{}=texelFetch({},int({}));", texel, texture, coords); ctx.Add("{}=texelFetch({},int({}));", texel, texture,
TransformTextureCoords(info, coords));
} else { } else {
ctx.Add("{}=texelFetch({},{},int({}));", texel, texture, int_coords, lod); ctx.Add("{}=texelFetch({},{},int({}));", texel, texture, int_coords, lod);
} }
@ -517,11 +526,12 @@ void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
} }
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchOffsetARB({},{},int({}),{},{}));",
*sparse_inst, texture, CastToIntVec(coords, info), lod, GetOffsetVec(ctx, offset), *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)),
texel); lod, GetOffsetVec(ctx, offset), texel);
} else { } else {
ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchARB({},{},int({}),{}));", ctx.AddU1("{}=sparseTexelsResidentARB(sparseTexelFetchARB({},{},int({}),{}));",
*sparse_inst, texture, CastToIntVec(coords, info), lod, texel); *sparse_inst, texture, TransformTextureCoords(info, CastToIntVec(coords, info)),
lod, texel);
} }
} }
@ -564,7 +574,8 @@ void EmitImageQueryLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords) { std::string_view coords) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
return ctx.AddF32x4("{}=float4(textureQueryLod({},{}),0.0,0.0);", inst, texture, coords); return ctx.AddF32x4("{}=float4(textureQueryLod({},{}),0.0,0.0);", inst, texture,
TransformTextureCoords(info, coords));
} }
void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
@ -582,7 +593,6 @@ void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
throw NotImplementedException("EmitImageGradient offset"); throw NotImplementedException("EmitImageGradient offset");
} }
const auto texture{Texture(ctx, info, index)}; const auto texture{Texture(ctx, info, index)};
TransformTextureCoords(info, coords);
const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, MslVarType::F32x4)};
const bool multi_component{info.num_derivatives > 1 || info.has_lod_clamp}; const bool multi_component{info.num_derivatives > 1 || info.has_lod_clamp};
const auto derivatives_vec{ctx.var_alloc.Consume(derivatives)}; const auto derivatives_vec{ctx.var_alloc.Consume(derivatives)};
@ -590,14 +600,15 @@ void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
if (info.num_derivatives >= 3) { if (info.num_derivatives >= 3) {
const auto offset_vec{ctx.var_alloc.Consume(offset)}; const auto offset_vec{ctx.var_alloc.Consume(offset)};
ctx.Add("{}=textureGrad({},{},float3({}.xz, {}.x),float3({}.yw, {}.y));", texel, ctx.Add("{}=textureGrad({},{},float3({}.xz, {}.x),float3({}.yw, {}.y));", texel,
texture, coords, derivatives_vec, offset_vec, derivatives_vec, offset_vec); texture, TransformTextureCoords(info, coords), derivatives_vec, offset_vec,
derivatives_vec, offset_vec);
return; return;
} }
ctx.Add("{}=textureGrad({},{},float2({}.xz),float2({}.yz));", texel, texture, coords, ctx.Add("{}=textureGrad({},{},float2({}.xz),float2({}.yz));", texel, texture,
derivatives_vec, derivatives_vec); TransformTextureCoords(info, coords), derivatives_vec, derivatives_vec);
} else { } else {
ctx.Add("{}=textureGrad({},{},float({}.x),float({}.y));", texel, texture, coords, ctx.Add("{}=textureGrad({},{},float({}.x),float({}.y));", texel, texture,
derivatives_vec, derivatives_vec); TransformTextureCoords(info, coords), derivatives_vec, derivatives_vec);
} }
} }
@ -609,53 +620,56 @@ void EmitImageRead(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
throw NotImplementedException("EmitImageRead Sparse"); throw NotImplementedException("EmitImageRead Sparse");
} }
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32x4("{}=uint4(imageLoad({},{}));", inst, image, CoordsCastToInt(coords, info)); ctx.AddU32x4("{}=uint4(imageLoad({},{}));", inst, image,
TransformTextureCoords(info, CoordsCastToInt(coords, info)));
} }
void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view color) { std::string_view coords, std::string_view color) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.Add("imageStore({},{},{});", image, CoordsCastToInt(coords, info), color); ctx.Add("imageStore({},{},{});", image,
TransformTextureCoords(info, CastToIntVec(coords, info)), color);
} }
void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicAdd({},{},{});", inst, image, CoordsCastToInt(coords, info), value); ctx.AddU32("{}=imageAtomicAdd({},{},{});", inst, image,
TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicSMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicSMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMin({},{},int({}));", inst, image, CoordsCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMin({},{},int({}));", inst, image,
value); TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicUMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicUMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMin({},{},uint({}));", inst, image, CoordsCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMin({},{},uint({}));", inst, image,
value); TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicSMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicSMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMax({},{},int({}));", inst, image, CoordsCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMax({},{},int({}));", inst, image,
value); TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicUMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicUMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMax({},{},uint({}));", inst, image, CoordsCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMax({},{},uint({}));", inst, image,
value); TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicInc32(EmitContext&, IR::Inst&, const IR::Value&, std::string_view, void EmitImageAtomicInc32(EmitContext&, IR::Inst&, const IR::Value&, std::string_view,
@ -672,29 +686,32 @@ void EmitImageAtomicAnd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& ind
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicAnd({},{},{});", inst, image, CoordsCastToInt(coords, info), value); ctx.AddU32("{}=imageAtomicAnd({},{},{});", inst, image,
TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicOr32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicOr32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicOr({},{},{});", inst, image, CoordsCastToInt(coords, info), value); ctx.AddU32("{}=imageAtomicOr({},{},{});", inst, image,
TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicXor32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicXor32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicXor({},{},{});", inst, image, CoordsCastToInt(coords, info), value); ctx.AddU32("{}=imageAtomicXor({},{},{});", inst, image,
TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, void EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view value) { std::string_view coords, std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info, index)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicExchange({},{},{});", inst, image, CoordsCastToInt(coords, info), ctx.AddU32("{}=imageAtomicExchange({},{},{});", inst, image,
value); TransformTextureCoords(info, CastToIntVec(coords, info)), value);
} }
void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) { void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) {