glsl: move to compiler/
[mesa.git] / src / mesa / drivers / dri / i965 / intel_pixel_bitmap.c
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portionsalloc
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "main/blend.h"
27 #include "main/enums.h"
28 #include "main/image.h"
29 #include "main/colormac.h"
30 #include "main/condrender.h"
31 #include "main/mtypes.h"
32 #include "main/macros.h"
33 #include "main/pbo.h"
34 #include "main/bufferobj.h"
35 #include "main/state.h"
36 #include "main/texobj.h"
37 #include "main/context.h"
38 #include "main/fbobject.h"
39 #include "swrast/swrast.h"
40 #include "drivers/common/meta.h"
41
42 #include "brw_context.h"
43 #include "intel_screen.h"
44 #include "intel_batchbuffer.h"
45 #include "intel_blit.h"
46 #include "intel_fbo.h"
47 #include "intel_image.h"
48 #include "intel_buffers.h"
49 #include "intel_pixel.h"
50 #include "intel_reg.h"
51
52
53 #define FILE_DEBUG_FLAG DEBUG_PIXEL
54
55
56 /* Unlike the other intel_pixel_* functions, the expectation here is
57 * that the incoming data is not in a PBO. With the XY_TEXT blit
58 * method, there's no benefit haveing it in a PBO, but we could
59 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
60 * PBO bitmaps. I think they are probably pretty rare though - I
61 * wonder if Xgl uses them?
62 */
63 static const GLubyte *map_pbo( struct gl_context *ctx,
64 GLsizei width, GLsizei height,
65 const struct gl_pixelstore_attrib *unpack,
66 const GLubyte *bitmap )
67 {
68 GLubyte *buf;
69
70 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
71 GL_COLOR_INDEX, GL_BITMAP,
72 INT_MAX, (const GLvoid *) bitmap)) {
73 _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
74 return NULL;
75 }
76
77 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0, unpack->BufferObj->Size,
78 GL_MAP_READ_BIT,
79 unpack->BufferObj,
80 MAP_INTERNAL);
81 if (!buf) {
82 _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
83 return NULL;
84 }
85
86 return ADD_POINTERS(buf, bitmap);
87 }
88
89 static bool test_bit( const GLubyte *src, GLuint bit )
90 {
91 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
92 }
93
94 static void set_bit( GLubyte *dest, GLuint bit )
95 {
96 dest[bit/8] |= 1 << (bit % 8);
97 }
98
99 /* Extract a rectangle's worth of data from the bitmap. Called
100 * per chunk of HW-sized bitmap.
101 */
102 static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
103 const struct gl_pixelstore_attrib *unpack,
104 const GLubyte *bitmap,
105 GLuint x, GLuint y,
106 GLuint w, GLuint h,
107 GLubyte *dest,
108 GLuint row_align,
109 bool invert)
110 {
111 GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
112 GLuint mask = unpack->LsbFirst ? 0 : 7;
113 GLuint bit = 0;
114 GLint row, col;
115 GLint first, last;
116 GLint incr;
117 GLuint count = 0;
118
119 DBG("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
120 __func__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
121
122 if (invert) {
123 first = h-1;
124 last = 0;
125 incr = -1;
126 }
127 else {
128 first = 0;
129 last = h-1;
130 incr = 1;
131 }
132
133 /* Require that dest be pre-zero'd.
134 */
135 for (row = first; row != (last+incr); row += incr) {
136 const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap,
137 width, height,
138 GL_COLOR_INDEX, GL_BITMAP,
139 y + row, x);
140
141 for (col = 0; col < w; col++, bit++) {
142 if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
143 set_bit(dest, bit ^ 7);
144 count++;
145 }
146 }
147
148 if (row_align)
149 bit = ALIGN(bit, row_align);
150 }
151
152 return count;
153 }
154
155 /**
156 * Returns the low Y value of the vertical range given, flipped according to
157 * whether the framebuffer is or not.
158 */
159 static inline int
160 y_flip(struct gl_framebuffer *fb, int y, int height)
161 {
162 if (_mesa_is_user_fbo(fb))
163 return y;
164 else
165 return fb->Height - y - height;
166 }
167
168 /*
169 * Render a bitmap.
170 */
171 static bool
172 do_blit_bitmap( struct gl_context *ctx,
173 GLint dstx, GLint dsty,
174 GLsizei width, GLsizei height,
175 const struct gl_pixelstore_attrib *unpack,
176 const GLubyte *bitmap )
177 {
178 struct brw_context *brw = brw_context(ctx);
179 struct gl_framebuffer *fb = ctx->DrawBuffer;
180 struct intel_renderbuffer *irb;
181 GLfloat tmpColor[4];
182 GLubyte ubcolor[4];
183 GLuint color;
184 GLsizei bitmap_width = width;
185 GLsizei bitmap_height = height;
186 GLint px, py;
187 GLuint stipple[32];
188 GLint orig_dstx = dstx;
189 GLint orig_dsty = dsty;
190
191 /* Update draw buffer bounds */
192 _mesa_update_state(ctx);
193
194 if (ctx->Depth.Test) {
195 /* The blit path produces incorrect results when depth testing is on.
196 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
197 * will likely be obscured by other, closer geometry.
198 */
199 return false;
200 }
201
202 intel_prepare_render(brw);
203
204 if (fb->_NumColorDrawBuffers != 1) {
205 perf_debug("accelerated glBitmap() only supports rendering to a "
206 "single color buffer\n");
207 return false;
208 }
209
210 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
211
212 if (_mesa_is_bufferobj(unpack->BufferObj)) {
213 bitmap = map_pbo(ctx, width, height, unpack, bitmap);
214 if (bitmap == NULL)
215 return true; /* even though this is an error, we're done */
216 }
217
218 COPY_4V(tmpColor, ctx->Current.RasterColor);
219
220 if (_mesa_need_secondary_color(ctx)) {
221 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
222 }
223
224 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
225 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
226 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
227 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
228
229 switch (_mesa_get_render_format(ctx, intel_rb_format(irb))) {
230 case MESA_FORMAT_B8G8R8A8_UNORM:
231 case MESA_FORMAT_B8G8R8X8_UNORM:
232 color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]);
233 break;
234 case MESA_FORMAT_B5G6R5_UNORM:
235 color = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]);
236 break;
237 default:
238 perf_debug("Unsupported format %s in accelerated glBitmap()\n",
239 _mesa_get_format_name(irb->mt->format));
240 return false;
241 }
242
243 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
244 return false;
245
246 /* Clip to buffer bounds and scissor. */
247 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
248 fb->_Xmax, fb->_Ymax,
249 &dstx, &dsty, &width, &height))
250 goto out;
251
252 dsty = y_flip(fb, dsty, height);
253
254 #define DY 32
255 #define DX 32
256
257 /* The blitter has no idea about fast color clears, so we need to resolve
258 * the miptree before we do anything.
259 */
260 intel_miptree_resolve_color(brw, irb->mt);
261
262 /* Chop it all into chunks that can be digested by hardware: */
263 for (py = 0; py < height; py += DY) {
264 for (px = 0; px < width; px += DX) {
265 int h = MIN2(DY, height - py);
266 int w = MIN2(DX, width - px);
267 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
268 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
269 ctx->Color.LogicOp : GL_COPY;
270
271 assert(sz <= sizeof(stipple));
272 memset(stipple, 0, sz);
273
274 /* May need to adjust this when padding has been introduced in
275 * sz above:
276 *
277 * Have to translate destination coordinates back into source
278 * coordinates.
279 */
280 int count = get_bitmap_rect(bitmap_width, bitmap_height, unpack,
281 bitmap,
282 -orig_dstx + (dstx + px),
283 -orig_dsty + y_flip(fb, dsty + py, h),
284 w, h,
285 (GLubyte *)stipple,
286 8,
287 _mesa_is_winsys_fbo(fb));
288 if (count == 0)
289 continue;
290
291 if (!intelEmitImmediateColorExpandBlit(brw,
292 irb->mt->cpp,
293 (GLubyte *)stipple,
294 sz,
295 color,
296 irb->mt->pitch,
297 irb->mt->bo,
298 0,
299 irb->mt->tiling,
300 dstx + px,
301 dsty + py,
302 w, h,
303 logic_op)) {
304 return false;
305 }
306
307 if (ctx->Query.CurrentOcclusionObject)
308 ctx->Query.CurrentOcclusionObject->Result += count;
309 }
310 }
311 out:
312
313 if (unlikely(INTEL_DEBUG & DEBUG_SYNC))
314 intel_batchbuffer_flush(brw);
315
316 if (_mesa_is_bufferobj(unpack->BufferObj)) {
317 /* done with PBO so unmap it now */
318 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj, MAP_INTERNAL);
319 }
320
321 return true;
322 }
323
324
325 /* There are a large number of possible ways to implement bitmap on
326 * this hardware, most of them have some sort of drawback. Here are a
327 * few that spring to mind:
328 *
329 * Blit:
330 * - XY_MONO_SRC_BLT_CMD
331 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
332 * - XY_TEXT_BLT
333 * - XY_TEXT_IMMEDIATE_BLT
334 * - blit per cliprect, subject to maximum immediate data size.
335 * - XY_COLOR_BLT
336 * - per pixel or run of pixels
337 * - XY_PIXEL_BLT
338 * - good for sparse bitmaps
339 *
340 * 3D engine:
341 * - Point per pixel
342 * - Translate bitmap to an alpha texture and render as a quad
343 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
344 */
345 void
346 intelBitmap(struct gl_context * ctx,
347 GLint x, GLint y,
348 GLsizei width, GLsizei height,
349 const struct gl_pixelstore_attrib *unpack,
350 const GLubyte * pixels)
351 {
352 if (!_mesa_check_conditional_render(ctx))
353 return;
354
355 if (do_blit_bitmap(ctx, x, y, width, height,
356 unpack, pixels))
357 return;
358
359 _mesa_meta_Bitmap(ctx, x, y, width, height, unpack, pixels);
360 }