1
1

immediate mode: Triple Buffer and two new shaders for TEXTURE_2D and TEXTURE_RECT

Use the same vertex shader for both fragment shaders
This commit is contained in:
Dalai Felinto
2016-09-22 13:20:44 +00:00
parent 1d469f3780
commit 4a1feaa555
9 changed files with 116 additions and 14 deletions

View File

@@ -125,6 +125,9 @@ data_to_c_simple(shaders/gpu_shader_2D_no_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_flat_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_smooth_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_smooth_color_frag.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_texture_2D_frag.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_texture_rect_frag.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_texture_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_no_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_flat_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_smooth_color_vert.glsl SRC)

View File

@@ -101,6 +101,9 @@ typedef enum GPUBuiltinShader {
GPU_SHADER_3D_FLAT_COLOR,
GPU_SHADER_3D_SMOOTH_COLOR,
GPU_SHADER_3D_DEPTH_ONLY,
/* basic 2D texture drawing */
GPU_SHADER_2D_TEXTURE_2D,
GPU_SHADER_2D_TEXTURE_RECT,
} GPUBuiltinShader;
GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader);

View File

@@ -650,3 +650,14 @@ void immUniformColor4ubv(const unsigned char rgba[4])
const float scale = 1.0f / 255.0f;
immUniform4f("color", scale * rgba[0], scale * rgba[1], scale * rgba[2], rgba[3]);
}
void immUniform1i(const char *name, const unsigned int data)
{
int loc = glGetUniformLocation(imm.bound_program, name);
#if TRUST_NO_ONE
assert(loc != -1);
#endif
glUniform1i(loc, data);
}

View File

@@ -72,3 +72,5 @@ void immUniform4f(const char* name, float x, float y, float z, float w);
// TODO: treat as sRGB?
void immUniformColor3ubv(const unsigned char data[3]);
void immUniformColor4ubv(const unsigned char data[4]);
void immUniform1i(const char *name, const unsigned int data);

View File

@@ -53,6 +53,9 @@ extern char datatoc_gpu_shader_2D_no_color_vert_glsl[];
extern char datatoc_gpu_shader_2D_flat_color_vert_glsl[];
extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[];
extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[];
extern char datatoc_gpu_shader_2D_texture_vert_glsl[];
extern char datatoc_gpu_shader_2D_texture_2D_frag_glsl[];
extern char datatoc_gpu_shader_2D_texture_rect_frag_glsl[];
extern char datatoc_gpu_shader_3D_no_color_vert_glsl[];
extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
@@ -88,6 +91,9 @@ static struct GPUShadersGlobal {
GPUShader *fx_shaders[MAX_FX_SHADERS * 2];
/* for drawing text */
GPUShader *text;
/* for drawing texture */
GPUShader *texture_2D;
GPUShader *texture_rect;
/* for simple 2D drawing */
GPUShader *uniform_color_2D;
GPUShader *flat_color_2D;
@@ -623,6 +629,22 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
NULL, NULL, NULL, 0, 0, 0);
retval = GG.shaders.text;
break;
case GPU_SHADER_2D_TEXTURE_2D:
if (!GG.shaders.texture_2D)
GG.shaders.texture_2D = GPU_shader_create(
datatoc_gpu_shader_2D_texture_vert_glsl,
datatoc_gpu_shader_2D_texture_2D_frag_glsl,
NULL, NULL, NULL, 0, 0, 0);
retval = GG.shaders.texture_2D;
break;
case GPU_SHADER_2D_TEXTURE_RECT:
if (!GG.shaders.texture_rect)
GG.shaders.texture_rect = GPU_shader_create(
datatoc_gpu_shader_2D_texture_vert_glsl,
datatoc_gpu_shader_2D_texture_rect_frag_glsl,
NULL, NULL, NULL, 0, 0, 0);
retval = GG.shaders.texture_rect;
break;
case GPU_SHADER_2D_UNIFORM_COLOR:
if (!GG.shaders.uniform_color_2D)
GG.shaders.uniform_color_2D = GPU_shader_create(
@@ -795,6 +817,16 @@ void GPU_shader_free_builtin_shaders(void)
GG.shaders.text = NULL;
}
if (GG.shaders.texture_2D) {
GPU_shader_free(GG.shaders.texture_2D);
GG.shaders.texture_2D = NULL;
}
if (GG.shaders.texture_rect) {
GPU_shader_free(GG.shaders.texture_rect);
GG.shaders.texture_rect = NULL;
}
if (GG.shaders.uniform_color_2D) {
GPU_shader_free(GG.shaders.uniform_color_2D);
GG.shaders.uniform_color_2D = NULL;

View File

@@ -0,0 +1,14 @@
#if __VERSION__ == 120
varying vec2 texture_coord;
#define fragColor gl_FragColor
#else
in vec2 texture_coord;
out vec4 fragColor;
#endif
uniform sampler2D texture_map;
void main()
{
fragColor = texture2D(texture_map, texture_coord);
}

View File

@@ -0,0 +1,14 @@
#if __VERSION__ == 120
varying vec2 texture_coord;
#define fragColor gl_FragColor
#else
in vec2 texture_coord;
out vec4 fragColor;
#endif
uniform sampler2DRect texture_map;
void main()
{
fragColor = texture2DRect(texture_map, texture_coord);
}

View File

@@ -0,0 +1,14 @@
#if __VERSION__ == 120
varying vec2 texture_coord;
#else
out vec2 texture_coord;
#endif
in vec2 texcoord;
in vec3 position;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(position.xyz, 1.0f);
texture_coord = texcoord;
}

