glsl: Address more feedback. Implement indexed texture reads

This commit is contained in:
ameerj 2021-06-11 02:50:30 -04:00
parent 7d89a82a48
commit e81c73a874
6 changed files with 112 additions and 114 deletions

View file

@ -559,53 +559,45 @@ std::string EmitContext::DefineGlobalMemoryFunctions() {
} }
void EmitContext::SetupImages(Bindings& bindings) { void EmitContext::SetupImages(Bindings& bindings) {
image_buffer_bindings.reserve(info.image_buffer_descriptors.size()); image_buffers.reserve(info.image_buffer_descriptors.size());
for (const auto& desc : info.image_buffer_descriptors) { for (const auto& desc : info.image_buffer_descriptors) {
image_buffer_bindings.push_back(bindings.image); image_buffers.push_back({bindings.image, desc.count});
const auto indices{bindings.image + desc.count};
const auto format{ImageFormatString(desc.format)}; const auto format{ImageFormatString(desc.format)};
for (u32 index = bindings.image; index < indices; ++index) { const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
header += fmt::format("layout(binding={}{}) uniform uimageBuffer img{};", header += fmt::format("layout(binding={}{}) uniform uimageBuffer img{}{};", bindings.image,
bindings.image, format, index); format, bindings.image, array_decorator);
}
bindings.image += desc.count; bindings.image += desc.count;
} }
image_bindings.reserve(info.image_descriptors.size()); images.reserve(info.image_descriptors.size());
for (const auto& desc : info.image_descriptors) { for (const auto& desc : info.image_descriptors) {
image_bindings.push_back(bindings.image); images.push_back({bindings.image, desc.count});
const auto format{ImageFormatString(desc.format)}; const auto format{ImageFormatString(desc.format)};
const auto image_type{ImageType(desc.type)}; const auto image_type{ImageType(desc.type)};
const auto qualifier{desc.is_written ? "" : "readonly "}; const auto qualifier{desc.is_written ? "" : "readonly "};
const auto indices{bindings.image + desc.count}; const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
for (u32 index = bindings.image; index < indices; ++index) { header += fmt::format("layout(binding={}{})uniform {}{} img{}{};", bindings.image, format,
header += fmt::format("layout(binding={}{})uniform {}{} img{};", bindings.image, format, qualifier, image_type, bindings.image, array_decorator);
qualifier, image_type, index);
}
bindings.image += desc.count; bindings.image += desc.count;
} }
} }
void EmitContext::SetupTextures(Bindings& bindings) { void EmitContext::SetupTextures(Bindings& bindings) {
texture_buffer_bindings.reserve(info.texture_buffer_descriptors.size()); texture_buffers.reserve(info.texture_buffer_descriptors.size());
for (const auto& desc : info.texture_buffer_descriptors) { for (const auto& desc : info.texture_buffer_descriptors) {
texture_buffer_bindings.push_back(bindings.texture); texture_buffers.push_back({bindings.texture, desc.count});
const auto sampler_type{SamplerType(TextureType::Buffer, false)}; const auto sampler_type{SamplerType(TextureType::Buffer, false)};
const auto indices{bindings.texture + desc.count}; const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
for (u32 index = bindings.texture; index < indices; ++index) { header += fmt::format("layout(binding={}) uniform {} tex{}{};", bindings.texture,
header += fmt::format("layout(binding={}) uniform {} tex{};", bindings.texture, sampler_type, bindings.texture, array_decorator);
sampler_type, index);
}
bindings.texture += desc.count; bindings.texture += desc.count;
} }
texture_bindings.reserve(info.texture_descriptors.size()); textures.reserve(info.texture_descriptors.size());
for (const auto& desc : info.texture_descriptors) { for (const auto& desc : info.texture_descriptors) {
textures.push_back({bindings.texture, desc.count});
const auto sampler_type{SamplerType(desc.type, desc.is_depth)}; const auto sampler_type{SamplerType(desc.type, desc.is_depth)};
texture_bindings.push_back(bindings.texture); const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""};
const auto indices{bindings.texture + desc.count}; header += fmt::format("layout(binding={}) uniform {} tex{}{};", bindings.texture,
for (u32 index = bindings.texture; index < indices; ++index) { sampler_type, bindings.texture, array_decorator);
header += fmt::format("layout(binding={}) uniform {} tex{};", bindings.texture,
sampler_type, index);
}
bindings.texture += desc.count; bindings.texture += desc.count;
} }
} }

