Skip to content

Commit

Permalink
enable bindless on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Aug 17, 2023
1 parent af32590 commit d6fabf7
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 90 deletions.
18 changes: 13 additions & 5 deletions Engine/gapi/metal/mtdescriptorarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,27 @@ void MtDescriptorArray::set(size_t id, AbstractGraphicsApi::Texture** tex, size_

void MtDescriptorArray::set(size_t id, AbstractGraphicsApi::Buffer** buf, size_t cnt) {
auto& d = desc[id];
d.argsBuf = MtBuffer(dev, nullptr, cnt*sizeof(uint64_t), MTL::ResourceStorageModePrivate);

std::unique_ptr<uint64_t[]> addr(new uint64_t[cnt]);
struct spvBufferDescriptor {
uint64_t ptr;
uint32_t len;
// 4 bytes of padding
};
d.argsBuf = MtBuffer(dev, nullptr, cnt*sizeof(spvBufferDescriptor), MTL::ResourceStorageModePrivate);

std::unique_ptr<spvBufferDescriptor[]> addr(new spvBufferDescriptor[cnt]);
d.args.reserve(cnt);
for(size_t i=0; i<cnt; ++i) {
if(buf[i]==nullptr) {
addr[i] = 0;
addr[i].ptr = 0;
addr[i].len = 0;
continue;
}
addr[i] = reinterpret_cast<MtBuffer*>(buf[i])->impl->gpuAddress();
addr[i].ptr = reinterpret_cast<MtBuffer*>(buf[i])->impl->gpuAddress();
addr[i].len = reinterpret_cast<MtBuffer*>(buf[i])->size;
d.args.push_back(reinterpret_cast<MtBuffer*>(buf[i])->impl.get());
}
d.argsBuf.update(addr.get(), 0, cnt*sizeof(uint64_t));
d.argsBuf.update(addr.get(), 0, cnt*sizeof(spvBufferDescriptor));
}

void MtDescriptorArray::fillBufferSizeBuffer(uint32_t* ret, ShaderReflection::Stage stage) {
Expand Down
2 changes: 1 addition & 1 deletion Engine/gapi/metal/mtdevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
#endif

if(dev.supportsFamily(MTL::GPUFamilyMetal3) && dev.argumentBuffersSupport()>=MTL::ArgumentBuffersTier2) {
// prop.descriptors.nonUniformIndexing = true;
prop.descriptors.nonUniformIndexing = true;
prop.descriptors.maxStorage = 500000;
prop.descriptors.maxTexture = 500000;
prop.descriptors.maxSamplers = 2048;
Expand Down
12 changes: 11 additions & 1 deletion Engine/gapi/metal/mtshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ MtShader::MtShader(MtDevice& dev, const void* source, size_t srcSize) {

std::string msl;
try {
fetchBindings(reinterpret_cast<const uint32_t*>(source),srcSize/4);

spirv_cross::CompilerMSL comp(reinterpret_cast<const uint32_t*>(source),srcSize/4);
optMSL.msl_version = dev.mslVersion;
if(dev.prop.descriptors.nonUniformIndexing) {
optMSL.argument_buffers_tier = spirv_cross::CompilerMSL::Options::ArgumentBuffersTier::Tier2;
}

for(auto& i:lay)
if(i.runtimeSized && (i.cls==ShaderReflection::SsboR || i.cls==ShaderReflection::SsboRW)) {
optMSL.runtime_array_rich_descriptor = true;
}

for(auto& cap:comp.get_declared_capabilities()) {
switch(cap) {
case spv::CapabilityRayQueryKHR: {
Expand All @@ -54,7 +65,6 @@ MtShader::MtShader(MtDevice& dev, const void* source, size_t srcSize) {

msl = comp.compile();

fetchBindings(reinterpret_cast<const uint32_t*>(source),srcSize/4);
bufferSizeBuffer = comp.needs_buffer_size_buffer();

for(auto& i:lay) {
Expand Down
6 changes: 2 additions & 4 deletions Engine/thirdparty/spirv_cross/spirv_cross.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,12 @@ bool Compiler::is_array(const SPIRType &type) const
return !type.array.empty();
}

bool Compiler::is_runtime_size_array(const SPIRType &type) const
bool Compiler::is_runtime_size_array(const SPIRType &type)
{
if (type.array.empty())
return false;
assert(type.array.size() == type.array_size_literal.size());
if (type.array_size_literal[type.array.size() - 1])
return type.array[type.array.size() - 1] == 0;
return false;
return type.array_size_literal.back() && type.array.back() == 0;
}

ShaderResources Compiler::get_shader_resources() const
Expand Down
2 changes: 1 addition & 1 deletion Engine/thirdparty/spirv_cross/spirv_cross.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ class Compiler
bool is_vector(const SPIRType &type) const;
bool is_matrix(const SPIRType &type) const;
bool is_array(const SPIRType &type) const;
bool is_runtime_size_array(const SPIRType &type) const;
static bool is_runtime_size_array(const SPIRType &type);
uint32_t expression_type_id(uint32_t id) const;
const SPIRType &expression_type(uint32_t id) const;
bool expression_is_lvalue(uint32_t id) const;
Expand Down
30 changes: 28 additions & 2 deletions Engine/thirdparty/spirv_cross/spirv_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,10 @@ string CompilerGLSL::to_interpolation_qualifiers(const Bitset &flags)
if (flags.get(DecorationInvariant) && (options.es || options.version >= 120))
res += "invariant ";
if (flags.get(DecorationPerPrimitiveEXT))
res += "perprimitiveEXT ";
{
res += "perprimitiveEXT ";
require_extension_internal("GL_EXT_mesh_shader");
}

if (flags.get(DecorationExplicitInterpAMD))
{
Expand Down Expand Up @@ -1486,6 +1489,10 @@ const char *CompilerGLSL::format_to_glsl(spv::ImageFormat format)
return "rg8i";
case ImageFormatR16i:
return "r16i";
case ImageFormatR64i:
return "r64i";
case ImageFormatR64ui:
return "r64ui";
default:
case ImageFormatUnknown:
return nullptr;
Expand Down Expand Up @@ -3155,6 +3162,10 @@ bool CompilerGLSL::should_force_emit_builtin_block(StorageClass storage)
should_force = true;
}

// Either glslang bug or oversight, but global invariant position does not work in mesh shaders.
if (get_execution_model() == ExecutionModelMeshEXT && position_invariant)
should_force = true;

return should_force;
}

Expand Down Expand Up @@ -3403,6 +3414,8 @@ void CompilerGLSL::emit_declared_builtin_block(StorageClass storage, ExecutionMo
auto itr = builtin_xfb_offsets.find(BuiltInPosition);
if (itr != end(builtin_xfb_offsets))
statement("layout(xfb_offset = ", itr->second, ") vec4 gl_Position;");
else if (position_invariant)
statement("invariant vec4 gl_Position;");
else
statement("vec4 gl_Position;");
}
Expand Down Expand Up @@ -3499,6 +3512,8 @@ void CompilerGLSL::emit_resources()
break;
}

bool global_invariant_position = position_invariant && (options.es || options.version >= 120);

// Emit custom gl_PerVertex for SSO compatibility.
if (options.separate_shader_objects && !options.es && execution.model != ExecutionModelFragment)
{
Expand All @@ -3509,11 +3524,13 @@ void CompilerGLSL::emit_resources()
case ExecutionModelTessellationEvaluation:
emit_declared_builtin_block(StorageClassInput, execution.model);
emit_declared_builtin_block(StorageClassOutput, execution.model);
global_invariant_position = false;
break;

case ExecutionModelVertex:
case ExecutionModelMeshEXT:
emit_declared_builtin_block(StorageClassOutput, execution.model);
global_invariant_position = false;
break;

default:
Expand All @@ -3523,6 +3540,7 @@ void CompilerGLSL::emit_resources()
else if (should_force_emit_builtin_block(StorageClassOutput))
{
emit_declared_builtin_block(StorageClassOutput, execution.model);
global_invariant_position = false;
}
else if (execution.geometry_passthrough)
{
Expand All @@ -3543,7 +3561,7 @@ void CompilerGLSL::emit_resources()
statement("");
}

if (position_invariant && (options.es || options.version >= 120))
if (global_invariant_position)
{
statement("invariant gl_Position;");
statement("");
Expand Down Expand Up @@ -15345,6 +15363,14 @@ string CompilerGLSL::image_type_glsl(const SPIRType &type, uint32_t id)

switch (imagetype.basetype)
{
case SPIRType::Int64:
res = "i64";
require_extension_internal("GL_EXT_shader_image_int64");
break;
case SPIRType::UInt64:
res = "u64";
require_extension_internal("GL_EXT_shader_image_int64");
break;
case SPIRType::Int:
case SPIRType::Short:
case SPIRType::SByte:
Expand Down
Loading

0 comments on commit d6fabf7

Please sign in to comment.