Simple printf functionality for GLSL.

Overview
Simple printf functionality for GLSL.

This library is a simple proof of concept of using printf directly from a shader. The main point of being able to do this is to ease the debugging of complicated shader programs. This is invaluable for example when it's essential to see the evolution of the value of a variable in a loop and simply outputting the final value will not give enough information of how the program works.

Currently the library is a single header file, shaderprintf.h. This file contains a function called glShaderSourcePrint that simply acts as a replacement for glShaderSource for any shaders where the printing functionality is desired. It also contains a final parser called getPrintBufferString that returns all of the printed content as a C++ std::string. In addition to these two it contains helpers to create, bind and delete a necessary temporary buffer that should be somewhat simple to understand by looking at the example main loop in main.cpp.

Note that on the GLSL side you need to call enablePrintf() before calling printf. This is simply because most of the time you'll have thousands of threads and no interest in seeing the values for all of them; instead you most often want to enable the prints for a certain small-ish subset (like a region on the screen or certain problematic vertices) so you don't need to do if("I should be writing") printf(...) all the time. There's also a corresponding disablePrintf() function -- these just set a hidden boolean variable to true or false and each printf tests against it.

Also note that GLSL doesn't support characters, strings or pointers so %c, %s and %p are out. As is %n since there's no dynamic memory handling anyway. As a consequence, this implementation of printf -- in contrast to C printf -- returns void.

Due to the native vector types of GLSL, the format specifier definition of printf is enhanced to contain dimensionality and is as follows : %[flags][width][.precision][length][^dimensions]specifier. dimensions is a number between 1 and 4. All elements will be printed with the same format specifier (which matches the standard if we omit the [^dimensions] part), surrounded by parentheses and separated with commas and spaces. They're written in the order xyzw and a subset can be printed (i.e. printf("%.1^3f", vec4(1.0)); is completely legal and will print "(1.0, 1.0, 1.0)")

The example program should work directly from the repository with Visual Studio, select shader-printf as the startup project and run. Everything here is written in VS2017 but at least VS2015 should be fine as well. OpenGL version 4.3 (or the shader storage buffer object extension) is required.

The repository also comes with a minimal OpenGL extension handler/windowing system that's aimed at targeting a single core version with all of the nice debug features that modern OpenGL comes with. It's probably not much of use as is but it's a nice reference and you might find features you didn't know about.
Comments
  • -Wshadow gcc warning

    -Wshadow gcc warning

    https://github.com/msqrt/shader-printf/blob/e74046eccd7dfb7a2cdb870cc712d3cdfe3854ab/shader-printf/shaderprintf.h#L225 this variable shadows this declaration: https://github.com/msqrt/shader-printf/blob/e74046eccd7dfb7a2cdb870cc712d3cdfe3854ab/shader-printf/shaderprintf.h#L185

    And thus issues a warning when the gcc compiler option -Wshadow is enabled:

    shader-printf/shader-printf/shaderprintf.h:246:8: warning: declaration of ‘inString’ shadows a previous local [-Wshadow]
    

    A simple fix would be to insert curly braces {} around line 183 to 196.

    opened by tsoj 1
  • `max()` function not declared

    `max()` function not declared

    https://github.com/msqrt/shader-printf/blob/e74046eccd7dfb7a2cdb870cc712d3cdfe3854ab/shader-printf/shaderprintf.h#L201 On my Linux machine I get this error:

    shader-printf/shader-printf/shaderprintf.h:219:48: error: ‘max’ was not declared in this scope
      219 |  if (extension != std::string::npos) version = max(extension, version);
    

    A simple fix would be to use std::max() instead.

    opened by tsoj 1
  • unwanted line offset

    unwanted line offset

    https://github.com/msqrt/shader-printf/blob/e74046eccd7dfb7a2cdb870cc712d3cdfe3854ab/shader-printf/shaderprintf.h#L212 I think here the bufferInsertOffset should be after the '\n'. Then compiler errors work in my environment.

    opened by tsoj 1
  • License?

    License?

    This project is awesome, but I'm unsure if I'm allowed to use its code or not in my own projects because there's no apparent license on it.

    If you'd like others to use this library, could you clarify its license? A LICENSE or COPYING file in the repository and/or a copyright + license inline in the shaderprintf.h header would do :-)

    opened by tomjakubowski 1
  • glGetProgramResourceiv bufSize should not be in bytes.

    glGetProgramResourceiv bufSize should not be in bytes.

    On line 52: https://github.com/msqrt/shader-printf/blob/e7b25ffb6e5557b48a1010c0a829e51d56903ad6/shader-printf/shaderprintf.h#L52

    	glGetProgramResourceiv(program, GL_SHADER_STORAGE_BLOCK, glGetProgramResourceIndex(program, GL_SHADER_STORAGE_BLOCK, "printfBuffer"), 1, &prop, sizeof(binding), nullptr, &binding);
    
    

    sizeof(binding) is wrong, it should just be 1.

    See https://www.khronos.org/opengl/wiki/Example_Code#Program_introspection for proper example usage. See https://github.com/KhronosGroup/OpenGL-API/issues/52 about this confusing naming.

    opened by realazthat 0
