OpenGL: tweak image shaders & code that uses them
- rename image shaders to describe exactly what they do - rename inputs to match other built-in shaders - set & use active texture unit - no need to enable/disable textures with GLSL - pull vertex format setup out of loops
This commit is contained in:
@@ -675,11 +675,11 @@ static void draw_empty_image(Object *ob, const short dflag, const unsigned char
|
||||
}
|
||||
|
||||
VertexFormat *format = immVertexFormat();
|
||||
unsigned pos = add_attrib(format, "position", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
unsigned texCoord = add_attrib(format, "texcoord", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
immBindBuiltinProgram(GPU_SHADER_2D_TEXTURE_2D); /* TODO: rename GPU_SHADER_3D_IMAGE_2D_MODULATE_ALPHA */
|
||||
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
unsigned texCoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_MODULATE_ALPHA);
|
||||
immUniform1f("alpha", ob_alpha);
|
||||
immUniform1i("texture_map", texUnit); /* TODO: rename "image" */
|
||||
immUniform1i("image", texUnit);
|
||||
|
||||
immBegin(GL_TRIANGLE_FAN, 4);
|
||||
immAttrib2f(texCoord, 0.0f, 0.0f);
|
||||
|
||||
@@ -133,9 +133,9 @@ data_to_c_simple(shaders/gpu_shader_2D_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_image_modulate_alpha_frag.glsl SRC)
|
||||
data_to_c_simple(shaders/gpu_shader_image_rect_modulate_alpha_frag.glsl SRC)
|
||||
data_to_c_simple(shaders/gpu_shader_3D_image_vert.glsl SRC)
|
||||
data_to_c_simple(shaders/gpu_shader_3D_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)
|
||||
|
||||
@@ -101,9 +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,
|
||||
/* basic image drawing */
|
||||
GPU_SHADER_3D_IMAGE_MODULATE_ALPHA,
|
||||
GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA,
|
||||
/* points */
|
||||
GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR,
|
||||
GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_SMOOTH,
|
||||
|
||||
@@ -53,9 +53,9 @@ extern char datatoc_gpu_shader_2D_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_image_vert_glsl[];
|
||||
extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
|
||||
extern char datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl[];
|
||||
extern char datatoc_gpu_shader_3D_vert_glsl[];
|
||||
extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
|
||||
extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
|
||||
@@ -105,9 +105,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 drawing images */
|
||||
GPUShader *image_modulate_alpha_3D;
|
||||
GPUShader *image_rect_modulate_alpha_3D;
|
||||
/* for simple 2D drawing */
|
||||
GPUShader *uniform_color_2D;
|
||||
GPUShader *flat_color_2D;
|
||||
@@ -651,21 +651,21 @@ 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,
|
||||
case GPU_SHADER_3D_IMAGE_MODULATE_ALPHA:
|
||||
if (!GG.shaders.image_modulate_alpha_3D)
|
||||
GG.shaders.image_modulate_alpha_3D = GPU_shader_create(
|
||||
datatoc_gpu_shader_3D_image_vert_glsl,
|
||||
datatoc_gpu_shader_image_modulate_alpha_frag_glsl,
|
||||
NULL, NULL, NULL, 0, 0, 0);
|
||||
retval = GG.shaders.texture_2D;
|
||||
retval = GG.shaders.image_modulate_alpha_3D;
|
||||
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,
|
||||
case GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA:
|
||||
if (!GG.shaders.image_rect_modulate_alpha_3D)
|
||||
GG.shaders.image_rect_modulate_alpha_3D = GPU_shader_create(
|
||||
datatoc_gpu_shader_3D_image_vert_glsl,
|
||||
datatoc_gpu_shader_image_rect_modulate_alpha_frag_glsl,
|
||||
NULL, NULL, NULL, 0, 0, 0);
|
||||
retval = GG.shaders.texture_rect;
|
||||
retval = GG.shaders.image_rect_modulate_alpha_3D;
|
||||
break;
|
||||
case GPU_SHADER_2D_UNIFORM_COLOR:
|
||||
if (!GG.shaders.uniform_color_2D)
|
||||
@@ -917,14 +917,14 @@ 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.image_modulate_alpha_3D) {
|
||||
GPU_shader_free(GG.shaders.image_modulate_alpha_3D);
|
||||
GG.shaders.image_modulate_alpha_3D = NULL;
|
||||
}
|
||||
|
||||
if (GG.shaders.texture_rect) {
|
||||
GPU_shader_free(GG.shaders.texture_rect);
|
||||
GG.shaders.texture_rect = NULL;
|
||||
if (GG.shaders.image_rect_modulate_alpha_3D) {
|
||||
GPU_shader_free(GG.shaders.image_rect_modulate_alpha_3D);
|
||||
GG.shaders.image_rect_modulate_alpha_3D = NULL;
|
||||
}
|
||||
|
||||
if (GG.shaders.uniform_color_2D) {
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
|
||||
uniform mat4 ModelViewProjectionMatrix;
|
||||
|
||||
#if __VERSION__ == 120
|
||||
attribute vec2 texcoord;
|
||||
attribute vec3 position;
|
||||
varying vec2 texture_coord;
|
||||
#else
|
||||
in vec2 texcoord;
|
||||
in vec3 position;
|
||||
out vec2 texture_coord;
|
||||
#endif
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = ModelViewProjectionMatrix * vec4(position.xyz, 1.0f);
|
||||
texture_coord = texcoord;
|
||||
}
|
||||
18
source/blender/gpu/shaders/gpu_shader_3D_image_vert.glsl
Normal file
18
source/blender/gpu/shaders/gpu_shader_3D_image_vert.glsl
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
uniform mat4 ModelViewProjectionMatrix;
|
||||
|
||||
#if __VERSION__ == 120
|
||||
attribute vec2 texCoord;
|
||||
attribute vec3 pos;
|
||||
varying vec2 texCoord_interp;
|
||||
#else
|
||||
in vec2 texCoord;
|
||||
in vec3 pos;
|
||||
out vec2 texCoord_interp;
|
||||
#endif
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = ModelViewProjectionMatrix * vec4(pos.xyz, 1.0f);
|
||||
texCoord_interp = texCoord;
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
|
||||
#if __VERSION__ == 120
|
||||
varying vec2 texture_coord;
|
||||
varying vec2 texCoord_interp;
|
||||
#define fragColor gl_FragColor
|
||||
#else
|
||||
in vec2 texture_coord;
|
||||
in vec2 texCoord_interp;
|
||||
out vec4 fragColor;
|
||||
#define texture2D texture
|
||||
#endif
|
||||
|
||||
uniform float alpha;
|
||||
uniform sampler2D texture_map;
|
||||
uniform sampler2D image;
|
||||
|
||||
void main()
|
||||
{
|
||||
fragColor = texture2D(texture_map, texture_coord);
|
||||
fragColor = texture2D(image, texCoord_interp);
|
||||
fragColor.a *= alpha;
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
|
||||
#if __VERSION__ == 120
|
||||
varying vec2 texture_coord;
|
||||
varying vec2 texCoord_interp;
|
||||
#define fragColor gl_FragColor
|
||||
#else
|
||||
in vec2 texture_coord;
|
||||
in vec2 texCoord_interp;
|
||||
out vec4 fragColor;
|
||||
#define texture2DRect texture
|
||||
#endif
|
||||
|
||||
uniform float alpha;
|
||||
uniform sampler2DRect texture_map;
|
||||
uniform sampler2DRect image;
|
||||
|
||||
void main()
|
||||
{
|
||||
fragColor = texture2DRect(texture_map, texture_coord);
|
||||
fragColor = texture2DRect(image, texCoord_interp);
|
||||
fragColor.a *= alpha;
|
||||
}
|
||||
@@ -447,16 +447,16 @@ void wm_triple_draw_textures(wmWindow *win, wmDrawTriple *triple, float alpha)
|
||||
}
|
||||
|
||||
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);
|
||||
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
|
||||
const int activeTex = GL_TEXTURE0;
|
||||
glActiveTexture(activeTex);
|
||||
glBindTexture(triple->target, triple->bind);
|
||||
|
||||
immBindBuiltinProgram((triple->target == GL_TEXTURE_2D) ? GPU_SHADER_3D_IMAGE_MODULATE_ALPHA : GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA);
|
||||
immUniform1f("alpha", alpha);
|
||||
immUniform1i("texture_map", 0);
|
||||
immUniform1i("image", activeTex);
|
||||
|
||||
immBegin(GL_QUADS, 4);
|
||||
|
||||
@@ -476,7 +476,6 @@ void wm_triple_draw_textures(wmWindow *win, wmDrawTriple *triple, float alpha)
|
||||
immUnbindProgram();
|
||||
|
||||
glBindTexture(triple->target, 0);
|
||||
glDisable(triple->target);
|
||||
}
|
||||
|
||||
static void wm_triple_copy_textures(wmWindow *win, wmDrawTriple *triple)
|
||||
|
||||
@@ -173,6 +173,13 @@ static void wm_method_draw_stereo3d_sidebyside(wmWindow *win)
|
||||
int soffx;
|
||||
bool cross_eyed = (win->stereo3d_format->flag & S3D_SIDEBYSIDE_CROSSEYED) != 0;
|
||||
|
||||
const int activeTex = GL_TEXTURE0;
|
||||
glActiveTexture(activeTex);
|
||||
|
||||
VertexFormat *format = immVertexFormat();
|
||||
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
|
||||
for (view = 0; view < 2; view ++) {
|
||||
drawdata = BLI_findlink(&win->drawdata, (view * 2) + 1);
|
||||
triple = drawdata->triple;
|
||||
@@ -204,16 +211,12 @@ static void wm_method_draw_stereo3d_sidebyside(wmWindow *win)
|
||||
halfy /= triple->y;
|
||||
}
|
||||
|
||||
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);
|
||||
/* TODO: if target is always same for both eyes, bind program & set uniform before loop */
|
||||
immBindBuiltinProgram((triple->target == GL_TEXTURE_2D) ? GPU_SHADER_3D_IMAGE_MODULATE_ALPHA : GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA);
|
||||
|
||||
glBindTexture(triple->target, triple->bind);
|
||||
|
||||
immUniform1i("texture_map", 0);
|
||||
immUniform1i("image", activeTex);
|
||||
|
||||
immBegin(GL_QUADS, 4);
|
||||
|
||||
@@ -230,10 +233,10 @@ static void wm_method_draw_stereo3d_sidebyside(wmWindow *win)
|
||||
immVertex2f(pos, soffx, sizey);
|
||||
|
||||
immEnd();
|
||||
immUnbindProgram();
|
||||
|
||||
/* TODO: if target is always same for both eyes, unbind program & texture target after loop */
|
||||
glBindTexture(triple->target, 0);
|
||||
glDisable(triple->target);
|
||||
immUnbindProgram();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,6 +248,13 @@ static void wm_method_draw_stereo3d_topbottom(wmWindow *win)
|
||||
int view;
|
||||
int soffy;
|
||||
|
||||
const int activeTex = GL_TEXTURE0;
|
||||
glActiveTexture(activeTex);
|
||||
|
||||
VertexFormat *format = immVertexFormat();
|
||||
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
|
||||
|
||||
for (view = 0; view < 2; view ++) {
|
||||
drawdata = BLI_findlink(&win->drawdata, (view * 2) + 1);
|
||||
triple = drawdata->triple;
|
||||
@@ -273,17 +283,13 @@ static void wm_method_draw_stereo3d_topbottom(wmWindow *win)
|
||||
halfy /= triple->y;
|
||||
}
|
||||
|
||||
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);
|
||||
/* TODO: if target is always same for both eyes, bind program & set uniforms before loop */
|
||||
immBindBuiltinProgram((triple->target == GL_TEXTURE_2D) ? GPU_SHADER_3D_IMAGE_MODULATE_ALPHA : GPU_SHADER_3D_IMAGE_RECT_MODULATE_ALPHA);
|
||||
|
||||
glBindTexture(triple->target, triple->bind);
|
||||
|
||||
immUniform1f("alpha", 1.0f);
|
||||
immUniform1i("texture_map", 0);
|
||||
immUniform1i("image", activeTex);
|
||||
|
||||
immBegin(GL_QUADS, 4);
|
||||
|
||||
@@ -300,10 +306,10 @@ static void wm_method_draw_stereo3d_topbottom(wmWindow *win)
|
||||
immVertex2f(pos, 0.0f, soffy + (sizey * 0.5f));
|
||||
|
||||
immEnd();
|
||||
immUnbindProgram();
|
||||
|
||||
/* TODO: if target is always same for both eyes, unbind program & texture target after loop */
|
||||
immUnbindProgram();
|
||||
glBindTexture(triple->target, 0);
|
||||
glDisable(triple->target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user