Merge commit 'origin/7.8'
[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/teximage.h"
33 #include "main/texenv.h"
34 #include "main/texobj.h"
35 #include "main/texstate.h"
36 #include "main/texparam.h"
37 #include "main/varray.h"
38 #include "main/attrib.h"
39 #include "main/enable.h"
40 #include "main/buffers.h"
41 #include "main/fbobject.h"
42 #include "main/depth.h"
43 #include "main/hash.h"
44 #include "main/blend.h"
45 #include "swrast/swrast.h"
46 #include "drivers/common/meta.h"
47
48 #include "intel_context.h"
49 #include "intel_pixel.h"
50 #include "intel_fbo.h"
51
52
53 /** XXX compare perf of this vs. _mesa_meta_DrawPixels(STENCIL) */
54 static GLboolean
55 intel_stencil_drawpixels(GLcontext * ctx,
56 GLint x, GLint y,
57 GLsizei width, GLsizei height,
58 GLenum format,
59 GLenum type,
60 const struct gl_pixelstore_attrib *unpack,
61 const GLvoid *pixels)
62 {
63 struct intel_context *intel = intel_context(ctx);
64 GLuint texname, rb_name, fb_name, old_fb_name;
65 GLfloat vertices[4][2];
66 struct intel_renderbuffer *irb;
67 struct intel_renderbuffer *depth_irb;
68 struct gl_pixelstore_attrib old_unpack;
69 GLstencil *stencil_pixels;
70 int row, y1, y2;
71 GLint old_active_texture;
72 GLboolean rendering_to_fbo = ctx->DrawBuffer->Name != 0;
73
74 if (format != GL_STENCIL_INDEX)
75 return GL_FALSE;
76
77 /* If there's nothing to write, we're done. */
78 if (ctx->Stencil.WriteMask[0] == 0)
79 return GL_TRUE;
80
81 /* Can't do a per-bit writemask while treating stencil as rgba data. */
82 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
83 if (INTEL_DEBUG & DEBUG_FALLBACKS)
84 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
85 "stencil mask enabled\n");
86 return GL_FALSE;
87 }
88
89 /* We don't support stencil testing/ops here */
90 if (ctx->Stencil._Enabled)
91 return GL_FALSE;
92
93 /* We use FBOs for our wrapping of the depthbuffer into a color
94 * destination.
95 */
96 if (!ctx->Extensions.EXT_framebuffer_object)
97 return GL_FALSE;
98
99 /* We're going to mess with texturing with no regard to existing texture
100 * state, so if there is some set up we have to bail.
101 */
102 if (ctx->Texture._EnabledUnits != 0) {
103 if (INTEL_DEBUG & DEBUG_FALLBACKS)
104 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
105 "texturing enabled\n");
106 return GL_FALSE;
107 }
108
109 /* Can't do textured DrawPixels with a fragment program, unless we were
110 * to generate a new program that sampled our texture and put the results
111 * in the fragment color before the user's program started.
112 */
113 if (ctx->FragmentProgram.Enabled) {
114 if (INTEL_DEBUG & DEBUG_FALLBACKS)
115 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
116 "fragment program enabled\n");
117 return GL_FALSE;
118 }
119
120 /* Check that we can load in a texture this big. */
121 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
122 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
123 if (INTEL_DEBUG & DEBUG_FALLBACKS)
124 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
125 "bitmap too large (%dx%d)\n",
126 width, height);
127 return GL_FALSE;
128 }
129
130 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
131 (!is_power_of_two(width) || !is_power_of_two(height))) {
132 if (INTEL_DEBUG & DEBUG_FALLBACKS)
133 fprintf(stderr,
134 "glDrawPixels(GL_STENCIL_INDEX) fallback: NPOT texture\n");
135 return GL_FALSE;
136 }
137
138 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
139 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
140 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
141 old_fb_name = ctx->DrawBuffer->Name;
142 old_active_texture = ctx->Texture.CurrentUnit;
143
144 _mesa_Disable(GL_POLYGON_STIPPLE);
145 _mesa_Disable(GL_DEPTH_TEST);
146 _mesa_Disable(GL_STENCIL_TEST);
147
148 /* Unpack the supplied stencil values into a ubyte buffer. */
149 assert(sizeof(GLstencil) == sizeof(GLubyte));
150 stencil_pixels = malloc(width * height * sizeof(GLstencil));
151 for (row = 0; row < height; row++) {
152 GLvoid *source = _mesa_image_address2d(unpack, pixels,
153 width, height,
154 GL_COLOR_INDEX, type,
155 row, 0);
156 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE,
157 stencil_pixels +
158 row * width * sizeof(GLstencil),
159 type, source, unpack, ctx->_ImageTransferState);
160 }
161
162 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
163 * it which will be treated as GL_RGBA8 so we can render to it as a color
164 * buffer.
165 */
166 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
167 irb = intel_create_renderbuffer(MESA_FORMAT_ARGB8888);
168 irb->Base.Width = depth_irb->Base.Width;
169 irb->Base.Height = depth_irb->Base.Height;
170 intel_renderbuffer_set_region(irb, depth_irb->region);
171
172 /* Create a name for our renderbuffer, which lets us use other mesa
173 * rb functions for convenience.
174 */
175 _mesa_GenRenderbuffersEXT(1, &rb_name);
176 irb->Base.RefCount++;
177 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base);
178
179 /* Bind the new renderbuffer to the color attachment point. */
180 _mesa_GenFramebuffersEXT(1, &fb_name);
181 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
182 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
183 GL_COLOR_ATTACHMENT0_EXT,
184 GL_RENDERBUFFER_EXT,
185 rb_name);
186 /* Choose to render to the color attachment. */
187 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
188
189 _mesa_DepthMask(GL_FALSE);
190 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
191
192 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
193 _mesa_Enable(GL_TEXTURE_2D);
194 _mesa_GenTextures(1, &texname);
195 _mesa_BindTexture(GL_TEXTURE_2D, texname);
196 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
197 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
198 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
199 old_unpack = ctx->Unpack;
200 ctx->Unpack = ctx->DefaultPacking;
201 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0,
202 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels);
203 ctx->Unpack = old_unpack;
204 free(stencil_pixels);
205
206 meta_set_passthrough_transform(&intel->meta);
207
208 /* Since we're rendering to the framebuffer as if it was an FBO,
209 * if it's the window system we have to flip the coordinates.
210 */
211 if (rendering_to_fbo) {
212 y1 = y;
213 y2 = y + height * ctx->Pixel.ZoomY;
214 } else {
215 y1 = irb->Base.Height - (y + height * ctx->Pixel.ZoomY);
216 y2 = irb->Base.Height - y;
217 }
218 vertices[0][0] = x;
219 vertices[0][1] = y1;
220 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
221 vertices[1][1] = y1;
222 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
223 vertices[2][1] = y2;
224 vertices[3][0] = x;
225 vertices[3][1] = y2;
226
227 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
228 _mesa_Enable(GL_VERTEX_ARRAY);
229 meta_set_default_texrect(&intel->meta);
230
231 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
232
233 meta_restore_texcoords(&intel->meta);
234 meta_restore_transform(&intel->meta);
235
236 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
237 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name);
238
239 _mesa_PopClientAttrib();
240 _mesa_PopAttrib();
241
242 _mesa_DeleteTextures(1, &texname);
243 _mesa_DeleteFramebuffersEXT(1, &fb_name);
244 _mesa_DeleteRenderbuffersEXT(1, &rb_name);
245
246 return GL_TRUE;
247 }
248
249 void
250 intelDrawPixels(GLcontext * ctx,
251 GLint x, GLint y,
252 GLsizei width, GLsizei height,
253 GLenum format,
254 GLenum type,
255 const struct gl_pixelstore_attrib *unpack,
256 const GLvoid * pixels)
257 {
258 #if 0
259 /* XXX this function doesn't seem to work reliably even when all
260 * the pre-requisite conditions are met.
261 * Note that this function is never hit with conform.
262 * Fall back to swrast because even the _mesa_meta_DrawPixels() approach
263 * isn't working because of an apparent stencil bug.
264 */
265 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
266 unpack, pixels))
267 return;
268 #else
269 (void) intel_stencil_drawpixels; /* silence warning */
270 if (format == GL_STENCIL_INDEX) {
271 _swrast_DrawPixels(ctx, x, y, width, height, format, type,
272 unpack, pixels);
273 return;
274 }
275 #endif
276
277 _mesa_meta_DrawPixels(ctx, x, y, width, height, format, type,
278 unpack, pixels);
279 }