[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29568] branches/soc-2010-jwilkins/source/ blender/windowmanager/intern/wm_draw.c: * no functional changes
Jason Wilkins
Jason.A.Wilkins at gmail.com
Sat Jun 19 17:50:54 CEST 2010
Revision: 29568
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29568
Author: jwilkins
Date: 2010-06-19 17:50:54 +0200 (Sat, 19 Jun 2010)
Log Message:
-----------
* no functional changes
* commented code in wm_draw.c that handles saving/restoring depth buffer
Modified Paths:
--------------
branches/soc-2010-jwilkins/source/blender/windowmanager/intern/wm_draw.c
Modified: branches/soc-2010-jwilkins/source/blender/windowmanager/intern/wm_draw.c
===================================================================
--- branches/soc-2010-jwilkins/source/blender/windowmanager/intern/wm_draw.c 2010-06-19 12:15:45 UTC (rev 29567)
+++ branches/soc-2010-jwilkins/source/blender/windowmanager/intern/wm_draw.c 2010-06-19 15:50:54 UTC (rev 29568)
@@ -561,8 +561,96 @@
glBindTexture(triple->target, 0);
}
+/* compile and link the shader program needed to write a depth texture to the depth buffer */
+static void load_depth_shader_program(wmDrawTriple* triple)
+{
+ int success;
+ GLsizei len;
+ GLbyte infoLog[1000];
+
+ /* This vertex program just passes the texture coordinate through and transforms the vertex position */
+ static const GLcharARB* depth_vertex_shader_source[] = {
+ "void main()\n",
+ "{\n",
+ " gl_TexCoord[0] = gl_MultiTexCoord0;\n",
+ " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n",
+ "}\n",
+ };
+
+ /* This fragment program is used non-rectangle textures */
+ static const GLcharARB* depth_fragment_shader_source[] = {
+ "uniform sampler2D depth_texture;\n",
+ "void main()\n",
+ "{\n",
+ " gl_FragDepth = texture2D(depth_texture, gl_TexCoord[0].xy).x;\n",
+ "}\n",
+ };
+
+ /* This fragment program is used for rectangular textures */
+ static const GLcharARB* depth_fragment_shader_rect_source[] = {
+ "#extension GL_ARB_texture_rectangle : enable\n",
+ "uniform sampler2DRect depth_texture;\n",
+ "void main()\n",
+ "{\n",
+ " gl_FragDepth = texture2DRect(depth_texture, gl_TexCoord[0].xy).x;\n",
+ "}\n",
+ };
+
+ /* compile the vertex program */
+
+ triple->depth_vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
+
+ glShaderSourceARB(triple->depth_vertex_shader, sizeof(depth_vertex_shader_source)/sizeof(GLcharARB*), depth_vertex_shader_source, NULL);
+ glCompileShaderARB(triple->depth_vertex_shader);
+
+ /* print any errors/warnings gotten while compiling the vertex program */
+
+ glGetObjectParameterivARB(triple->depth_vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success);
+ glGetInfoLogARB(triple->depth_vertex_shader, 1000, &len, infoLog);
+
+ if (len > 0)
+ fprintf(stderr, "triple depth buffer vertex program compilation messages:\n%s\n", infoLog);
+
+ /* compile the appropriate fragment program depending on support for rectangular textures */
+
+ triple->depth_fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
+
+ if (GLEW_ARB_texture_rectangle)
+ glShaderSourceARB(triple->depth_fragment_shader, sizeof(depth_fragment_shader_rect_source)/sizeof(GLcharARB*), depth_fragment_shader_rect_source, NULL);
+ else
+ glShaderSourceARB(triple->depth_fragment_shader, sizeof(depth_fragment_shader_source)/sizeof(GLcharARB*), depth_fragment_shader_source, NULL);
+
+ glCompileShaderARB(triple->depth_fragment_shader);
+
+ /* print any errors/warnings gotten while compiling the fragment program */
+
+ glGetObjectParameterivARB(triple->depth_fragment_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success);
+ glGetInfoLogARB(triple->depth_fragment_shader, 1000, &len, infoLog);
+
+ if (len > 0)
+ fprintf(stderr, "triple depth buffer fragment program compilation messages:\n%s\n", infoLog);
+
+ /* link the shaders into a complete program */
+
+ triple->depth_program = glCreateProgramObjectARB();
+
+ glAttachObjectARB(triple->depth_program, triple->depth_vertex_shader);
+ glAttachObjectARB(triple->depth_program, triple->depth_fragment_shader);
+
+ glLinkProgramARB(triple->depth_program);
+
+ /* print any errors/warnings gotten whilelinking the final fragment program */
+
+ glGetObjectParameterivARB(triple->depth_program, GL_OBJECT_LINK_STATUS_ARB, &success);
+ glGetInfoLogARB(triple->depth_program, 1000, &len, infoLog);
+
+ if (len > 0)
+ fprintf(stderr, "triple depth buffer program linker messages:\n%s\n", infoLog);
+}
+
static int wm_triple_gen_depth_buffer(wmWindow *win, wmDrawTriple *triple)
{
+ /* To do this fast we need support for depth textures and GLSL */
if (GLEW_ARB_depth_texture &&
GLEW_ARB_shader_objects &&
GLEW_ARB_vertex_shader &&
@@ -572,6 +660,9 @@
GLint maxsize;
int x, y;
+ /* XXX: this is copied from wm_triple_gen_textures.
+ can probably combine them together once this is accepted into trunk */
+
/* compute texture sizes */
if(GLEW_ARB_texture_rectangle) {
triple->depth_target= GL_TEXTURE_RECTANGLE_ARB;
@@ -618,10 +709,18 @@
/* setup actual texture */
glBindTexture(triple->depth_target, triple->depth_bind[x + y*triple->depth_nx]);
+
+ /* important difference from wm_triple_gen_textures!
+ use GL_DEPTH_COMPONENT as format and internalformat */
glTexImage2D(triple->depth_target, 0, GL_DEPTH_COMPONENT, triple->depth_x[x], triple->depth_y[y], 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
+
glTexParameteri(triple->depth_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(triple->depth_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+ /* important difference from wm_triple_gen_textures!
+ turn off depth comparison mode that would be used if this was a shadow map */
glTexParameteri(triple->depth_target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
+
glBindTexture(triple->depth_target, 0);
/* not sure if this works everywhere .. */
@@ -632,99 +731,9 @@
}
}
- {
- int success;
- GLsizei len;
-
- static const GLcharARB* depth_vertex_shader_source[] = {
-// "varying vec2 texture_coordinate;\n",
- "void main()\n",
- "{\n",
- "gl_TexCoord[0] = gl_MultiTexCoord0;\n",
- "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n",
- "}\n",
- };
-
- static const GLcharARB* depth_fragment_shader_source[] = {
-// "varying vec2 texture_coordinate;\n",
- "uniform sampler2DRect depth_texture;\n",
- "void main()\n",
- "{\n",
- "gl_FragDepth = texture2D(depth_texture, gl_TexCoord[0].xy).x;\n",
- "}\n",
- };
-
- static const GLcharARB* depth_fragment_shader_rect_source[] = {
- "#extension GL_ARB_texture_rectangle : enable\n",
-// "varying vec2 texture_coordinate;\n",
- "uniform sampler2DRect depth_texture;\n",
- "void main()\n",
- "{\n",
- "gl_FragDepth = texture2DRect(depth_texture, gl_TexCoord[0].xy).x;\n",
- "}\n",
- };
-
- triple->depth_vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
- glShaderSourceARB(triple->depth_vertex_shader, sizeof(depth_vertex_shader_source)/sizeof(GLcharARB*), depth_vertex_shader_source, NULL);
- glCompileShaderARB(triple->depth_vertex_shader);
-
- glGetObjectParameterivARB(triple->depth_vertex_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success);
-
- /*if (!success)*/ {
- GLbyte infoLog[1000];
- len = 1000;
-
- glGetInfoLogARB(triple->depth_vertex_shader, 1000, &len, infoLog);
-
- //fprintf(stderr, "Error in vertex shader compilation!\n");
-
- if (len > 0)
- fprintf(stderr, "vertex:\n%s\n", infoLog);
- }
-
- triple->depth_fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
-
- if (GLEW_ARB_texture_rectangle)
- glShaderSourceARB(triple->depth_fragment_shader, sizeof(depth_fragment_shader_rect_source)/sizeof(GLcharARB*), depth_fragment_shader_rect_source, NULL);
- else
- glShaderSourceARB(triple->depth_fragment_shader, sizeof(depth_fragment_shader_source)/sizeof(GLcharARB*), depth_fragment_shader_source, NULL);
-
- glCompileShaderARB(triple->depth_fragment_shader);
-
- glGetObjectParameterivARB(triple->depth_fragment_shader, GL_OBJECT_COMPILE_STATUS_ARB, &success);
-
- /*if (!success)*/ {
- GLbyte infoLog[1000];
-
- glGetInfoLogARB(triple->depth_fragment_shader, 1000, &len, infoLog);
-
- //fprintf(stderr, "Error in fragment shader compilation!\n");
-
- if (len > 0)
- fprintf(stderr, "fragment:\n%s\n", infoLog);
- }
-
- triple->depth_program = glCreateProgramObjectARB();
-
- glAttachObjectARB(triple->depth_program, triple->depth_vertex_shader);
- glAttachObjectARB(triple->depth_program, triple->depth_fragment_shader);
-
- glLinkProgramARB(triple->depth_program);
-
- glGetObjectParameterivARB(triple->depth_program, GL_OBJECT_LINK_STATUS_ARB, &success);
-
- /*if (!success)*/ {
- GLbyte infoLog[1000];
-
- glGetInfoLogARB(triple->depth_program, 1000, &len, infoLog);
-
- //fprintf(stderr, "Error in linking!\n");
-
- if (len > 0)
- fprintf(stderr, "GLSL linking:\n%s\n", infoLog);
- }
- }
+ load_depth_shader_program(triple);
}
+ /* otherwise, we have to fall back to the more compatible glReadBuffer/glDrawBuffer method */
else {
const int count = win->sizex * win->sizey;
const int size = count*sizeof(GLfloat);
@@ -738,6 +747,7 @@
static void wm_triple_copy_depth_buffer(wmWindow *win, wmDrawTriple *triple)
{
+ /* To do this fast we need support for depth textures and GLSL */
if (GLEW_ARB_depth_texture &&
GLEW_ARB_shader_objects &&
GLEW_ARB_vertex_shader &&
@@ -746,6 +756,9 @@
{
int x, y, sizex, sizey, offx, offy;
+ /* XXX this is pretty much identical to wm_triple_copy_textures,
+ the fact that the textures are GL_DEPTH_COMPONENT format is what makes the difference */
+
for(y=0, offy=0; y<triple->depth_ny; offy+=triple->depth_y[y], y++) {
for(x=0, offx=0; x<triple->depth_nx; offx+=triple->depth_x[x], x++) {
sizex= (x == triple->depth_nx-1)? win->sizex-offx: triple->depth_x[x];
@@ -759,6 +772,10 @@
glBindTexture(triple->depth_target, 0);
}
else if (triple->depth) {
+ /* For the compatibility fallback, we set the pixel store state to the defaults,
+ anything else is very unlikely to be even remotely fast (and it is already slow
+ using these defaults) */
+
glPushAttrib(GL_PIXEL_MODE_BIT);
glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
@@ -778,6 +795,7 @@
static void wm_triple_draw_depth_buffer(wmWindow *win, wmDrawTriple *triple)
{
+ /* To do this fast we need support for depth textures and GLSL */
if (GLEW_ARB_depth_texture &&
GLEW_ARB_shader_objects &&
GLEW_ARB_vertex_shader &&
@@ -792,16 +810,23 @@
GL_COLOR_BUFFER_BIT|
GL_DEPTH_BUFFER_BIT);
+ /* depth test has to be enabled to write to depth buffer,
+ set GL_ALWAYS so that what is in the texture overwrites what
+ is there, and make sure the buffer is set to be writable */
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_TRUE);
+ /* since the fragment shader does not write gl_FragColor what it would
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list