[Intel-gfx] [PATCH] intel: Add support for glDrawPixels(GL_DEPTH_COMPONENT)

Eric Anholt eric at anholt.net
Sat Nov 1 00:17:11 CET 2008


---
 src/mesa/drivers/dri/intel/intel_context.h    |    7 ++
 src/mesa/drivers/dri/intel/intel_pixel.c      |  120 ++++++++++++++++++++++
 src/mesa/drivers/dri/intel/intel_pixel.h      |   10 ++
 src/mesa/drivers/dri/intel/intel_pixel_draw.c |  135 ++++++++++++++++++-------
 4 files changed, 234 insertions(+), 38 deletions(-)

diff --git a/src/mesa/drivers/dri/intel/intel_context.h b/src/mesa/drivers/dri/intel/intel_context.h
index 3938af4..849a5b3 100644
--- a/src/mesa/drivers/dri/intel/intel_context.h
+++ b/src/mesa/drivers/dri/intel/intel_context.h
@@ -157,6 +157,13 @@ struct intel_context
       void (*debug_batch)(struct intel_context *intel);
    } vtbl;
 
+   struct {
+      struct gl_fragment_program *drawpixels_depth_fp;
+      struct gl_fragment_program *saved_fp;
+
+      GLboolean saved_fp_enable;
+   } meta;
+
    GLint refcount;
    GLuint Fallback;
    GLuint NewGLState;
diff --git a/src/mesa/drivers/dri/intel/intel_pixel.c b/src/mesa/drivers/dri/intel/intel_pixel.c
index b267ffd..cc1fd67 100644
--- a/src/mesa/drivers/dri/intel/intel_pixel.c
+++ b/src/mesa/drivers/dri/intel/intel_pixel.c
@@ -27,7 +27,14 @@
 
 #include "main/enums.h"
 #include "main/state.h"
+#include "main/context.h"
+#include "main/enable.h"
+#include "main/attrib.h"
+#include "main/varray.h"
+#include "glapi/dispatch.h"
 #include "swrast/swrast.h"
+#include "shader/arbprogram.h"
+#include "shader/program.h"
 
 #include "intel_context.h"
 #include "intel_pixel.h"
@@ -173,6 +180,110 @@ intel_check_blit_format(struct intel_region * region,
    return GL_FALSE;
 }
 
+void intel_meta_draw_texture_rect(GLcontext *ctx,
+				  GLfloat x, GLfloat y,
+				  GLfloat width, GLfloat height)
+{
+   GLfloat vertices[4][4];
+   GLfloat texcoords[4][2];
+
+   /* Create the vertex buffer based on the current raster pos.  The x and y
+    * we're handed are ctx->Current.RasterPos[0,1] rounded to integers,
+    * and width and height should reflect PixelZoom if appropriate (true for
+    * DrawPixels, false for Bitmap).
+    *
+    * We also apply the depth.  However, the W component is already multiplied
+    * into ctx->Current.RasterPos[0,1,2] and we can ignore it at this point.
+    */
+   vertices[0][0] = x;
+   vertices[0][1] = y;
+   vertices[0][2] = ctx->Current.RasterPos[2];
+   vertices[0][3] = 1.0;
+   vertices[1][0] = x + width;
+   vertices[1][1] = y;
+   vertices[1][2] = ctx->Current.RasterPos[2];
+   vertices[1][3] = 1.0;
+   vertices[2][0] = x + width;
+   vertices[2][1] = y + height;
+   vertices[2][2] = ctx->Current.RasterPos[2];
+   vertices[2][3] = 1.0;
+   vertices[3][0] = x;
+   vertices[3][1] = y + height;
+   vertices[3][2] = ctx->Current.RasterPos[2];
+   vertices[3][3] = 1.0;
+
+   texcoords[0][0] = 0.0;
+   texcoords[0][1] = 0.0;
+   texcoords[1][0] = 1.0;
+   texcoords[1][1] = 0.0;
+   texcoords[2][0] = 1.0;
+   texcoords[2][1] = 1.0;
+   texcoords[3][0] = 0.0;
+   texcoords[3][1] = 1.0;
+
+   _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
+
+   _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
+   _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
+   _mesa_Enable(GL_VERTEX_ARRAY);
+   _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
+   CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
+
+   _mesa_PopClientAttrib();
+}
+
+/**
+ * Binds the given program string to GL_FRAGMENT_PROGRAM_ARB, caching the
+ * program object.
+ */
+void
+intel_meta_set_fragment_program(struct intel_context *intel,
+				struct gl_fragment_program **prog,
+				const char *prog_string)
+{
+   GLcontext *ctx = &intel->ctx;
+   assert(intel->meta.saved_fp == NULL);
+
+   _mesa_reference_fragprog(ctx, &intel->meta.saved_fp,
+			    ctx->FragmentProgram.Current);
+   if (*prog == NULL) {
+      GLuint prog_name;
+      _mesa_GenPrograms(1, &prog_name);
+      _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, prog_name);
+      _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
+			     GL_PROGRAM_FORMAT_ASCII_ARB,
+			     strlen(prog_string), (const GLubyte *)prog_string);
+      _mesa_reference_fragprog(ctx, prog, ctx->FragmentProgram.Current);
+      /* Note that DeletePrograms unbinds the program on us */
+      _mesa_DeletePrograms(1, &prog_name);
+   }
+
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
+   _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, *prog);
+   ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, &((*prog)->Base));
+
+   intel->meta.saved_fp_enable = ctx->FragmentProgram.Enabled;
+   _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB);
+}
+
+/**
+ * Restores the previous fragment program after
+ * intel_meta_set_fragment_program()
+ */
+void
+intel_meta_restore_fragment_program(struct intel_context *intel)
+{
+   GLcontext *ctx = &intel->ctx;
+
+   _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
+			    intel->meta.saved_fp);
+   _mesa_reference_fragprog(ctx, &intel->meta.saved_fp, NULL);
+   ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
+			   &ctx->FragmentProgram.Current->Base);
+
+   if (!intel->meta.saved_fp_enable)
+      _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB);
+}
 
 void
 intelInitPixelFuncs(struct dd_function_table *functions)
