google / shaderc

A collection of tools, libraries, and tests for Vulkan shader compilation.
Other
1.85k stars 362 forks source link

BUG REPORT: Cannot use the ternary `?` operator in preprocessor macros. #1155

Open devshgraphicsprogramming opened 3 years ago

devshgraphicsprogramming commented 3 years ago

We're using the v2020.3 tag

I attach the file we're trying to feed into shaderc, it doesn't like our IRR_GLSL_MAX macro.

GCC with the -E option consumes it and produces the expected output, feel free to check with godbolt.org postprocessed.comp.txt

devshgraphicsprogramming commented 3 years ago

should I open this against glslang instead?

zoddicus commented 3 years ago

It is likely a glslang issue.

dneto0 commented 3 years ago

You don't say what the problem is. I couldn't reproduce an issue.

Here's an example compute shader I tried

#version 450

#define MAXISH(X,Y)((X)<(Y)? (Y):(X))

void main() {
  float f = MAXISH(12.0,1.0);
}

This is the output of glslc -E

#version 450

void main(){
  float f =((12.0)<(1.0)?(1.0):(12.0));
}

And that compiles into this SPIR-V:

; SPIR-V
; Version: 1.0
; Generator: Google Shaderc over Glslang; 10
; Bound: 10
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %main "main"
               OpExecutionMode %main LocalSize 1 1 1
               OpSource GLSL 450
               OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
               OpSourceExtension "GL_GOOGLE_include_directive"
               OpName %main "main"
               OpName %f "f"
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
%_ptr_Function_float = OpTypePointer Function %float
   %float_12 = OpConstant %float 12
       %main = OpFunction %void None %3
          %5 = OpLabel
          %f = OpVariable %_ptr_Function_float Function
               OpStore %f %float_12
               OpReturn
               OpFunctionEnd
devshgraphicsprogramming commented 3 years ago

You don't say what the problem is. I couldn't reproduce an issue.

Here's an example compute shader I tried

#version 450

#define MAXISH(X,Y)((X)<(Y)? (Y):(X))

void main() {
  float f = MAXISH(12.0,1.0);
}

This is the output of glslc -E

#version 450

void main(){
  float f =((12.0)<(1.0)?(1.0):(12.0));
}

And that compiles into this SPIR-V:

; SPIR-V
; Version: 1.0
; Generator: Google Shaderc over Glslang; 10
; Bound: 10
; Schema: 0
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %main "main"
               OpExecutionMode %main LocalSize 1 1 1
               OpSource GLSL 450
               OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
               OpSourceExtension "GL_GOOGLE_include_directive"
               OpName %main "main"
               OpName %f "f"
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
      %float = OpTypeFloat 32
%_ptr_Function_float = OpTypePointer Function %float
   %float_12 = OpConstant %float 12
       %main = OpFunction %void None %3
          %5 = OpLabel
          %f = OpVariable %_ptr_Function_float Function
               OpStore %f %float_12
               OpReturn
               OpFunctionEnd

it not about having the ? in a macro its about using its results in preprocessing

#define MAXISH(X,Y)((X)<(Y)? (Y):(X))

#if 5==MAXISH(1,5)
#define PASSED
#endif