Skip to content

Commit 6c88b00

Browse files
committed
PR feedback
1 parent ee86000 commit 6c88b00

File tree

9 files changed

+583
-388
lines changed

9 files changed

+583
-388
lines changed

main.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,20 @@ static int main_inner(int argc, char *argv[])
780780
return EXIT_FAILURE;
781781
}
782782

783-
unique_ptr<CompilerGLSL> compiler;
783+
// Special case reflection because it has little to do with the path followed by code-outputting compilers
784+
if (!args.reflect.empty())
785+
{
786+
CompilerReflection compiler(read_spirv_file(args.input));
787+
compiler.set_format(args.reflect);
788+
auto json = compiler.compile();
789+
if (args.output)
790+
write_string_to_file(args.output, json.c_str());
791+
else
792+
printf("%s", json.c_str());
793+
return EXIT_SUCCESS;
794+
}
784795

796+
unique_ptr<CompilerGLSL> compiler;
785797
bool combined_image_samplers = false;
786798
bool build_dummy_sampler = false;
787799

@@ -791,13 +803,6 @@ static int main_inner(int argc, char *argv[])
791803
if (args.cpp_interface_name)
792804
static_cast<CompilerCPP *>(compiler.get())->set_interface_name(args.cpp_interface_name);
793805
}
794-
else if (!args.reflect.empty())
795-
{
796-
combined_image_samplers = !args.vulkan_semantics;
797-
build_dummy_sampler = true;
798-
compiler = unique_ptr<CompilerGLSL>(new CompilerReflection(read_spirv_file(args.input)));
799-
static_cast<CompilerReflection *>(compiler.get())->set_format(args.reflect);
800-
}
801806
else if (args.msl)
802807
{
803808
compiler = unique_ptr<CompilerMSL>(new CompilerMSL(read_spirv_file(args.input)));

msvc/SPIRV-Cross.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<ClCompile Include="..\spirv_cpp.cpp" />
128128
<ClCompile Include="..\spirv_cross.cpp" />
129129
<ClCompile Include="..\spirv_glsl.cpp" />
130+
<ClCompile Include="..\spirv_reflect.cpp" />
130131
<ClCompile Include="..\spirv_hlsl.cpp" />
131132
<ClCompile Include="..\spirv_msl.cpp" />
132133
<ClCompile Include="..\spirv_cfg.cpp" />
@@ -138,6 +139,7 @@
138139
<ClInclude Include="..\spirv_cpp.hpp" />
139140
<ClInclude Include="..\spirv_cross.hpp" />
140141
<ClInclude Include="..\spirv_glsl.hpp" />
142+
<ClInclude Include="..\spirv_reflect.hpp" />
141143
<ClInclude Include="..\spirv.hpp" />
142144
<ClInclude Include="..\spirv_hlsl.hpp" />
143145
<ClInclude Include="..\spirv_msl.hpp" />

msvc/SPIRV-Cross.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClCompile Include="..\spirv_glsl.cpp">
2525
<Filter>Source Files</Filter>
2626
</ClCompile>
27+
<ClCompile Include="..\spirv_reflect.cpp">
28+
<Filter>Source Files</Filter>
29+
</ClCompile>
2730
<ClCompile Include="..\spirv_cpp.cpp">
2831
<Filter>Source Files</Filter>
2932
</ClCompile>
@@ -50,6 +53,9 @@
5053
<ClInclude Include="..\spirv_glsl.hpp">
5154
<Filter>Header Files</Filter>
5255
</ClInclude>
56+
<ClInclude Include="..\spirv_reflect.hpp">
57+
<Filter>Header Files</Filter>
58+
</ClInclude>
5359
<ClInclude Include="..\spirv.hpp">
5460
<Filter>Header Files</Filter>
5561
</ClInclude>

spirv_cross.cpp

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4567,3 +4567,292 @@ bool Compiler::instruction_to_result_type(uint32_t &result_type, uint32_t &resul
45674567
return false;
45684568
}
45694569
}
4570+
4571+
Bitset Compiler::combined_decoration_for_member(const SPIRType &type, uint32_t index) const
4572+
{
4573+
Bitset flags;
4574+
auto &memb = meta[type.self].members;
4575+
if (index >= memb.size())
4576+
return flags;
4577+
auto &dec = memb[index];
4578+
4579+
// If our type is a struct, traverse all the members as well recursively.
4580+
flags.merge_or(dec.decoration_flags);
4581+
for (uint32_t i = 0; i < type.member_types.size(); i++)
4582+
flags.merge_or(combined_decoration_for_member(get<SPIRType>(type.member_types[i]), i));
4583+
4584+
return flags;
4585+
}
4586+
4587+
// The optional id parameter indicates the object whose type we are trying
4588+
// to find the description for. It is optional. Most type descriptions do not
4589+
// depend on a specific object's use of that type.
4590+
string Compiler::type_to_string(const SPIRType &type)
4591+
{
4592+
switch (type.basetype)
4593+
{
4594+
case SPIRType::Struct:
4595+
return to_name(type.self);
4596+
4597+
case SPIRType::Image:
4598+
case SPIRType::SampledImage:
4599+
return image_type_to_string(type);
4600+
4601+
case SPIRType::Sampler:
4602+
// The depth field is set by calling code based on the variable ID of the sampler, effectively reintroducing
4603+
// this distinction into the type system.
4604+
return "sampler";
4605+
4606+
case SPIRType::Void:
4607+
return "void";
4608+
4609+
default:
4610+
break;
4611+
}
4612+
4613+
if (type.vecsize == 1 && type.columns == 1) // Scalar builtin
4614+
{
4615+
switch (type.basetype)
4616+
{
4617+
case SPIRType::Boolean:
4618+
return "bool";
4619+
case SPIRType::Int:
4620+
return "int";
4621+
case SPIRType::UInt:
4622+
return "uint";
4623+
case SPIRType::AtomicCounter:
4624+
return "atomic_uint";
4625+
case SPIRType::Half:
4626+
return "float16_t";
4627+
case SPIRType::Float:
4628+
return "float";
4629+
case SPIRType::Double:
4630+
return "double";
4631+
case SPIRType::Int64:
4632+
return "int64_t";
4633+
case SPIRType::UInt64:
4634+
return "uint64_t";
4635+
default:
4636+
return "???";
4637+
}
4638+
}
4639+
else if (type.vecsize > 1 && type.columns == 1) // Vector builtin
4640+
{
4641+
switch (type.basetype)
4642+
{
4643+
case SPIRType::Boolean:
4644+
return join("bvec", type.vecsize);
4645+
case SPIRType::Int:
4646+
return join("ivec", type.vecsize);
4647+
case SPIRType::UInt:
4648+
return join("uvec", type.vecsize);
4649+
case SPIRType::Half:
4650+
return join("f16vec", type.vecsize);
4651+
case SPIRType::Float:
4652+
return join("vec", type.vecsize);
4653+
case SPIRType::Double:
4654+
return join("dvec", type.vecsize);
4655+
case SPIRType::Int64:
4656+
return join("i64vec", type.vecsize);
4657+
case SPIRType::UInt64:
4658+
return join("u64vec", type.vecsize);
4659+
default:
4660+
return "???";
4661+
}
4662+
}
4663+
else if (type.vecsize == type.columns) // Simple Matrix builtin
4664+
{
4665+
switch (type.basetype)
4666+
{
4667+
case SPIRType::Boolean:
4668+
return join("bmat", type.vecsize);
4669+
case SPIRType::Int:
4670+
return join("imat", type.vecsize);
4671+
case SPIRType::UInt:
4672+
return join("umat", type.vecsize);
4673+
case SPIRType::Half:
4674+
return join("f16mat", type.vecsize);
4675+
case SPIRType::Float:
4676+
return join("mat", type.vecsize);
4677+
case SPIRType::Double:
4678+
return join("dmat", type.vecsize);
4679+
// Matrix types not supported for int64/uint64.
4680+
default:
4681+
return "???";
4682+
}
4683+
}
4684+
else
4685+
{
4686+
switch (type.basetype)
4687+
{
4688+
case SPIRType::Boolean:
4689+
return join("bmat", type.columns, "x", type.vecsize);
4690+
case SPIRType::Int:
4691+
return join("imat", type.columns, "x", type.vecsize);
4692+
case SPIRType::UInt:
4693+
return join("umat", type.columns, "x", type.vecsize);
4694+
case SPIRType::Half:
4695+
return join("f16mat", type.columns, "x", type.vecsize);
4696+
case SPIRType::Float:
4697+
return join("mat", type.columns, "x", type.vecsize);
4698+
case SPIRType::Double:
4699+
return join("dmat", type.columns, "x", type.vecsize);
4700+
// Matrix types not supported for int64/uint64.
4701+
default:
4702+
return "???";
4703+
}
4704+
}
4705+
}
4706+
4707+
string Compiler::image_type_to_string(const SPIRType &type)
4708+
{
4709+
auto &imagetype = get<SPIRType>(type.image.type);
4710+
string res;
4711+
4712+
switch (imagetype.basetype)
4713+
{
4714+
case SPIRType::Int:
4715+
res = "i";
4716+
break;
4717+
case SPIRType::UInt:
4718+
res = "u";
4719+
break;
4720+
default:
4721+
break;
4722+
}
4723+
4724+
if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData)
4725+
return res + "subpassInput" + (type.image.ms ? "MS" : "");
4726+
else
4727+
res += "sampler";
4728+
4729+
switch (type.image.dim)
4730+
{
4731+
case Dim1D:
4732+
res += "1D";
4733+
break;
4734+
case Dim2D:
4735+
res += "2D";
4736+
break;
4737+
case Dim3D:
4738+
res += "3D";
4739+
break;
4740+
case DimCube:
4741+
res += "Cube";
4742+
break;
4743+
4744+
case DimBuffer:
4745+
res += "Buffer";
4746+
break;
4747+
4748+
case DimSubpassData:
4749+
res += "2D";
4750+
break;
4751+
default:
4752+
SPIRV_CROSS_THROW("Only 1D, 2D, 3D, Buffer, InputTarget and Cube textures supported.");
4753+
}
4754+
4755+
if (type.image.ms)
4756+
res += "MS";
4757+
4758+
if (type.image.arrayed)
4759+
{
4760+
res += "Array";
4761+
}
4762+
4763+
// "Shadow" state in GLSL only exists for samplers and combined image samplers.
4764+
if (((type.basetype == SPIRType::SampledImage) || (type.basetype == SPIRType::Sampler)) && type.image.depth)
4765+
res += "Shadow";
4766+
4767+
return res;
4768+
}
4769+
4770+
const char *Compiler::format_to_string(spv::ImageFormat format)
4771+
{
4772+
switch (format)
4773+
{
4774+
case ImageFormatRgba32f:
4775+
return "rgba32f";
4776+
case ImageFormatRgba16f:
4777+
return "rgba16f";
4778+
case ImageFormatR32f:
4779+
return "r32f";
4780+
case ImageFormatRgba8:
4781+
return "rgba8";
4782+
case ImageFormatRgba8Snorm:
4783+
return "rgba8_snorm";
4784+
case ImageFormatRg32f:
4785+
return "rg32f";
4786+
case ImageFormatRg16f:
4787+
return "rg16f";
4788+
4789+
case ImageFormatRgba32i:
4790+
return "rgba32i";
4791+
case ImageFormatRgba16i:
4792+
return "rgba16i";
4793+
case ImageFormatR32i:
4794+
return "r32i";
4795+
case ImageFormatRgba8i:
4796+
return "rgba8i";
4797+
case ImageFormatRg32i:
4798+
return "rg32i";
4799+
case ImageFormatRg16i:
4800+
return "rg16i";
4801+
4802+
case ImageFormatRgba32ui:
4803+
return "rgba32ui";
4804+
case ImageFormatRgba16ui:
4805+
return "rgba16ui";
4806+
case ImageFormatR32ui:
4807+
return "r32ui";
4808+
case ImageFormatRgba8ui:
4809+
return "rgba8ui";
4810+
case ImageFormatRg32ui:
4811+
return "rg32ui";
4812+
case ImageFormatRg16ui:
4813+
return "rg16ui";
4814+
case ImageFormatR11fG11fB10f:
4815+
return "r11f_g11f_b10f";
4816+
case ImageFormatR16f:
4817+
return "r16f";
4818+
case ImageFormatRgb10A2:
4819+
return "rgb10_a2";
4820+
case ImageFormatR8:
4821+
return "r8";
4822+
case ImageFormatRg8:
4823+
return "rg8";
4824+
case ImageFormatR16:
4825+
return "r16";
4826+
case ImageFormatRg16:
4827+
return "rg16";
4828+
case ImageFormatRgba16:
4829+
return "rgba16";
4830+
case ImageFormatR16Snorm:
4831+
return "r16_snorm";
4832+
case ImageFormatRg16Snorm:
4833+
return "rg16_snorm";
4834+
case ImageFormatRgba16Snorm:
4835+
return "rgba16_snorm";
4836+
case ImageFormatR8Snorm:
4837+
return "r8_snorm";
4838+
case ImageFormatRg8Snorm:
4839+
return "rg8_snorm";
4840+
case ImageFormatR8ui:
4841+
return "r8ui";
4842+
case ImageFormatRg8ui:
4843+
return "rg8ui";
4844+
case ImageFormatR16ui:
4845+
return "r16ui";
4846+
case ImageFormatRgb10a2ui:
4847+
return "rgb10_a2ui";
4848+
case ImageFormatR8i:
4849+
return "r8i";
4850+
case ImageFormatRg8i:
4851+
return "rg8i";
4852+
case ImageFormatR16i:
4853+
return "r16i";
4854+
default:
4855+
case ImageFormatUnknown:
4856+
return nullptr;
4857+
}
4858+
}

spirv_cross.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ class Compiler
489489
// The most common use here is to check if a buffer is readonly or writeonly.
490490
Bitset get_buffer_block_flags(uint32_t id) const;
491491

492+
std::string type_to_string(const SPIRType &type);
493+
std::string image_type_to_string(const SPIRType &type);
494+
static const char *format_to_string(spv::ImageFormat format);
495+
492496
protected:
493497
const uint32_t *stream(const Instruction &instr) const
494498
{
@@ -856,6 +860,8 @@ class Compiler
856860
bool instruction_to_result_type(uint32_t &result_type, uint32_t &result_id, spv::Op op, const uint32_t *args,
857861
uint32_t length);
858862

863+
Bitset combined_decoration_for_member(const SPIRType &type, uint32_t index) const;
864+
859865
private:
860866
// Used only to implement the old deprecated get_entry_point() interface.
861867
const SPIREntryPoint &get_first_entry_point(const std::string &name) const;

0 commit comments

Comments
 (0)