@@ -187,3 +298,12 @@ intelInitPixelFuncs(struct dd_function_table *functions)
 #endif
    }
 }
+
+void
+intel_free_pixel_state(struct intel_context *intel)
+{
+   GLcontext *ctx = &intel->ctx;
+
+   _mesa_reference_fragprog(ctx, &intel->meta.drawpixels_depth_fp, NULL);
+}
+
diff --git a/src/mesa/drivers/dri/intel/intel_pixel.h b/src/mesa/drivers/dri/intel/intel_pixel.h
index 6fa6eff..ee5b927 100644
--- a/src/mesa/drivers/dri/intel/intel_pixel.h
+++ b/src/mesa/drivers/dri/intel/intel_pixel.h
@@ -32,6 +32,16 @@
 
 void intelInitPixelFuncs(struct dd_function_table *functions);
 
+void intel_meta_restore_fragment_program(struct intel_context *intel);
+void intel_meta_set_fragment_program(struct intel_context *intel,
+				     struct gl_fragment_program **prog,
+				     const char *prog_string);
+void intel_meta_draw_texture_rect(GLcontext *ctx,
+				  GLfloat x, GLfloat y,
+				  GLfloat width, GLfloat height);
+
+void intel_free_pixel_state(struct intel_context *intel);
+
 GLboolean intel_check_blit_fragment_ops(GLcontext * ctx,
 					GLboolean src_alpha_is_one);
 
