Coder Social home page Coder Social logo

shader-printf's Introduction

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.

shader-printf's People

Contributors

msqrt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shader-printf's Issues

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 :-)

`max()` function not declared

if (extension != std::string::npos) version = max(extension, version);

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.

-Wshadow gcc warning

bool inString = false;

this variable shadows this declaration:
bool inString = false;

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.

glGetProgramResourceiv bufSize should not be in bytes.

On line 52:

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

	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 KhronosGroup/OpenGL-API#52 about this confusing naming.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.