View File

@@ -59,6 +59,7 @@
#include "GPU_extensions.h"
#include "GPU_glew.h"
#include "GPU_basic_shader.h"
#include "GPU_immediate.h"
#include "RE_engine.h"
@@ -445,28 +446,36 @@ void wm_triple_draw_textures(wmWindow *win, wmDrawTriple *triple, float alpha)
halfy /= triple->y;
}
GPU_basic_shader_bind((triple->target == GL_TEXTURE_2D) ? GPU_SHADER_TEXTURE_2D : GPU_SHADER_TEXTURE_RECT);
VertexFormat *format = immVertexFormat();
unsigned texcoord = add_attrib(format, "texcoord", GL_FLOAT, 2, KEEP_FLOAT);
unsigned pos = add_attrib(format, "position", GL_FLOAT, 2, KEEP_FLOAT);
glEnable(triple->target);
immBindBuiltinProgram((triple->target == GL_TEXTURE_2D) ? GPU_SHADER_2D_TEXTURE_2D : GPU_SHADER_2D_TEXTURE_RECT);
glBindTexture(triple->target, triple->bind);
glColor4f(1.0f, 1.0f, 1.0f, alpha);
glBegin(GL_QUADS);
glTexCoord2f(halfx, halfy);
glVertex2f(0, 0);
immUniform1i("texture_map", 0);
glTexCoord2f(ratiox + halfx, halfy);
glVertex2f(sizex, 0);
immBegin(GL_QUADS, 4);
glTexCoord2f(ratiox + halfx, ratioy + halfy);
glVertex2f(sizex, sizey);
immAttrib2f(texcoord, halfx, halfy);
immVertex2f(pos, 0.0f, 0.0f);
glTexCoord2f(halfx, ratioy + halfy);
glVertex2f(0, sizey);
glEnd();
immAttrib2f(texcoord, ratiox + halfx, halfy);
immVertex2f(pos, sizex, 0.0f);
immAttrib2f(texcoord, ratiox + halfx, ratioy + halfy);
immVertex2f(pos, sizex, sizey);
immAttrib2f(texcoord, halfx, ratioy + halfy);
immVertex2f(pos, 0.0f, sizey);
immEnd();
immUnbindProgram();
glBindTexture(triple->target, 0);
GPU_basic_shader_bind(GPU_SHADER_USE_COLOR);
glDisable(triple->target);
}
static void wm_triple_copy_textures(wmWindow *win, wmDrawTriple *triple)