diff --git a/src/mesa/drivers/dri/intel/intel_pixel_draw.c b/src/mesa/drivers/dri/intel/intel_pixel_draw.c
index 8ebbc95..ca859a6 100644
--- a/src/mesa/drivers/dri/intel/intel_pixel_draw.c
+++ b/src/mesa/drivers/dri/intel/intel_pixel_draw.c
@@ -69,8 +69,7 @@ intel_texture_drawpixels(GLcontext * ctx,
 			 const GLvoid *pixels)
 {
    GLuint texname;
-   GLfloat vertices[4][4];
-   GLfloat texcoords[4][2];
+   GLint saved_active_texture;
 
    /* We're going to mess with texturing with no regard to existing texture
     * state, so if there is some set up we have to bail.
@@ -119,11 +118,11 @@ intel_texture_drawpixels(GLcontext * ctx,
 
    _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
 		    GL_CURRENT_BIT);
-   _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
 
    /* XXX: pixel store stuff */
    _mesa_Disable(GL_POLYGON_STIPPLE);
 
+   saved_active_texture = ctx->Texture.CurrentUnit;
    _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
    _mesa_Enable(GL_TEXTURE_2D);
    _mesa_GenTextures(1, &texname);
@@ -147,48 +146,104 @@ intel_texture_drawpixels(GLcontext * ctx,
    _mesa_PushMatrix();
    _mesa_LoadIdentity();
 
-   /* Create the vertex buffer based on the current raster pos.  The x and y
-    * we're handed are ctx->Current.RasterPos[0,1] rounded to integers.
-    * We also apply the depth.  However, the W component is already multiplied
-    * into ctx->Current.RasterPos[0,1,2] and we can ignore it at this point.
-    */
-   vertices[0][0] = x;
-   vertices[0][1] = y;
-   vertices[0][2] = ctx->Current.RasterPos[2];
-   vertices[0][3] = 1.0;
-   vertices[1][0] = x + width * ctx->Pixel.ZoomX;
-   vertices[1][1] = y;
-   vertices[1][2] = ctx->Current.RasterPos[2];
-   vertices[1][3] = 1.0;
-   vertices[2][0] = x + width * ctx->Pixel.ZoomX;
-   vertices[2][1] = y + height * ctx->Pixel.ZoomY;
-   vertices[2][2] = ctx->Current.RasterPos[2];
-   vertices[2][3] = 1.0;
-   vertices[3][0] = x;
-   vertices[3][1] = y + height * ctx->Pixel.ZoomY;
-   vertices[3][2] = ctx->Current.RasterPos[2];
-   vertices[3][3] = 1.0;
+   intel_meta_draw_texture_rect(ctx,
+				x,
+				y,
+				width * ctx->Pixel.ZoomX,
+				height * ctx->Pixel.ZoomY);
 
-   texcoords[0][0] = 0.0;
-   texcoords[0][1] = 0.0;
-   texcoords[1][0] = 1.0;
-   texcoords[1][1] = 0.0;
-   texcoords[2][0] = 1.0;
-   texcoords[2][1] = 1.0;
-   texcoords[3][0] = 0.0;
-   texcoords[3][1] = 1.0;
+   _mesa_MatrixMode(GL_PROJECTION);
+   _mesa_PopMatrix();
+   _mesa_MatrixMode(GL_MODELVIEW);
+   _mesa_PopMatrix();
+   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + saved_active_texture);
+   _mesa_PopAttrib();
 
-   _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
-   _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
-   _mesa_Enable(GL_VERTEX_ARRAY);
-   _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
-   CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
+   _mesa_DeleteTextures(1, &texname);
+
+   return GL_TRUE;
+}
+
+static GLboolean
+intel_depth_drawpixels(GLcontext * ctx,
+			 GLint x, GLint y,
+			 GLsizei width, GLsizei height,
+			 GLenum format,
+			 GLenum type,
+			 const struct gl_pixelstore_attrib *unpack,
+			 const GLvoid *pixels)
+{
+   struct intel_context *intel = intel_context(ctx);
+   static const char *fp =
+      "!!ARBfp1.0\n"
+      "TEMP depth;\n"
+      "TEX depth, fragment.texcoord[0], texture[0], 2D;\n"
+      "MOV result.depth, depth.rrrr;\n"
+      "END\n";
+   GLuint texname;
+   GLint saved_active_texture;
+
+   if (format != GL_DEPTH_COMPONENT)
+      return GL_FALSE;
+
+   /* Check that we can load in a texture this big. */
+   if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
+       height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
+      if (INTEL_DEBUG & DEBUG_FALLBACKS)
+	 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
+		 "bitmap too large (%dx%d)\n",
+		 width, height);
+      return GL_FALSE;
+   }
+
+   _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
+		    GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+   _mesa_Disable(GL_POLYGON_STIPPLE);
+   _mesa_Disable(GL_STENCIL_TEST);
+
+   /* Enable writing to depth but not color */
+   _mesa_DepthMask(GL_TRUE);
+   _mesa_DepthFunc(GL_ALWAYS);
+   _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+
+   /* Load the texture data to a depth_component texture */
+   saved_active_texture = ctx->Texture.CurrentUnit;
+   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
+   _mesa_Enable(GL_TEXTURE_2D);
+   _mesa_GenTextures(1, &texname);
+   _mesa_BindTexture(GL_TEXTURE_2D, texname);
+   _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+   _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+   _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+   _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0,
+		    GL_DEPTH_COMPONENT, type, pixels);
+
+   intel_meta_set_fragment_program(intel, &intel->meta.drawpixels_depth_fp, fp);
+
+   _mesa_MatrixMode(GL_PROJECTION);
+   _mesa_PushMatrix();
+   _mesa_LoadIdentity();
+   _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
+
+   _mesa_MatrixMode(GL_MODELVIEW);
+   _mesa_PushMatrix();
+   _mesa_LoadIdentity();
+
+   intel_meta_draw_texture_rect(ctx,
+				x,
+				y,
+				width * ctx->Pixel.ZoomX,
+				height * ctx->Pixel.ZoomY);
+
+
+   intel_meta_restore_fragment_program(intel);
 
    _mesa_MatrixMode(GL_PROJECTION);
    _mesa_PopMatrix();
    _mesa_MatrixMode(GL_MODELVIEW);
    _mesa_PopMatrix();
-   _mesa_PopClientAttrib();
+   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + saved_active_texture);
    _mesa_PopAttrib();
 
    _mesa_DeleteTextures(1, &texname);
@@ -397,6 +452,10 @@ intelDrawPixels(GLcontext * ctx,
 				unpack, pixels))
       return;
 
+   if (intel_depth_drawpixels(ctx, x, y, width, height, format, type,
+			      unpack, pixels))
+      return;
+
    if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
 				unpack, pixels))
       return;
-- 
1.5.6.5




More information about the Intel-gfx mailing list