GLuint CreateProgram(const char* vsKey, const char* fsKey) { static int first = 1; if (first) { glswInit(); glswAddPath("../", ".glsl"); glswAddPath("./", ".glsl"); char qualifiedPath[128]; strcpy(qualifiedPath, PezResourcePath()); strcat(qualifiedPath, "/"); glswAddPath(qualifiedPath, ".glsl"); first = 0; } const char* vsSource = glswGetShader(vsKey); const char* fsSource = glswGetShader(fsKey); const char* msg = "Can't find %s shader: '%s'.\n"; PezCheckCondition(vsSource != 0, msg, "vertex", vsKey); PezCheckCondition(fsSource != 0, msg, "fragment", fsKey); GLint compileSuccess; GLchar compilerSpew[256]; GLuint vsHandle = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vsHandle, 1, &vsSource, 0); glCompileShader(vsHandle); glGetShaderiv(vsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(vsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, "Can't compile %s:\n%s", vsKey, compilerSpew); GLuint fsHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fsHandle, 1, &fsSource, 0); glCompileShader(fsHandle); glGetShaderiv(fsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(fsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, "Can't compile %s:\n%s", fsKey, compilerSpew); GLuint programHandle = glCreateProgram(); glAttachShader(programHandle, vsHandle); glAttachShader(programHandle, fsHandle); glLinkProgram(programHandle); GLint linkSuccess; glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); glGetProgramInfoLog(programHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(linkSuccess, "Can't link %s with %s:\n%s", vsKey, fsKey, compilerSpew); return programHandle; }
static void LoadEffect() { const char* vsSource, * fsSource; GLuint vsHandle, fsHandle; GLint compileSuccess, linkSuccess; GLchar compilerSpew[256]; GLuint programHandle; glswInit(); glswSetPath("../demo/", ".glsl"); glswAddDirectiveToken("GL3", "#version 130"); vsSource = glswGetShader("Simple.Vertex.Textured." PEZ_GL_VERSION_TOKEN); fsSource = glswGetShader("Simple.Fragment.Textured." PEZ_GL_VERSION_TOKEN); PezCheckCondition(vsSource, "Can't find vertex shader.\n"); PezCheckCondition(fsSource, "Can't find fragment shader.\n"); vsHandle = glCreateShader(GL_VERTEX_SHADER); fsHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vsHandle, 1, &vsSource, 0); glCompileShader(vsHandle); glGetShaderiv(vsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(vsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, compilerSpew); glShaderSource(fsHandle, 1, &fsSource, 0); glCompileShader(fsHandle); glGetShaderiv(fsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(fsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, compilerSpew); programHandle = glCreateProgram(); glAttachShader(programHandle, vsHandle); glAttachShader(programHandle, fsHandle); glBindAttribLocation(programHandle, PositionSlot, "Position"); glBindAttribLocation(programHandle, TexCoordSlot, "InCoord"); glLinkProgram(programHandle); glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); glGetProgramInfoLog(programHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(linkSuccess, compilerSpew); glUseProgram(programHandle); }
static GLuint BuildProgram(const char* vsKey, const char* fsKey) { DebugString("Compiling '%s'...\n", vsKey); const char* vsString = glswGetShader(vsKey); if (!vsString) { FatalError("%s\n", glswGetError()); } GLuint vsHandle = BuildShader(vsString, GL_VERTEX_SHADER); DebugString("Compiling '%s'...\n", fsKey); const char* fsString = glswGetShader(fsKey); if (!fsString) { FatalError("%s\n", glswGetError()); } GLuint fsHandle = BuildShader(fsString, GL_FRAGMENT_SHADER); DebugString("Linking...\n"); GLuint programHandle = glCreateProgram(); glAttachShader(programHandle, vsHandle); glAttachShader(programHandle, fsHandle); glBindAttribLocation(programHandle, VertexAttributes::Position, "Position"); glBindAttribLocation(programHandle, VertexAttributes::Normal, "Normal"); glLinkProgram(programHandle); GLint linkSuccess; glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); if (linkSuccess == GL_FALSE) { GLchar messages[256]; glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]); FatalError(messages); } else { GLchar messages[256]; glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]); DebugString(messages); } return programHandle; }
void test(const char* shaderKey) { const char* shaderSource = glswGetShader(shaderKey); if (!bEnableOutput) return; if (!shaderSource) { printf("Error with %s: %s\n\n\n", shaderKey, glswGetError()); } else { printf("------------------%s:\n%s\n\n\n", shaderKey, shaderSource); } }
const char* glswGetShaders(const char* combinedKeys) { glswContext* gc = __glsw__Context; int tokenIndex=0; struct bstrList* tokens; bstring effectKeys; glswList* pCombinedEffect; effectKeys = bfromcstr(combinedKeys); tokens = bsplit(effectKeys, '+'); pCombinedEffect = gc->CombinedEffects; while (pCombinedEffect) { if (1 == biseq(pCombinedEffect->Key, effectKeys)) break; pCombinedEffect = pCombinedEffect->Next; } if(!pCombinedEffect) { glswList* temp = gc->CombinedEffects; gc->CombinedEffects = (glswList*) calloc(sizeof(glswList), 1); if(gc->CombinedEffects) { gc->CombinedEffects->Key = bstrcpy(effectKeys); gc->CombinedEffects->Value = bfromcstr(""); gc->CombinedEffects->Next = temp; for (tokenIndex = 0; tokenIndex < tokens->qty; tokenIndex++) { bstring token = tokens->entry[tokenIndex]; bconcat(gc->CombinedEffects->Value, bfromcstr(glswGetShader(token->data))); //bdestroy(token); } bstrListDestroy(tokens); bdestroy(effectKeys); return (char*)gc->CombinedEffects->Value->data; } else return 0; } else return (char*)pCombinedEffect->Value->data; }
void fulltest() { starttest(); partialtest(); randomtest(); glswShutdown(); { const char* shaderSource = glswGetShader("SignedEuclidean.Vertex.GL3.Erosion"); if (bEnableOutput) { if (shaderSource) printf("Bad; should've reported an error!"); else printf("Good, got another expected error: : %s\n\n\n", glswGetError()); } } }
unsigned int glswGetShadersAlt(const char* combinedKeys, const char** pArray, unsigned int maxItems) { glswContext* gc = __glsw__Context; int tokenIndex=0; struct bstrList* tokens; bstring effectKeys; int idx = 0; effectKeys = bfromcstr(combinedKeys); tokens = bsplit(effectKeys, '+'); for (tokenIndex = 0; tokenIndex < tokens->qty; tokenIndex++) { bstring token = tokens->entry[tokenIndex]; pArray[idx++] = glswGetShader(token->data); //bdestroy(token); } bdestroy(effectKeys); bstrListDestroy(tokens); return idx; }
GLuint CreateProgram(const char* vsKey, const char* gsKey, const char* fsKey) { static int first = 1; if (first) { glswInit(); glswAddPath("../", ".glsl"); glswAddPath("./", ".glsl"); char qualifiedPath[128]; strcpy(qualifiedPath, PezResourcePath()); strcat(qualifiedPath, "/"); glswAddPath(qualifiedPath, ".glsl"); glswAddDirective("*", "#version 150"); first = 0; } const char* vsSource = glswGetShader(vsKey); const char* gsSource = glswGetShader(gsKey); const char* fsSource = glswGetShader(fsKey); const char* msg = "Can't find %s shader: '%s'.\n"; PezCheckCondition(vsSource != 0, msg, "vertex", vsKey); PezCheckCondition(gsKey == 0 || gsSource != 0, msg, "geometry", gsKey); PezCheckCondition(fsKey == 0 || fsSource != 0, msg, "fragment", fsKey); GLint compileSuccess; GLchar compilerSpew[256]; GLuint programHandle = glCreateProgram(); GLuint vsHandle = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vsHandle, 1, &vsSource, 0); glCompileShader(vsHandle); glGetShaderiv(vsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(vsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, "Can't compile %s:\n%s", vsKey, compilerSpew); glAttachShader(programHandle, vsHandle); GLuint gsHandle; if (gsKey) { gsHandle = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(gsHandle, 1, &gsSource, 0); glCompileShader(gsHandle); glGetShaderiv(gsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(gsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, "Can't compile %s:\n%s", gsKey, compilerSpew); glAttachShader(programHandle, gsHandle); } GLuint fsHandle; if (fsKey) { fsHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fsHandle, 1, &fsSource, 0); glCompileShader(fsHandle); glGetShaderiv(fsHandle, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(fsHandle, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition(compileSuccess, "Can't compile %s:\n%s", fsKey, compilerSpew); glAttachShader(programHandle, fsHandle); } glLinkProgram(programHandle); GLint linkSuccess; glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); glGetProgramInfoLog(programHandle, sizeof(compilerSpew), 0, compilerSpew); if (!linkSuccess) { PezDebugString("Link error.\n"); if (vsKey) PezDebugString("Vertex Shader: %s\n", vsKey); if (gsKey) PezDebugString("Geometry Shader: %s\n", gsKey); if (fsKey) PezDebugString("Fragment Shader: %s\n", fsKey); PezDebugString("%s\n", compilerSpew); } return programHandle; }
void setup() { // 3. Define and compile vertex and fragment shaders GLuint vs; GLuint fs; GLint compileSuccess, linkSuccess; GLchar compilerSpew[256]; glswInit(); glswSetPath("../shader/", ".glsl"); glswAddDirectiveToken("GL3", "#version 150"); const char *vss = glswGetShader("test.Vertex.GL3"); const char *fss = glswGetShader("test.Fragment.GL3"); PezCheckCondition((void *)vss, "Can't find vertex shader.\n"); PezCheckCondition((void *)fss, "Can't find fragment shader.\n"); vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vss, NULL); glCompileShader(vs); glGetShaderiv(vs, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(vs, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition((void*)compileSuccess, compilerSpew); fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fss, NULL); glCompileShader(fs); glGetShaderiv(fs, GL_COMPILE_STATUS, &compileSuccess); glGetShaderInfoLog(fs, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition((void*)compileSuccess, compilerSpew); shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vs); glAttachShader(shaderProgram, fs); glBindFragDataLocation(shaderProgram, 0, "fragColour"); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkSuccess); glGetProgramInfoLog(shaderProgram, sizeof(compilerSpew), 0, compilerSpew); PezCheckCondition((void*)linkSuccess, compilerSpew); positionUniform = glGetUniformLocation(shaderProgram, "p"); colourAttribute = glGetAttribLocation(shaderProgram, "colour"); positionAttribute = glGetAttribLocation(shaderProgram, "position"); glDeleteShader(vs); glDeleteShader(fs); GLfloat vertexData[]= { -0.5,-0.5,0.0,1.0, 1.0,0.0,0.0,1.0, -0.5, 0.5,0.0,1.0, 0.0,1.0,0.0,1.0, 0.5, 0.5,0.0,1.0, 0.0,0.0,1.0,1.0, 0.5,-0.5,0.0,1.0, 1.0,1.0,1.0,1.0}; glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, 4*8*sizeof(GLfloat), vertexData, GL_STATIC_DRAW); glEnableVertexAttribArray((GLuint)positionAttribute); glEnableVertexAttribArray((GLuint)colourAttribute ); glVertexAttribPointer((GLuint)positionAttribute, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0); glVertexAttribPointer((GLuint)colourAttribute , 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (char*)0+4*sizeof(GLfloat)); }