0ddc3aa30d8be11c516c6bc298bada9b2a89ad0b
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/mtypes.h"
32 #include "main/macros.h"
33 #include "main/bufferobj.h"
34 #include "main/teximage.h"
35 #include "main/texobj.h"
36 #include "main/texstate.h"
37 #include "main/matrix.h"
38 #include "main/varray.h"
39 #include "main/attrib.h"
40 #include "main/enable.h"
41 #include "glapi/dispatch.h"
42 #include "swrast/swrast.h"
43
44 #include "intel_screen.h"
45 #include "intel_context.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_blit.h"
48 #include "intel_buffers.h"
49 #include "intel_regions.h"
50 #include "intel_pixel.h"
51 #include "intel_buffer_objects.h"
52 #include "intel_tris.h"
53
54
55 static GLboolean
56 intel_texture_drawpixels(GLcontext * ctx,
57 GLint x, GLint y,
58 GLsizei width, GLsizei height,
59 GLenum format,
60 GLenum type,
61 const struct gl_pixelstore_attrib *unpack,
62 const GLvoid *pixels)
63 {
64 GLuint texname;
65 GLfloat vertices[4][4];
66 GLfloat texcoords[4][2];
67
68 /* We're going to mess with texturing with no regard to existing texture
69 * state, so if there is some set up we have to bail.
70 */
71 if (ctx->Texture._EnabledUnits != 0)
72 return GL_FALSE;
73
74 /* Can't do textured DrawPixels with a fragment program, unless we were
75 * to generate a new program that sampled our texture and put the results
76 * in the fragment color before the user's program started.
77 */
78 if (ctx->FragmentProgram.Enabled)
79 return GL_FALSE;
80
81 /* Don't even want to think about it */
82 if (format == GL_COLOR_INDEX)
83 return GL_FALSE;
84
85 /* We don't have a way to generate fragments with stencil values which *
86 * will set the resulting stencil value.
87 */
88 if (format == GL_STENCIL_INDEX)
89 return GL_FALSE;
90
91 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
92 * the color buffer, and sample the texture values into the fragment depth
93 * in a program.
94 */
95 if (format == GL_DEPTH_COMPONENT)
96 return GL_FALSE;
97
98 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
99 GL_CURRENT_BIT);
100 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
101
102 /* XXX: pixel store stuff */
103 _mesa_Disable(GL_POLYGON_STIPPLE);
104
105 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
106 _mesa_Enable(GL_TEXTURE_2D);
107 _mesa_GenTextures(1, &texname);
108 _mesa_BindTexture(GL_TEXTURE_2D, texname);
109 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
111 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
112 /*
113 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
114 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
115 */
116 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format,
117 type, pixels);
118
119 _mesa_MatrixMode(GL_PROJECTION);
120 _mesa_PushMatrix();
121 _mesa_LoadIdentity();
122 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
123
124 _mesa_MatrixMode(GL_MODELVIEW);
125 _mesa_PushMatrix();
126 _mesa_LoadIdentity();
127
128 vertices[0][0] = x;
129 vertices[0][1] = y;
130 vertices[0][2] = ctx->Current.RasterPos[2];
131 vertices[0][3] = ctx->Current.RasterPos[3];
132 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
133 vertices[1][1] = y;
134 vertices[1][2] = ctx->Current.RasterPos[2];
135 vertices[1][3] = ctx->Current.RasterPos[3];
136 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
137 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
138 vertices[2][2] = ctx->Current.RasterPos[2];
139 vertices[2][3] = ctx->Current.RasterPos[3];
140 vertices[3][0] = x;
141 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
142 vertices[3][2] = ctx->Current.RasterPos[2];
143 vertices[3][3] = ctx->Current.RasterPos[3];
144
145 texcoords[0][0] = 0.0;
146 texcoords[0][1] = 0.0;
147 texcoords[1][0] = 1.0;
148 texcoords[1][1] = 0.0;
149 texcoords[2][0] = 1.0;
150 texcoords[2][1] = 1.0;
151 texcoords[3][0] = 0.0;
152 texcoords[3][1] = 1.0;
153
154 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
155 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
156 _mesa_Enable(GL_VERTEX_ARRAY);
157 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
158 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
159
160 _mesa_MatrixMode(GL_PROJECTION);
161 _mesa_PopMatrix();
162 _mesa_MatrixMode(GL_MODELVIEW);
163 _mesa_PopMatrix();
164 _mesa_PopClientAttrib();
165 _mesa_PopAttrib();
166
167 _mesa_DeleteTextures(1, &texname);
168
169 return GL_TRUE;
170 }
171
172 void
173 intelDrawPixels(GLcontext * ctx,
174 GLint x, GLint y,
175 GLsizei width, GLsizei height,
176 GLenum format,
177 GLenum type,
178 const struct gl_pixelstore_attrib *unpack,
179 const GLvoid * pixels)
180 {
181 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
182 unpack, pixels))
183 return;
184
185 if (INTEL_DEBUG & DEBUG_PIXEL)
186 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
187
188 if (ctx->FragmentProgram._Current == ctx->FragmentProgram._TexEnvProgram) {
189 /*
190 * We don't want the i915 texenv program to be applied to DrawPixels.
191 * This is really just a performance optimization (mesa will other-
192 * wise happily run the fragment program on each pixel in the image).
193 */
194 struct gl_fragment_program *fpSave = ctx->FragmentProgram._Current;
195 /* can't just set current frag prog to 0 here as on buffer resize
196 we'll get new state checks which will segfault. Remains a hack. */
197 ctx->FragmentProgram._Current = NULL;
198 ctx->FragmentProgram._UseTexEnvProgram = GL_FALSE;
199 ctx->FragmentProgram._Active = GL_FALSE;
200 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
201 unpack, pixels );
202 ctx->FragmentProgram._Current = fpSave;
203 ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
204 ctx->FragmentProgram._Active = GL_TRUE;
205 _swrast_InvalidateState(ctx, _NEW_PROGRAM);
206 }
207 else {
208 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
209 unpack, pixels );
210 }
211 }