Merge pull request #384 from nounoursheureux/develop

Store the default shaders
This commit is contained in:
Ray 2017-11-10 12:10:49 +01:00 committed by GitHub
commit e12182f59b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 99 deletions

View File

@ -20,10 +20,8 @@
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#define DEFAULT_VERTEX_SHADER "resources/shaders/glsl330/base.vs"
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#define DEFAULT_VERTEX_SHADER "resources/shaders/glsl100/base.vs"
#endif
#define MAX_POSTPRO_SHADERS 12
@ -85,18 +83,18 @@ int main()
// NOTE 2: We load the correct shader depending on GLSL version
Shader shaders[MAX_POSTPRO_SHADERS];
shaders[FX_GRAYSCALE] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
shaders[FX_POSTERIZATION] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION));
shaders[FX_DREAM_VISION] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION));
shaders[FX_PIXELIZER] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION));
shaders[FX_CROSS_HATCHING] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION));
shaders[FX_CROSS_STITCHING] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION));
shaders[FX_PREDATOR_VIEW] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION));
shaders[FX_SCANLINES] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION));
shaders[FX_FISHEYE] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION));
shaders[FX_SOBEL] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION));
shaders[FX_BLOOM] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION));
shaders[FX_BLUR] = LoadShader(DEFAULT_VERTEX_SHADER, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION));
shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
shaders[FX_POSTERIZATION] = LoadShader(0, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION));
shaders[FX_DREAM_VISION] = LoadShader(0, FormatText("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION));
shaders[FX_PIXELIZER] = LoadShader(0, FormatText("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION));
shaders[FX_CROSS_HATCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION));
shaders[FX_CROSS_STITCHING] = LoadShader(0, FormatText("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION));
shaders[FX_PREDATOR_VIEW] = LoadShader(0, FormatText("resources/shaders/glsl%i/predator.fs", GLSL_VERSION));
shaders[FX_SCANLINES] = LoadShader(0, FormatText("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION));
shaders[FX_FISHEYE] = LoadShader(0, FormatText("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION));
shaders[FX_SOBEL] = LoadShader(0, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION));
shaders[FX_BLOOM] = LoadShader(0, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION));
shaders[FX_BLUR] = LoadShader(0, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION));
int currentShader = FX_GRAYSCALE;
@ -177,4 +175,4 @@ int main()
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -267,6 +267,10 @@ static Vector3 *tempBuffer;
static int tempBufferCount = 0;
static bool useTempBuffer = false;
// Shaders
static unsigned int defaultVertexShader;
static unsigned int defaultFragmentShader;
// Shader Programs
static Shader defaultShader; // Basic shader, support vertex color and diffuse texture
static Shader currentShader; // Shader to be used on rendering (by default, defaultShader)
@ -324,7 +328,8 @@ static int screenHeight; // Default framebuffer height
//----------------------------------------------------------------------------------
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
static void LoadTextureCompressed(unsigned char *data, int width, int height, int compressedFormat, int mipmapCount);
static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShaderStr); // Load custom shader strings and return program id
static unsigned int LoadShaderPartial(const char *shaderStr, int type); // Load custom shader and return shader id
static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fragmentShader); // Load custom shader strings and return program id
static Shader LoadShaderDefault(void); // Load default shader (just vertex positioning and texture coloring)
static void SetShaderDefaultLocations(Shader *shader); // Bind default shader locations (attributes and uniforms)
@ -2359,22 +2364,33 @@ Shader LoadShader(char *vsFileName, char *fsFileName)
for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Shaders loading from external text file
char *vShaderStr = LoadText(vsFileName);
char *fShaderStr = LoadText(fsFileName);
if ((vShaderStr != NULL) && (fShaderStr != NULL))
{
shader.id = LoadShaderProgram(vShaderStr, fShaderStr);
unsigned int vertexShader, fragmentShader;
// After shader loading, we TRY to set default location names
if (shader.id > 0) SetShaderDefaultLocations(&shader);
// Shader strings must be freed
if (vsFileName == NULL) {
vertexShader = defaultVertexShader;
} else {
char *vShaderStr = LoadText(vsFileName);
vertexShader = LoadShaderPartial(vShaderStr, GL_VERTEX_SHADER);
free(vShaderStr);
}
if (fsFileName == NULL) {
fragmentShader = defaultVertexShader;
} else {
char* fShaderStr = LoadText(fsFileName);
fragmentShader = LoadShaderPartial(fShaderStr, GL_FRAGMENT_SHADER);
free(fShaderStr);
}
shader.id = LoadShaderProgram(vertexShader, fragmentShader);
// After shader loading, we TRY to set default location names
if (shader.id > 0) SetShaderDefaultLocations(&shader);
if (vertexShader != defaultVertexShader) glDeleteShader(vertexShader);
if (fragmentShader != defaultFragmentShader) glDeleteShader(fragmentShader);
if (shader.id == 0)
{
TraceLog(LOG_WARNING, "Custom shader could not be loaded");
@ -3127,82 +3143,48 @@ static void LoadTextureCompressed(unsigned char *data, int width, int height, in
}
}
static unsigned int LoadShaderPartial(const char *shaderStr, int type)
{
unsigned int shader = glCreateShader(type);
glShaderSource(shader, 1, &shaderStr, NULL);
GLint success = 0;
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success != GL_TRUE)
{
TraceLog(LOG_WARNING, "[VSHDR ID %i] Failed to compile shader...", shader);
int maxLength = 0;
int length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
#ifdef _MSC_VER
char *log = malloc(maxLength);
#else
char log[maxLength];
#endif
glGetShaderInfoLog(shader, maxLength, &length, log);
TraceLog(LOG_INFO, "%s", log);
#ifdef _MSC_VER
free(log);
#endif
}
else TraceLog(LOG_INFO, "[VSHDR ID %i] Shader compiled successfully", shader);
return shader;
}
// Load custom shader strings and return program id
static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShaderStr)
static unsigned int LoadShaderProgram(unsigned int vertexShader, unsigned int fragmentShader)
{
unsigned int program = 0;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
GLuint vertexShader;
GLuint fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const char *pvs = vShaderStr;
const char *pfs = fShaderStr;
glShaderSource(vertexShader, 1, &pvs, NULL);
glShaderSource(fragmentShader, 1, &pfs, NULL);
GLint success = 0;
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (success != GL_TRUE)
{
TraceLog(LOG_WARNING, "[VSHDR ID %i] Failed to compile vertex shader...", vertexShader);
int maxLength = 0;
int length;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
#ifdef _MSC_VER
char *log = malloc(maxLength);
#else
char log[maxLength];
#endif
glGetShaderInfoLog(vertexShader, maxLength, &length, log);
TraceLog(LOG_INFO, "%s", log);
#ifdef _MSC_VER
free(log);
#endif
}
else TraceLog(LOG_INFO, "[VSHDR ID %i] Vertex shader compiled successfully", vertexShader);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (success != GL_TRUE)
{
TraceLog(LOG_WARNING, "[FSHDR ID %i] Failed to compile fragment shader...", fragmentShader);
int maxLength = 0;
int length;
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
#ifdef _MSC_VER
char *log = malloc(maxLength);
#else
char log[maxLength];
#endif
glGetShaderInfoLog(fragmentShader, maxLength, &length, log);
TraceLog(LOG_INFO, "%s", log);
#ifdef _MSC_VER
free(log);
#endif
}
else TraceLog(LOG_INFO, "[FSHDR ID %i] Fragment shader compiled successfully", fragmentShader);
program = glCreateProgram();
glAttachShader(program, vertexShader);
@ -3250,9 +3232,6 @@ static unsigned int LoadShaderProgram(const char *vShaderStr, const char *fShade
program = 0;
}
else TraceLog(LOG_INFO, "[SHDR ID %i] Shader program loaded successfully", program);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
#endif
return program;
}
@ -3326,7 +3305,9 @@ static Shader LoadShaderDefault(void)
#endif
"} \n";
shader.id = LoadShaderProgram(vDefaultShaderStr, fDefaultShaderStr);
defaultVertexShader = LoadShaderPartial(vDefaultShaderStr, GL_VERTEX_SHADER);
defaultFragmentShader = LoadShaderPartial(fDefaultShaderStr, GL_FRAGMENT_SHADER);
shader.id = LoadShaderProgram(defaultVertexShader, defaultFragmentShader);
if (shader.id > 0)
{
@ -3388,6 +3369,8 @@ static void UnloadShaderDefault(void)
{
glUseProgram(0);
glDeleteShader(defaultVertexShader);
glDeleteShader(defaultFragmentShader);
//glDetachShader(defaultShader, vertexShader);
//glDetachShader(defaultShader, fragmentShader);
//glDeleteShader(vertexShader); // Already deleted on shader compilation