View file

@ -36,6 +36,11 @@ struct GenericElementInfo {
u32 num_components{}; u32 num_components{};
}; };
struct TextureImageDefinition {
u32 binding;
u32 count;
};
class EmitContext { class EmitContext {
public: public:
explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_, explicit EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
@ -142,10 +147,10 @@ public:
std::string_view stage_name = "invalid"; std::string_view stage_name = "invalid";
std::string_view position_name = "gl_Position"; std::string_view position_name = "gl_Position";
std::vector<u32> texture_buffer_bindings; std::vector<TextureImageDefinition> texture_buffers;
std::vector<u32> image_buffer_bindings; std::vector<TextureImageDefinition> image_buffers;
std::vector<u32> texture_bindings; std::vector<TextureImageDefinition> textures;
std::vector<u32> image_bindings; std::vector<TextureImageDefinition> images;
std::array<std::array<GenericElementInfo, 4>, 32> output_generics{}; std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
bool uses_y_direction{}; bool uses_y_direction{};

View file

@ -12,20 +12,18 @@
namespace Shader::Backend::GLSL { namespace Shader::Backend::GLSL {
namespace { namespace {
std::string Texture(EmitContext& ctx, const IR::TextureInstInfo& info) { std::string Texture(EmitContext& ctx, const IR::TextureInstInfo& info, const IR::Value& index) {
if (info.type == TextureType::Buffer) { const auto def{info.type == TextureType::Buffer ? ctx.texture_buffers.at(info.descriptor_index)
return fmt::format("tex{}", ctx.texture_buffer_bindings.at(info.descriptor_index)); : ctx.textures.at(info.descriptor_index)};
} else { const auto index_offset{def.count > 1 ? fmt::format("[{}]", ctx.var_alloc.Consume(index)) : ""};
return fmt::format("tex{}", ctx.texture_bindings.at(info.descriptor_index)); return fmt::format("tex{}{}", def.binding, index_offset);
}
} }
std::string Image(EmitContext& ctx, const IR::TextureInstInfo& info) { std::string Image(EmitContext& ctx, const IR::TextureInstInfo& info, const IR::Value& index) {
if (info.type == TextureType::Buffer) { const auto def{info.type == TextureType::Buffer ? ctx.image_buffers.at(info.descriptor_index)
return fmt::format("img{}", ctx.image_buffer_bindings.at(info.descriptor_index)); : ctx.images.at(info.descriptor_index)};
} else { const auto index_offset{def.count > 1 ? fmt::format("[{}]", ctx.var_alloc.Consume(index)) : ""};
return fmt::format("img{}", ctx.image_bindings.at(info.descriptor_index)); return fmt::format("img{}{}", def.binding, index_offset);
}
} }
std::string CastToIntVec(std::string_view value, const IR::TextureInstInfo& info) { std::string CastToIntVec(std::string_view value, const IR::TextureInstInfo& info) {
@ -137,14 +135,14 @@ IR::Inst* PrepareSparse(IR::Inst& inst) {
} }
} // Anonymous namespace } // Anonymous namespace
void EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst& inst, void EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view bias_lc,
std::string_view bias_lc, const IR::Value& offset) { const IR::Value& offset) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
if (info.has_lod_clamp) { if (info.has_lod_clamp) {
throw NotImplementedException("EmitImageSampleImplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleImplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info)}; const auto texture{Texture(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, GlslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
@ -175,9 +173,9 @@ void EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst& inst,
} }
} }
void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst, void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view lod_lc,
std::string_view lod_lc, const IR::Value& offset) { const IR::Value& offset) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
if (info.has_bias) { if (info.has_bias) {
throw NotImplementedException("EmitImageSampleExplicitLod Bias texture samples"); throw NotImplementedException("EmitImageSampleExplicitLod Bias texture samples");
@ -185,7 +183,7 @@ void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst,
if (info.has_lod_clamp) { if (info.has_lod_clamp) {
throw NotImplementedException("EmitImageSampleExplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleExplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info)}; const auto texture{Texture(ctx, info, index)};
const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
if (!sparse_inst) { if (!sparse_inst) {
@ -208,8 +206,7 @@ void EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst& inst,
} }
} }
void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst, void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index,
std::string_view coords, std::string_view dref, std::string_view coords, std::string_view dref,
std::string_view bias_lc, const IR::Value& offset) { std::string_view bias_lc, const IR::Value& offset) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
@ -223,7 +220,7 @@ void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst,
if (info.has_lod_clamp) { if (info.has_lod_clamp) {
throw NotImplementedException("EmitImageSampleDrefImplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleDrefImplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info)}; const auto texture{Texture(ctx, info, index)};
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 ? "vec4" : "vec3"}; const auto cast{needs_shadow_ext ? "vec4" : "vec3"};
@ -263,8 +260,7 @@ void EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst& inst,
} }
} }
void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst, void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index,
std::string_view coords, std::string_view dref, std::string_view coords, std::string_view dref,
std::string_view lod_lc, const IR::Value& offset) { std::string_view lod_lc, const IR::Value& offset) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
@ -278,7 +274,7 @@ void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst,
if (info.has_lod_clamp) { if (info.has_lod_clamp) {
throw NotImplementedException("EmitImageSampleDrefExplicitLod Lod clamp samples"); throw NotImplementedException("EmitImageSampleDrefExplicitLod Lod clamp samples");
} }
const auto texture{Texture(ctx, info)}; const auto texture{Texture(ctx, info, index)};
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 ? "vec4" : "vec3"}; const auto cast{needs_shadow_ext ? "vec4" : "vec3"};
@ -313,10 +309,10 @@ void EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst& inst,
} }
} }
void EmitImageGather(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, 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)}; const auto texture{Texture(ctx, info, index)};
const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
if (!sparse_inst) { if (!sparse_inst) {
@ -355,11 +351,11 @@ void EmitImageGather(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR
info.gather_component); info.gather_component);
} }
void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, void EmitImageGatherDref(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,
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)}; const auto texture{Texture(ctx, info, index)};
const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
if (!sparse_inst) { if (!sparse_inst) {
@ -395,7 +391,7 @@ void EmitImageGatherDref(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] cons
*sparse_inst, texture, CastToIntVec(coords, info), dref, offsets, texel); *sparse_inst, texture, CastToIntVec(coords, info), dref, offsets, texel);
} }
void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, std::string_view offset, std::string_view lod, std::string_view coords, std::string_view offset, std::string_view lod,
[[maybe_unused]] std::string_view ms) { [[maybe_unused]] std::string_view ms) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
@ -405,7 +401,7 @@ void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR:
if (info.has_lod_clamp) { if (info.has_lod_clamp) {
throw NotImplementedException("EmitImageFetch Lod clamp samples"); throw NotImplementedException("EmitImageFetch Lod clamp samples");
} }
const auto texture{Texture(ctx, info)}; const auto texture{Texture(ctx, info, index)};
const auto sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
if (!sparse_inst) { if (!sparse_inst) {
@ -433,10 +429,10 @@ void EmitImageFetch(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR:
} }
} }
void EmitImageQueryDimensions(EmitContext& ctx, IR::Inst& inst, void EmitImageQueryDimensions(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view lod) { std::string_view lod) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto texture{Texture(ctx, info)}; const auto texture{Texture(ctx, info, index)};
switch (info.type) { switch (info.type) {
case TextureType::Color1D: case TextureType::Color1D:
return ctx.AddU32x4( return ctx.AddU32x4(
@ -460,14 +456,14 @@ void EmitImageQueryDimensions(EmitContext& ctx, IR::Inst& inst,
throw LogicError("Unspecified image type {}", info.type.Value()); throw LogicError("Unspecified image type {}", info.type.Value());
} }
void EmitImageQueryLod(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, 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)}; const auto texture{Texture(ctx, info, index)};
return ctx.AddF32x4("{}=vec4(textureQueryLod({},{}),0.0,0.0);", inst, texture, coords); return ctx.AddF32x4("{}=vec4(textureQueryLod({},{}),0.0,0.0);", inst, texture, coords);
} }
void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
std::string_view coords, const IR::Value& derivatives, std::string_view coords, const IR::Value& derivatives,
const IR::Value& offset, [[maybe_unused]] const IR::Value& lod_clamp) { const IR::Value& offset, [[maybe_unused]] const IR::Value& lod_clamp) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
@ -481,7 +477,7 @@ void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const
if (!offset.IsEmpty()) { if (!offset.IsEmpty()) {
throw NotImplementedException("EmitImageGradient offset"); throw NotImplementedException("EmitImageGradient offset");
} }
const auto texture{Texture(ctx, info)}; const auto texture{Texture(ctx, info, index)};
const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)}; const auto texel{ctx.var_alloc.Define(inst, GlslVarType::F32x4)};
const bool multi_component{info.num_derivates > 1 || info.has_lod_clamp}; const bool multi_component{info.num_derivates > 1 || info.has_lod_clamp};
const auto derivatives_vec{ctx.var_alloc.Consume(derivatives)}; const auto derivatives_vec{ctx.var_alloc.Consume(derivatives)};
@ -494,65 +490,60 @@ void EmitImageGradient(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const
} }
} }
void EmitImageRead(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, void EmitImageRead(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 sparse_inst{PrepareSparse(inst)}; const auto sparse_inst{PrepareSparse(inst)};
if (sparse_inst) { if (sparse_inst) {
throw NotImplementedException("EmitImageRead Sparse"); throw NotImplementedException("EmitImageRead Sparse");
} }
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32x4("{}=uvec4(imageLoad({},{}));", inst, image, TexelFetchCastToInt(coords, info)); ctx.AddU32x4("{}=uvec4(imageLoad({},{}));", inst, image, TexelFetchCastToInt(coords, info));
} }
void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] 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)}; const auto image{Image(ctx, info, index)};
ctx.Add("imageStore({},{},{});", image, TexelFetchCastToInt(coords, info), color); ctx.Add("imageStore({},{},{});", image, TexelFetchCastToInt(coords, info), color);
} }
void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view value) {
std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicAdd({},{},{});", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicAdd({},{},{});", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicSMin32(EmitContext& ctx, IR::Inst& inst, void EmitImageAtomicSMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view value) {
std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMin({},{},int({}));", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMin({},{},int({}));", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicUMin32(EmitContext& ctx, IR::Inst& inst, void EmitImageAtomicUMin32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view value) {
std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMin({},{},uint({}));", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMin({},{},uint({}));", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicSMax32(EmitContext& ctx, IR::Inst& inst, void EmitImageAtomicSMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view value) {
std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMax({},{},int({}));", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMax({},{},int({}));", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicUMax32(EmitContext& ctx, IR::Inst& inst, void EmitImageAtomicUMax32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view value) {
std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicMax({},{},uint({}));", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicMax({},{},uint({}));", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
@ -567,35 +558,34 @@ void EmitImageAtomicDec32(EmitContext&, IR::Inst&, const IR::Value&, std::string
NotImplemented(); NotImplemented();
} }
void EmitImageAtomicAnd32(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] const IR::Value& index, void EmitImageAtomicAnd32(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)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicAnd({},{},{});", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicAnd({},{},{});", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicOr32(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] 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)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicOr({},{},{});", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicOr({},{},{});", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicXor32(EmitContext& ctx, IR::Inst& inst, [[maybe_unused]] 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)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicXor({},{},{});", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicXor({},{},{});", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }
void EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, void EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index,
[[maybe_unused]] const IR::Value& index, std::string_view coords, std::string_view coords, std::string_view value) {
std::string_view value) {
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
const auto image{Image(ctx, info)}; const auto image{Image(ctx, info, index)};
ctx.AddU32("{}=imageAtomicExchange({},{},{});", inst, image, TexelFetchCastToInt(coords, info), ctx.AddU32("{}=imageAtomicExchange({},{},{});", inst, image, TexelFetchCastToInt(coords, info),
value); value);
} }

