openframeworks / openframeworks/openFrameworks

Versioned shader doesn't work when setup from source

Open
#6,235 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

I posted this on the forum, but I now think it's a bug, so I'm filing here.
https://forum.openframeworks.cc/t/shader-setupshaderfromsource-not-working/29432/4

If I load my #versioned 150 shaders from file, it works fine. If I setup from source via stringify or R", it compiles & links fine, but I see nothing. I dump the strings to the console to compare, and they look fine. I couldn't track down the issue. (Actually it seems to be displaying something, maybe it's related to the default uniforms like modelViewProjectionMatrix etc not being passed correctly or something?)

I did try:

  1. manually adding shader.setUniformMatrix4f("modelViewProjectionMatrix", ofGetCurrentMatrix(OF_MATRIX_PROJECTION) * ofGetCurrentMatrix(OF_MATRIX_MODELVIEW));
    but this made no difference at all to either shader.
  2. renaming the uniform modelViewProjectionMatrix to mvp and setting it manually to ofGetCurrentMatrix(OF_MATRIX_PROJECTION) * ofGetCurrentMatrix(OF_MATRIX_MODELVIEW)). In this case shader0 (loaded from file) still showed the correct content, but was offset by half (excluding the plane's position. is this a bug in core or user error on my side?) However shader1 is still not showing anything.

This is openFrameworks 0.10.1 on ubuntu 18.04.1 with Qt Creator 4.6.2

// vert shader
#version 150

uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec2 texcoord;
out vec2 varyingtexcoord;

void main(){
    varyingtexcoord = texcoord;
    gl_Position = modelViewProjectionMatrix * position;
}
// frag shader
#version 150

uniform sampler2DRect tex0;
in vec2 varyingtexcoord;
out vec4 outputColor;

void main()
{
    outputColor = texture(tex0, varyingtexcoord);
}
// main.cpp
#include "ofMain.h"

#define STRINGIFY(A) string("#version 150\n")+#A

class ofApp : public ofBaseApp{
public:
    ofShader shader0;   // loaded from file <-- WORKS!
    ofShader shader1;   // setup from (identical) source <-- COMPILES, BUT NO OUTPUT!
    ofVideoGrabber cam;
    ofPlanePrimitive quad;

    //--------------------------------------------------------------
    // vertex shader source
#ifdef STRINGIFY
    string vertSource = STRINGIFY(
            uniform mat4 modelViewProjectionMatrix;
            in vec4 position;
            in vec2 texcoord;
            out vec2 varyingtexcoord;

            void main(){
                varyingtexcoord = texcoord;
                gl_Position = modelViewProjectionMatrix * position;
            }
     );

    //--------------------------------------------------------------
    // fragment shader source
    string fragSource = STRINGIFY(
            uniform sampler2DRect tex0;
            in vec2 varyingtexcoord;
            out vec4 outputColor;

            void main()
            {
                outputColor = texture(tex0, varyingtexcoord);
            }

     );
#else
    string vertSource = R"(
            #version 150

            uniform mat4 modelViewProjectionMatrix;
            in vec4 position;
            in vec2 texcoord;
            out vec2 varyingtexcoord;

            void main(){
                varyingtexcoord = texcoord;
                gl_Position = modelViewProjectionMatrix * position;
            }

      )";

    //--------------------------------------------------------------
    // fragment shader source
    string fragSource = R"(
            #version 150

            uniform sampler2DRect tex0;
            in vec2 varyingtexcoord;
            out vec4 outputColor;

            void main()
            {
                outputColor = texture(tex0, varyingtexcoord);
            }
     )";
#endif


    //--------------------------------------------------------------
    void setup(){
        ofBackground(50);
        cam.setup(320, 240);

        quad.set(cam.getWidth(), -cam.getHeight(), 2, 2); // flip vertically
        quad.setPosition(cam.getWidth()/2, cam.getHeight()/2, 0);
        quad.mapTexCoords(1, 1, cam.getWidth(), cam.getHeight());

        // setup shader0 from files
        shader0.load("pass");

        // setup shader1 from source
        ofLogNotice() << "//=== vertSource \n" << vertSource;
        ofLogNotice() << "//=== fragSource \n" << fragSource;
        shader1.setupShaderFromSource(GL_VERTEX_SHADER, vertSource);
        shader1.setupShaderFromSource(GL_FRAGMENT_SHADER, fragSource);
        shader1.linkProgram();
    }

    //--------------------------------------------------------------
    void drawShader(ofShader& s) {
        s.begin();
        s.setUniformTexture("tex0", cam.getTexture(), 0);
        quad.draw();
        s.end();
    }

    //--------------------------------------------------------------
    void draw(){
        cam.update();

        drawShader(shader0);

        ofPushMatrix();
        ofTranslate(0, cam.getHeight());
        drawShader(shader1);
        ofPopMatrix();
    }
};

int main( ){
    ofGLFWWindowSettings settings;
    settings.setGLVersion(3, 2);
    settings.setSize(1024, 768);
    ofCreateWindow(settings);
    ofRunApp(new ofApp());
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the provided main.cpp and compare shader0, loaded with shader1, configured through setupShaderFromSource. Start by tracing the shader setup and uniform handling paths, then verify that the source-configured shader renders the same output as the file-loaded shader under the stated openFrameworks and Ubuntu setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.