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