Owner
simple dude doing simple things
null
LLVM IR and optimizer for shaders, including front-end adapters for GLSL and SPIR-V and back-end adapter for GLSL

Licensing LunarGLASS is available via a three clause BSD-style open source license. Goals The primary goals of the LunarGLASS project are: Reduce the

LunarG, Inc. 153 Dec 8, 2022
⚔️ A tool for cross compiling shaders. Convert between GLSL, HLSL, Metal Shader Language, or older versions of GLSL.

A cross compiler for shader languages. Convert between SPIR-V, GLSL / GLSL ES, HLSL, Metal Shader Language, or older versions of a given language. Cross Shader wraps glslang and SPIRV-Cross, exposing a simpler interface to transpile shaders.

Alain Galvan 207 Dec 30, 2022
GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.

GLSL optimizer ⚠️ As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a different shader compilati

Aras Pranckevičius 1.6k Jan 3, 2023
this is my simple voxel engine, appart from librairies like glad it is entierly written in C++ and GLSL

simple-voxel-raycaster this is my simple voxel engine, appart from librairies like glad it is entierly written in C++ and GLSL here is a gif: https://

null 2 Sep 26, 2022
🍦 Never use cout/printf to debug again

IceCream-Cpp IceCream-Cpp is a little (single header) library to help with the print debugging on C++11 and forward. Try it at godbolt! Contents Insta

Renato Garcia 423 Jan 4, 2023
mimic of libc’s printf function (42 Project, mandatory part only)

42printf mimic of libc’s printf function (42 Project, mandatory part only) This hasn't been tested yet by moulinette and is not recommended to take as

null 1 Oct 26, 2021
Type-safe Printf in C

Type-Safe Printf For C This uses macro magic, compound literals, and _Generic to take printf() to the next level: type-safe printing, printing into co

Henrik Theiling 63 Dec 18, 2022
printf() implementation in C

ft_printf This project was made in accordance with the project of School 21 (Ecole 42). Preamble · Description · Examples · Installation and usage · T

null 4 Oct 4, 2022
glsl code blocks for org-mode

GLSL code blocks for Emacs Org-mode This org-mode extension adds the capability to run GLSL code blocks directly from inside Emacs and immediately dis

null 31 Dec 1, 2022
VSIX Project that provides GLSL language integration.

GLSL language integration (for VS2017, 2019 and 2022) Download this extension from Visual Studio Marketplace version VS2017 & 2019 or VS 2022 preview

Daniel Scherzer 208 Dec 24, 2022
HLSL Parser and Translator for HLSL, GLSL, and MSL.

HLSLParser This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (usin

null 3 Jul 2, 2022
HLSL to GLSL language translator based on ATI's HLSL2GLSL. Used in Unity.

HLSL to GLSL shader language translator ⚠️ As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a d

Aras Pranckevičius 522 Dec 18, 2022
HLSL Parser and Translator for HLSL, GLSL, and MSL.

HLSLParser This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (usin

null 315 Dec 25, 2022
Minify and obfuscate GLSL or HLSL code

Shader Minifier Shader Minifier is a tool that minifies and obfuscates shader code (GLSL and HLSL). Its original use-case is for the demoscene, for op

Laurent Le Brun 251 Jan 2, 2023
Shader cross compiler to translate HLSL (Shader Model 4 and 5) to GLSL

XShaderCompiler ("Cross Shader Compiler") Features Cross compiles HLSL shader code (Shader Model 4 and 5) into GLSL Simple to integrate into other pro

Lukas Hermanns 345 Dec 9, 2022
An efficient texture-free GLSL procedural noise library

Wombat An efficient texture-free GLSL procedural noise library Source: https://github.com/BrianSharpe/Wombat Derived from: https://github.com/BrianSha

Brian Sharpe 200 Dec 18, 2022
Fast glsl deNoise spatial filter, with circular gaussian kernel, full configurable

glslSmartDeNoise Fast glsl spatial deNoise filter, with circular gaussian kernel and smart/flexible/adaptable -> full configurable: Standard Deviation

Michele Morrone 212 Dec 24, 2022
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.

News Visual Studio 2013 is no longer supported As scheduled, Microsoft Visual Studio 2013 is no longer officially supported. Please upgrade to at leas

The Khronos Group 2.4k Jan 9, 2023
Not related to software bugs and exploits; this repo contains snippets of code that demonstrate some interesting functionality or a handy trick.

Proof-of-Concept Not related to software bugs and exploits; this repo contains snippets of code that demonstrate some interesting functionality or a h

Alisa Esage 32 Nov 19, 2022