librw/src/gl/gl3shader.cpp

270 lines
5.6 KiB
C++
Raw Normal View History

2020-04-30 17:54:38 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2020-05-14 00:07:34 +02:00
#include <assert.h>
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#include "rwgl3.h"
#include "rwgl3shader.h"
namespace rw {
namespace gl3 {
2020-05-13 20:46:27 +02:00
#ifdef RW_GLES2
#include "gl2_shaders/header_vs.inc"
#include "gl2_shaders/header_fs.inc"
2020-05-13 20:46:27 +02:00
#else
2020-04-26 19:33:43 +02:00
#include "shaders/header_vs.inc"
#include "shaders/header_fs.inc"
2020-05-13 20:46:27 +02:00
#endif
2020-04-26 19:33:43 +02:00
UniformRegistry uniformRegistry;
int32
registerUniform(const char *name)
{
int i;
i = findUniform(name);
if(i >= 0) return i;
// TODO: print error
2020-05-14 00:07:34 +02:00
if(uniformRegistry.numUniforms+1 >= MAX_UNIFORMS){
assert(0 && "no space for uniform");
return -1;
2020-05-14 00:07:34 +02:00
}
uniformRegistry.uniformNames[uniformRegistry.numUniforms] = strdup(name);
return uniformRegistry.numUniforms++;
}
int32
findUniform(const char *name)
{
int i;
for(i = 0; i < uniformRegistry.numUniforms; i++)
if(strcmp(name, uniformRegistry.uniformNames[i]) == 0)
return i;
return -1;
}
int32
registerBlock(const char *name)
{
int i;
i = findBlock(name);
if(i >= 0) return i;
// TODO: print error
if(uniformRegistry.numBlocks+1 >= MAX_BLOCKS)
return -1;
uniformRegistry.blockNames[uniformRegistry.numBlocks] = strdup(name);
return uniformRegistry.numBlocks++;
}
int32
findBlock(const char *name)
{
int i;
for(i = 0; i < uniformRegistry.numBlocks; i++)
if(strcmp(name, uniformRegistry.blockNames[i]) == 0)
return i;
return -1;
}
Shader *currentShader;
2020-05-13 20:46:27 +02:00
static void
printShaderSource(const char **src)
{
2020-07-22 13:56:03 +02:00
int f;
2020-05-13 20:46:27 +02:00
const char *file;
bool printline;
int line = 1;
for(f = 0; src[f]; f++){
char c;
file = src[f];
printline = true;
while(c = *file++, c != '\0'){
if(printline)
printf("%.4d: ", line++);
putchar(c);
printline = c == '\n';
}
putchar('\n');
}
}
static int
2020-04-26 19:33:43 +02:00
compileshader(GLenum type, const char **src, GLuint *shader)
{
2020-04-26 19:33:43 +02:00
GLint n;
GLint shdr, success;
GLint len;
char *log;
2020-04-26 19:33:43 +02:00
for(n = 0; src[n]; n++);
shdr = glCreateShader(type);
2020-04-26 19:33:43 +02:00
glShaderSource(shdr, n, src, nil);
glCompileShader(shdr);
glGetShaderiv(shdr, GL_COMPILE_STATUS, &success);
if(!success){
2020-05-13 20:46:27 +02:00
printShaderSource(src);
fprintf(stderr, "Error in %s shader\n",
type == GL_VERTEX_SHADER ? "vertex" : "fragment");
glGetShaderiv(shdr, GL_INFO_LOG_LENGTH, &len);
2017-08-24 15:10:34 +02:00
log = (char*)rwMalloc(len, MEMDUR_FUNCTION);
glGetShaderInfoLog(shdr, len, nil, log);
fprintf(stderr, "%s\n", log);
2017-08-24 15:10:34 +02:00
rwFree(log);
return 1;
}
*shader = shdr;
return 0;
}
static int
linkprogram(GLint vs, GLint fs, GLuint *program)
{
GLint prog, success;
GLint len;
char *log;
prog = glCreateProgram();
2020-05-13 20:46:27 +02:00
#ifdef RW_GLES2
// TODO: perhaps just do this always and get rid of the layout stuff?
glBindAttribLocation(prog, ATTRIB_POS, "in_pos");
glBindAttribLocation(prog, ATTRIB_NORMAL, "in_normal");
glBindAttribLocation(prog, ATTRIB_COLOR, "in_color");
glBindAttribLocation(prog, ATTRIB_TEXCOORDS0, "in_tex0");
glBindAttribLocation(prog, ATTRIB_WEIGHTS, "in_weights");
glBindAttribLocation(prog, ATTRIB_INDICES, "in_indices");
#endif
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &success);
if(!success){
fprintf(stderr, "Error in program\n");
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
2017-08-24 15:10:34 +02:00
log = (char*)rwMalloc(len, MEMDUR_FUNCTION);
glGetProgramInfoLog(prog, len, nil, log);
fprintf(stderr, "%s\n", log);
2017-08-24 15:10:34 +02:00
rwFree(log);
return 1;
}
*program = prog;
return 0;
}
2020-04-26 19:33:43 +02:00
Shader*
Shader::create(const char **vsrc, const char **fsrc)
{
GLuint vs, fs, program;
int i;
int fail;
fail = compileshader(GL_VERTEX_SHADER, vsrc, &vs);
if(fail)
return nil;
fail = compileshader(GL_FRAGMENT_SHADER, fsrc, &fs);
2020-04-28 12:44:28 +02:00
if(fail){
glDeleteShader(vs);
2020-04-26 19:33:43 +02:00
return nil;
2020-04-28 12:44:28 +02:00
}
2020-04-26 19:33:43 +02:00
fail = linkprogram(vs, fs, &program);
2020-04-28 12:44:28 +02:00
if(fail){
glDeleteShader(fs);
glDeleteShader(vs);
2020-04-26 19:33:43 +02:00
return nil;
2020-04-28 12:44:28 +02:00
}
2020-04-26 19:33:43 +02:00
glDeleteProgram(vs);
glDeleteProgram(fs);
Shader *sh = rwNewT(Shader, 1, MEMDUR_EVENT | ID_DRIVER); // or global?
2020-05-13 20:46:27 +02:00
#ifdef xxxRW_GLES2
int numUniforms;
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numUniforms);
for(i = 0; i < numUniforms; i++){
GLint size;
GLenum type;
char name[100];
glGetActiveUniform(program, i, 100, nil, &size, &type, name);
printf("%d %d %s\n", size, type, name);
}
printf("\n");
#endif
#ifdef xxxRW_GLES2
2020-05-13 20:46:27 +02:00
int numAttribs;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &numAttribs);
for(i = 0; i < numAttribs; i++){
GLint size;
GLenum type;
char name[100];
glGetActiveAttrib(program, i, 100, nil, &size, &type, name);
GLint bind = glGetAttribLocation(program, name);
printf("%d %d %s. %d\n", size, type, name, bind);
}
printf("\n");
#endif
2020-04-26 19:33:43 +02:00
// set uniform block binding
for(i = 0; i < uniformRegistry.numBlocks; i++){
int idx = glGetUniformBlockIndex(program,
uniformRegistry.blockNames[i]);
if(idx >= 0)
glUniformBlockBinding(program, idx, i);
}
// query uniform locations
sh->program = program;
sh->uniformLocations = rwNewT(GLint, uniformRegistry.numUniforms, MEMDUR_EVENT | ID_DRIVER);
for(i = 0; i < uniformRegistry.numUniforms; i++)
sh->uniformLocations[i] = glGetUniformLocation(program,
uniformRegistry.uniformNames[i]);
// set samplers
glUseProgram(program);
char name[64];
GLint loc;
for(i = 0; i < 4; i++){
sprintf(name, "tex%d", i);
loc = glGetUniformLocation(program, name);
glUniform1i(loc, i);
}
return sh;
}
void
Shader::use(void)
{
2017-08-09 10:57:32 +02:00
if(currentShader != this){
glUseProgram(this->program);
currentShader = this;
}
}
2017-08-29 10:12:56 +02:00
void
Shader::destroy(void)
{
glDeleteProgram(this->program);
rwFree(this->uniformLocations);
rwFree(this);
}
}
}
#endif