b60dad7460198a7e6b10027714bae907c5a47474
[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/texenv.h"
36 #include "main/texobj.h"
37 #include "main/texstate.h"
38 #include "main/texparam.h"
39 #include "main/matrix.h"
40 #include "main/varray.h"
41 #include "main/attrib.h"
42 #include "main/enable.h"
43 #include "glapi/dispatch.h"
44 #include "swrast/swrast.h"
45
46 #include "intel_screen.h"
47 #include "intel_context.h"
48 #include "intel_batchbuffer.h"
49 #include "intel_blit.h"
50 #include "intel_buffers.h"
51 #include "intel_regions.h"
52 #include "intel_pixel.h"
53 #include "intel_buffer_objects.h"
54
55
56 static GLboolean
57 intel_texture_drawpixels(GLcontext * ctx,
58 GLint x, GLint y,
59 GLsizei width, GLsizei height,
60 GLenum format,
61 GLenum type,
62 const struct gl_pixelstore_attrib *unpack,
63 const GLvoid *pixels)
64 {
65 GLuint texname;
66 GLfloat vertices[4][4];
67 GLfloat texcoords[4][2];
68
69 /* We're going to mess with texturing with no regard to existing texture
70 * state, so if there is some set up we have to bail.
71 */
72 if (ctx->Texture._EnabledUnits != 0)
73 return GL_FALSE;
74
75 /* Can't do textured DrawPixels with a fragment program, unless we were
76 * to generate a new program that sampled our texture and put the results
77 * in the fragment color before the user's program started.
78 */
79 if (ctx->FragmentProgram.Enabled)
80 return GL_FALSE;
81
82 /* Don't even want to think about it */
83 if (format == GL_COLOR_INDEX)
84 return GL_FALSE;
85
86 /* We don't have a way to generate fragments with stencil values which *
87 * will set the resulting stencil value.
88 */
89 if (format == GL_STENCIL_INDEX)
90 return GL_FALSE;
91
92 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
93 * the color buffer, and sample the texture values into the fragment depth
94 * in a program.
95 */
96 if (format == GL_DEPTH_COMPONENT)
97 return GL_FALSE;
98
99 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
100 GL_CURRENT_BIT);
101 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
102
103 /* XXX: pixel store stuff */
104 _mesa_Disable(GL_POLYGON_STIPPLE);
105
106 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
107 _mesa_Enable(GL_TEXTURE_2D);
108 _mesa_GenTextures(1, &texname);
109 _mesa_BindTexture(GL_TEXTURE_2D, texname);
110 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
111 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
112 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
113 /*
114 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
115 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
116 */
117 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format,
118 type, pixels);
119
120 _mesa_MatrixMode(GL_PROJECTION);
121 _mesa_PushMatrix();
122 _mesa_LoadIdentity();
123 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
124
125 _mesa_MatrixMode(GL_MODELVIEW);
126 _mesa_PushMatrix();
127 _mesa_LoadIdentity();
128
129 vertices[0][0] = x;
130 vertices[0][1] = y;
131 vertices[0][2] = ctx->Current.RasterPos[2];
132 vertices[0][3] = ctx->Current.RasterPos[3];
133 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
134 vertices[1][1] = y;
135 vertices[1][2] = ctx->Current.RasterPos[2];
136 vertices[1][3] = ctx->Current.RasterPos[3];
137 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
138 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
139 vertices[2][2] = ctx->Current.RasterPos[2];
140 vertices[2][3] = ctx->Current.RasterPos[3];
141 vertices[3][0] = x;
142 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
143 vertices[3][2] = ctx->Current.RasterPos[2];
144 vertices[3][3] = ctx->Current.RasterPos[3];
145
146 texcoords[0][0] = 0.0;
147 texcoords[0][1] = 0.0;
148 texcoords[1][0] = 1.0;
149 texcoords[1][1] = 0.0;
150 texcoords[2][0] = 1.0;
151 texcoords[2][1] = 1.0;
152 texcoords[3][0] = 0.0;
153 texcoords[3][1] = 1.0;
154
155 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
156 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
157 _mesa_Enable(GL_VERTEX_ARRAY);
158 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
159 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
160
161 _mesa_MatrixMode(GL_PROJECTION);
162 _mesa_PopMatrix();
163 _mesa_MatrixMode(GL_MODELVIEW);
164 _mesa_PopMatrix();
165 _mesa_PopClientAttrib();
166 _mesa_PopAttrib();
167
168 _mesa_DeleteTextures(1, &texname);
169
170 return GL_TRUE;
171 }
172
173 void
174 intelDrawPixels(GLcontext * ctx,
175 GLint x, GLint y,
176 GLsizei width, GLsizei height,
177 GLenum format,
178 GLenum type,
179 const struct gl_pixelstore_attrib *unpack,
180 const GLvoid * pixels)
181 {
182 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
183 unpack, pixels))
184 return;
185
186 if (INTEL_DEBUG & DEBUG_PIXEL)
187 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
188
189 if (ctx->FragmentProgram._Current == ctx->FragmentProgram._TexEnvProgram) {
190 /*
191 * We don't want the i915 texenv program to be applied to DrawPixels.
192 * This is really just a performance optimization (mesa will other-
193 * wise happily run the fragment program on each pixel in the image).
194 */
195 struct gl_fragment_program *fpSave = ctx->FragmentProgram._Current;
196 /* can't just set current frag prog to 0 here as on buffer resize
197 we'll get new state checks which will segfault. Remains a hack. */
198 ctx->FragmentProgram._Current = NULL;
199 ctx->FragmentProgram._UseTexEnvProgram = GL_FALSE;
200 ctx->FragmentProgram._Active = GL_FALSE;
201 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
202 unpack, pixels );
203 ctx->FragmentProgram._Current = fpSave;
204 ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
205 ctx->FragmentProgram._Active = GL_TRUE;
206 _swrast_InvalidateState(ctx, _NEW_PROGRAM);
207 }
208 else {
209 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
210 unpack, pixels );
211 }
212 }