<p>They get help from the compiler. The line <code>#extension GL_EXT_debug_printf : enable</code> tells the compiler to look for and process debugPrintf statements inside the shader it then parses them out, modifies them, adds its own instructions, creates buffers... Basically they can do whatever they need to make it work.</p>
<p>You could forward just the variables but that would be pretty limited. And , unfortunately the printf would end up being very generic.</p>
<p>Another choice would be to write a macro that expands to a call to the printf with a bool wrapper around it. Basically the macro would expand to the if statement that is need to gate the call without using a function call, and have the printf inside of its body. I haven't tried this in GLSL myself but it seems very doable. GLSL's macro language is pretty powerful. I've seen some crazy long macro's put together with long code segments passed as strings.</p>
<p>Updated the code to the version that worked from the comment:</p>
<pre><code>#ifdef DEBUG 
#define printf( condition, message, value ) if( condition ){ \
       debugPrintfEXT( message, value ); \
     } 
#define printfDbg(message, value) if(isCenter() &amp;&amp; pSettings.PrintDebugEnabled){ \
     debugPrintfEXT(message, value);\
  } 
#else 
#define printf( condition, message, value ) 
#define printfDbg(message, value) 
#endif

...

printf( x==40.0 &amp;&amp; y==20.0, &quot;at 20,40 color is %1fv4.&quot;, color );
</code></pre>
<p>Unfortunately glsl doesn't support variadic macros so this has to be specific to 1 value or 2 values ect.</p>
<p>Maybe a macro for 1,2,3 variable outputs like printf1, printf2, and printf3.</p>
<p>Also the glsl language version can effect macro behavior but since the code is using debugprintf then it probably won't be an issue.</p>
<p>Finally you may want to just preprocess this through the compiler a few times until you get the syntax just right.</p>

CC BY-SA 4.0
2023-09-16T07:36:02.617Z
Not quite as nice with the lack of variadic macros, but a lot better than retyping the condition every time.

Just for completeness, this is the syntax that got it working for me (with `isCenter() and `pSettings` coming from my program, of course:

```#ifdef DEBUG
#define printf( condition, message, value ) if( condition ){ \
    debugPrintfEXT( message, value ); \
 }
#define printfDbg(message, value) if(isCenter() && pSettings.PrintDebugEnabled){ \
 debugPrintfEXT(message, value);\
}
#else
#define printf( condition, message, value )
#define printfDbg(message, value)
#endif```
Tare
CC BY-SA 4.0
2023-09-16T00:06:23.217Z
2023-09-16T11:53:51.607Z
2023-09-16T11:53:51.607Z
pmw1234
<p>Glsl does not support string / char data types, but obviously <code>debugPrintfEXT</code> works and takes some form of string. How are these handled in glsl?</p>
<p>Ideally I would like to forward the input, such that I can very easily limit when <code>debugPrintfEXT</code> is called, something like this</p>
<pre><code>layout(pushconstant) uniform DebugSettings
{
    bool Enabled;
}

debugPrintLimited(string s)
{
    bool isCenter = gl_LaunchIDEXT.x == gl_LaunchSizeEXT.x / 2
        &amp;&amp; gl_LaunchIDEXT.y == gl_LaunchSizeEXT.y / 2
    if(DebugSettings.Enabled &amp;&amp; isCenter)
    {
        debugPrintfEXT(s);
    }
}
</code></pre>
<p>Obviously the string here is not correct, but glsl has to handle the strings put into <code>debugPrintfEXT</code> somehow, so I would like to forward whatever they use, if at all possible.</p>

<glsl><vulkan>
How are strings for debugPrintfEXT handled in glsl?
Tare
# How are strings for debugPrintfEXT handled in glsl?

(Asked by: username_0 on 2023-09-14)

Glsl does not support string / char data types, but obviously `debugPrintfEXT` works and takes some form of string. How are these handled in glsl?

Ideally I would like to forward the input, such that I can very easily limit when `debugPrintfEXT` is called, something like this

```
layout(pushconstant) uniform DebugSettings
{
    bool Enabled;
}

debugPrintLimited(string s)
{
    bool isCenter = gl_LaunchIDEXT.x == gl_LaunchSizeEXT.x / 2
        && gl_LaunchIDEXT.y == gl_LaunchSizeEXT.y / 2
    if(DebugSettings.Enabled && isCenter)
    {
        debugPrintfEXT(s);
    }
}

```

Obviously the string here is not correct, but glsl has to handle the strings put into `debugPrintfEXT` somehow, so I would like to forward whatever they use, if at all possible.

**Tags:** glsl, vulkan


---

## 1 answers

### Answer by username_1 on 2023-09-16. Score: 1 | Accepted answer

They get help from the compiler. The line `#extension GL_EXT_debug_printf : enable` tells the compiler to look for and process debugPrintf statements inside the shader it then parses them out, modifies them, adds its own instructions, creates buffers... Basically they can do whatever they need to make it work.

You could forward just the variables but that would be pretty limited. And , unfortunately the printf would end up being very generic.

Another choice would be to write a macro that expands to a call to the printf with a bool wrapper around it. Basically the macro would expand to the if statement that is need to gate the call without using a function call, and have the printf inside of its body. I haven't tried this in GLSL myself but it seems very doable. GLSL's macro language is pretty powerful. I've seen some crazy long macro's put together with long code segments passed as strings.

Updated the code to the version that worked from the comment:

```
#ifdef DEBUG 
#define printf( condition, message, value ) if( condition ){ \
       debugPrintfEXT( message, value ); \
     } 
#define printfDbg(message, value) if(isCenter() && pSettings.PrintDebugEnabled){ \
     debugPrintfEXT(message, value);\
  } 
#else 
#define printf( condition, message, value ) 
#define printfDbg(message, value) 
#endif

...

printf( x==40.0 && y==20.0, "at 20,40 color is %1fv4.", color );

```

Unfortunately glsl doesn't support variadic macros so this has to be specific to 1 value or 2 values ect.

Maybe a macro for 1,2,3 variable outputs like printf1, printf2, and printf3.

Also the glsl language version can effect macro behavior but since the code is using debugprintf then it probably won't be an issue.

Finally you may want to just preprocess this through the compiler a few times until you get the syntax just right.

> **Comments:**
> **username_0**: Not quite as nice with the lack of variadic macros, but a lot better than retyping the condition every time.  Just for completeness, this is the syntax that got it working for me (with `isCenter() and `pSettings` coming from my program, of course:  ```#ifdef DEBUG #define printf( condition, message, value ) if( condition ){ \     debugPrintfEXT( message, value ); \  } #define printfDbg(message, value) if(isCenter() && pSettings.PrintDebugEnabled){ \  debugPrintfEXT(message, value);\ } #else #define printf( condition, message, value ) #define printfDbg(message, value) #endif```  

---