View file

@ -28,12 +28,12 @@ void EmitSelectU16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::stri
void EmitSelectU32(EmitContext& ctx, IR::Inst& inst, std::string_view cond, void EmitSelectU32(EmitContext& ctx, IR::Inst& inst, std::string_view cond,
std::string_view true_value, std::string_view false_value) { std::string_view true_value, std::string_view false_value) {
ctx.AddU32("{}={}?uint({}):uint({});", inst, cond, true_value, false_value); ctx.AddU32("{}={}?{}:{};", inst, cond, true_value, false_value);
} }
void EmitSelectU64(EmitContext& ctx, IR::Inst& inst, std::string_view cond, void EmitSelectU64(EmitContext& ctx, IR::Inst& inst, std::string_view cond,
std::string_view true_value, std::string_view false_value) { std::string_view true_value, std::string_view false_value) {
ctx.AddU64("{}={}?uint64_t({}):uint64_t({});", inst, cond, true_value, false_value); ctx.AddU64("{}={}?{}:{};", inst, cond, true_value, false_value);
} }
void EmitSelectF16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view cond, void EmitSelectF16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] std::string_view cond,

View file

@ -9,6 +9,17 @@
#include "shader_recompiler/frontend/ir/value.h" #include "shader_recompiler/frontend/ir/value.h"
namespace Shader::Backend::GLSL { namespace Shader::Backend::GLSL {
namespace {
constexpr char cas_loop[]{"for(;;){{uint old_value={};uint "
"cas_result=atomicCompSwap({},old_value,bitfieldInsert({},{},{},{}));"
"if(cas_result==old_value){{break;}}}}"};
void SharedWriteCas(EmitContext& ctx, std::string_view offset, std::string_view value,
std::string_view bit_offset, u32 num_bits) {
const auto smem{fmt::format("smem[{}>>2]", offset)};
ctx.Add(cas_loop, smem, smem, smem, value, bit_offset, num_bits);
}
} // Anonymous namespace
void EmitLoadSharedU8(EmitContext& ctx, IR::Inst& inst, std::string_view offset) { void EmitLoadSharedU8(EmitContext& ctx, IR::Inst& inst, std::string_view offset) {
ctx.AddU32("{}=bitfieldExtract(smem[{}>>2],int({}%4)*8,8);", inst, offset, offset); ctx.AddU32("{}=bitfieldExtract(smem[{}>>2],int({}%4)*8,8);", inst, offset, offset);
} }
@ -39,13 +50,13 @@ void EmitLoadSharedU128(EmitContext& ctx, IR::Inst& inst, std::string_view offse
} }
void EmitWriteSharedU8(EmitContext& ctx, std::string_view offset, std::string_view value) { void EmitWriteSharedU8(EmitContext& ctx, std::string_view offset, std::string_view value) {
ctx.Add("smem[{}>>2]=bitfieldInsert(smem[{}>>2],{},int({}%4)*8,8);", offset, offset, value, const auto bit_offset{fmt::format("int({}%4)*8", offset)};
offset); SharedWriteCas(ctx, offset, value, bit_offset, 8);
} }
void EmitWriteSharedU16(EmitContext& ctx, std::string_view offset, std::string_view value) { void EmitWriteSharedU16(EmitContext& ctx, std::string_view offset, std::string_view value) {
ctx.Add("smem[{}>>2]=bitfieldInsert(smem[{}>>2],{},int(({}>>1)%2)*16,16);", offset, offset, const auto bit_offset{fmt::format("int(({}>>1)%2)*16", offset)};
value, offset); SharedWriteCas(ctx, offset, value, bit_offset, 16);
} }
void EmitWriteSharedU32(EmitContext& ctx, std::string_view offset, std::string_view value) { void EmitWriteSharedU32(EmitContext& ctx, std::string_view offset, std::string_view value) {

View file

@ -13,7 +13,7 @@
namespace OpenGL { namespace OpenGL {
static void LogShader(GLuint shader, std::optional<std::string_view> code = {}) { static void LogShader(GLuint shader, std::string_view code = {}) {
GLint shader_status{}; GLint shader_status{};
glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_status); glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_status);
if (shader_status == GL_FALSE) { if (shader_status == GL_FALSE) {
@ -28,8 +28,8 @@ static void LogShader(GLuint shader, std::optional<std::string_view> code = {})
glGetShaderInfoLog(shader, log_length, nullptr, log.data()); glGetShaderInfoLog(shader, log_length, nullptr, log.data());
if (shader_status == GL_FALSE) { if (shader_status == GL_FALSE) {
LOG_ERROR(Render_OpenGL, "{}", log); LOG_ERROR(Render_OpenGL, "{}", log);
if (code.has_value()) { if (!code.empty()) {
LOG_INFO(Render_OpenGL, "\n{}", *code); LOG_INFO(Render_OpenGL, "\n{}", code);
} }
} else { } else {
LOG_WARNING(Render_OpenGL, "{}", log); LOG_WARNING(Render_OpenGL, "{}", log);