Merge commit 'origin/master' into i965g-restart
[mesa.git] / src / mesa / state_tracker / st_cb_bitmap.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 portions
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 /*
29 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/bufferobj.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "shader/program.h"
39 #include "shader/prog_parameter.h"
40 #include "shader/prog_print.h"
41
42 #include "st_context.h"
43 #include "st_atom.h"
44 #include "st_atom_constbuf.h"
45 #include "st_program.h"
46 #include "st_cb_bitmap.h"
47 #include "st_cb_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_texture.h"
50 #include "st_inlines.h"
51
52 #include "pipe/p_context.h"
53 #include "pipe/p_defines.h"
54 #include "pipe/p_inlines.h"
55 #include "util/u_tile.h"
56 #include "util/u_draw_quad.h"
57 #include "util/u_simple_shaders.h"
58 #include "shader/prog_instruction.h"
59 #include "cso_cache/cso_context.h"
60
61
62
63 /**
64 * glBitmaps are drawn as textured quads. The user's bitmap pattern
65 * is stored in a texture image. An alpha8 texture format is used.
66 * The fragment shader samples a bit (texel) from the texture, then
67 * discards the fragment if the bit is off.
68 *
69 * Note that we actually store the inverse image of the bitmap to
70 * simplify the fragment program. An "on" bit gets stored as texel=0x0
71 * and an "off" bit is stored as texel=0xff. Then we kill the
72 * fragment if the negated texel value is less than zero.
73 */
74
75
76 /**
77 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
78 * buffer which is then rendered en mass upon a flush, state change, etc.
79 * A wide, short buffer is used to target the common case of a series
80 * of glBitmap calls being used to draw text.
81 */
82 static GLboolean UseBitmapCache = GL_TRUE;
83
84
85 #define BITMAP_CACHE_WIDTH 512
86 #define BITMAP_CACHE_HEIGHT 32
87
88 struct bitmap_cache
89 {
90 /** Window pos to render the cached image */
91 GLint xpos, ypos;
92 /** Bounds of region used in window coords */
93 GLint xmin, ymin, xmax, ymax;
94
95 GLfloat color[4];
96
97 /** Bitmap's Z position */
98 GLfloat zpos;
99
100 struct pipe_texture *texture;
101 struct pipe_transfer *trans;
102
103 GLboolean empty;
104
105 /** An I8 texture image: */
106 ubyte *buffer;
107 };
108
109
110 /** Epsilon for Z comparisons */
111 #define Z_EPSILON 1e-06
112
113
114 /**
115 * Make fragment program for glBitmap:
116 * Sample the texture and kill the fragment if the bit is 0.
117 * This program will be combined with the user's fragment program.
118 */
119 static struct st_fragment_program *
120 make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
121 {
122 struct st_fragment_program *stfp;
123 struct gl_program *p;
124 GLuint ic = 0;
125
126 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
127 if (!p)
128 return NULL;
129
130 p->NumInstructions = 3;
131
132 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
133 if (!p->Instructions) {
134 ctx->Driver.DeleteProgram(ctx, p);
135 return NULL;
136 }
137 _mesa_init_instructions(p->Instructions, p->NumInstructions);
138
139 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
140 p->Instructions[ic].Opcode = OPCODE_TEX;
141 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
142 p->Instructions[ic].DstReg.Index = 0;
143 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
144 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
145 p->Instructions[ic].TexSrcUnit = samplerIndex;
146 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
147 ic++;
148
149 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
150 p->Instructions[ic].Opcode = OPCODE_KIL;
151 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
152
153 if (ctx->st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM)
154 p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_XXXX;
155
156 p->Instructions[ic].SrcReg[0].Index = 0;
157 p->Instructions[ic].SrcReg[0].Negate = NEGATE_XYZW;
158 ic++;
159
160 /* END; */
161 p->Instructions[ic++].Opcode = OPCODE_END;
162
163 assert(ic == p->NumInstructions);
164
165 p->InputsRead = FRAG_BIT_TEX0;
166 p->OutputsWritten = 0x0;
167 p->SamplersUsed = (1 << samplerIndex);
168
169 stfp = (struct st_fragment_program *) p;
170 stfp->Base.UsesKill = GL_TRUE;
171
172 return stfp;
173 }
174
175
176 static int
177 find_free_bit(uint bitfield)
178 {
179 int i;
180 for (i = 0; i < 32; i++) {
181 if ((bitfield & (1 << i)) == 0) {
182 return i;
183 }
184 }
185 return -1;
186 }
187
188
189 /**
190 * Combine basic bitmap fragment program with the user-defined program.
191 */
192 static struct st_fragment_program *
193 combined_bitmap_fragment_program(GLcontext *ctx)
194 {
195 struct st_context *st = ctx->st;
196 struct st_fragment_program *stfp = st->fp;
197
198 if (!stfp->bitmap_program) {
199 /*
200 * Generate new program which is the user-defined program prefixed
201 * with the bitmap sampler/kill instructions.
202 */
203 struct st_fragment_program *bitmap_prog;
204 uint sampler;
205
206 sampler = find_free_bit(st->fp->Base.Base.SamplersUsed);
207 bitmap_prog = make_bitmap_fragment_program(ctx, sampler);
208
209 stfp->bitmap_program = (struct st_fragment_program *)
210 _mesa_combine_programs(ctx,
211 &bitmap_prog->Base.Base, &stfp->Base.Base);
212 stfp->bitmap_program->bitmap_sampler = sampler;
213
214 /* done with this after combining */
215 st_reference_fragprog(st, &bitmap_prog, NULL);
216
217 #if 0
218 {
219 struct gl_program *p = &stfp->bitmap_program->Base.Base;
220 printf("Combined bitmap program:\n");
221 _mesa_print_program(p);
222 printf("InputsRead: 0x%x\n", p->InputsRead);
223 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
224 _mesa_print_parameter_list(p->Parameters);
225 }
226 #endif
227
228 /* translate to TGSI tokens */
229 st_translate_fragment_program(st, stfp->bitmap_program, NULL);
230 }
231
232 return stfp->bitmap_program;
233 }
234
235
236 /**
237 * Copy user-provide bitmap bits into texture buffer, expanding
238 * bits into texels.
239 * "On" bits will set texels to 0x0.
240 * "Off" bits will not modify texels.
241 * Note that the image is actually going to be upside down in
242 * the texture. We deal with that with texcoords.
243 */
244 static void
245 unpack_bitmap(struct st_context *st,
246 GLint px, GLint py, GLsizei width, GLsizei height,
247 const struct gl_pixelstore_attrib *unpack,
248 const GLubyte *bitmap,
249 ubyte *destBuffer, uint destStride)
250 {
251 destBuffer += py * destStride + px;
252
253 _mesa_expand_bitmap(width, height, unpack, bitmap,
254 destBuffer, destStride, 0x0);
255 }
256
257
258 /**
259 * Create a texture which represents a bitmap image.
260 */
261 static struct pipe_texture *
262 make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
263 const struct gl_pixelstore_attrib *unpack,
264 const GLubyte *bitmap)
265 {
266 struct pipe_context *pipe = ctx->st->pipe;
267 struct pipe_screen *screen = pipe->screen;
268 struct pipe_transfer *transfer;
269 ubyte *dest;
270 struct pipe_texture *pt;
271
272 /* PBO source... */
273 bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
274 if (!bitmap) {
275 return NULL;
276 }
277
278 /**
279 * Create texture to hold bitmap pattern.
280 */
281 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, ctx->st->bitmap.tex_format,
282 0, width, height, 1,
283 PIPE_TEXTURE_USAGE_SAMPLER);
284 if (!pt) {
285 _mesa_unmap_pbo_source(ctx, unpack);
286 return NULL;
287 }
288
289 transfer = st_no_flush_get_tex_transfer(st_context(ctx), pt, 0, 0, 0,
290 PIPE_TRANSFER_WRITE,
291 0, 0, width, height);
292
293 dest = screen->transfer_map(screen, transfer);
294
295 /* Put image into texture transfer */
296 memset(dest, 0xff, height * transfer->stride);
297 unpack_bitmap(ctx->st, 0, 0, width, height, unpack, bitmap,
298 dest, transfer->stride);
299
300 _mesa_unmap_pbo_source(ctx, unpack);
301
302 /* Release transfer */
303 screen->transfer_unmap(screen, transfer);
304 screen->tex_transfer_destroy(transfer);
305
306 return pt;
307 }
308
309 static GLuint
310 setup_bitmap_vertex_data(struct st_context *st,
311 int x, int y, int width, int height,
312 float z, const float color[4])
313 {
314 struct pipe_context *pipe = st->pipe;
315 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
316 const GLfloat fb_width = (GLfloat)fb->Width;
317 const GLfloat fb_height = (GLfloat)fb->Height;
318 const GLfloat x0 = (GLfloat)x;
319 const GLfloat x1 = (GLfloat)(x + width);
320 const GLfloat y0 = (GLfloat)y;
321 const GLfloat y1 = (GLfloat)(y + height);
322 const GLfloat sLeft = (GLfloat)0.0, sRight = (GLfloat)1.0;
323 const GLfloat tTop = (GLfloat)0.0, tBot = (GLfloat)1.0 - tTop;
324 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
325 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
326 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
327 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
328
329 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
330 * no_flush) updates to buffers where we know there is no conflict
331 * with previous data. Currently using max_slots > 1 will cause
332 * synchronous rendering if the driver flushes its command buffers
333 * between one bitmap and the next. Our flush hook below isn't
334 * sufficient to catch this as the driver doesn't tell us when it
335 * flushes its own command buffers. Until this gets fixed, pay the
336 * price of allocating a new buffer for each bitmap cache-flush to
337 * avoid synchronous rendering.
338 */
339 const GLuint max_slots = 1; /* 4096 / sizeof(st->bitmap.vertices); */
340 GLuint i;
341
342 if (st->bitmap.vbuf_slot >= max_slots) {
343 pipe_buffer_reference(&st->bitmap.vbuf, NULL);
344 st->bitmap.vbuf_slot = 0;
345 }
346
347 if (!st->bitmap.vbuf) {
348 st->bitmap.vbuf = pipe_buffer_create(pipe->screen, 32,
349 PIPE_BUFFER_USAGE_VERTEX,
350 max_slots * sizeof(st->bitmap.vertices));
351 }
352
353 /* Positions are in clip coords since we need to do clipping in case
354 * the bitmap quad goes beyond the window bounds.
355 */
356 st->bitmap.vertices[0][0][0] = clip_x0;
357 st->bitmap.vertices[0][0][1] = clip_y0;
358 st->bitmap.vertices[0][2][0] = sLeft;
359 st->bitmap.vertices[0][2][1] = tTop;
360
361 st->bitmap.vertices[1][0][0] = clip_x1;
362 st->bitmap.vertices[1][0][1] = clip_y0;
363 st->bitmap.vertices[1][2][0] = sRight;
364 st->bitmap.vertices[1][2][1] = tTop;
365
366 st->bitmap.vertices[2][0][0] = clip_x1;
367 st->bitmap.vertices[2][0][1] = clip_y1;
368 st->bitmap.vertices[2][2][0] = sRight;
369 st->bitmap.vertices[2][2][1] = tBot;
370
371 st->bitmap.vertices[3][0][0] = clip_x0;
372 st->bitmap.vertices[3][0][1] = clip_y1;
373 st->bitmap.vertices[3][2][0] = sLeft;
374 st->bitmap.vertices[3][2][1] = tBot;
375
376 /* same for all verts: */
377 for (i = 0; i < 4; i++) {
378 st->bitmap.vertices[i][0][2] = z;
379 st->bitmap.vertices[i][0][3] = 1.0;
380 st->bitmap.vertices[i][1][0] = color[0];
381 st->bitmap.vertices[i][1][1] = color[1];
382 st->bitmap.vertices[i][1][2] = color[2];
383 st->bitmap.vertices[i][1][3] = color[3];
384 st->bitmap.vertices[i][2][2] = 0.0; /*R*/
385 st->bitmap.vertices[i][2][3] = 1.0; /*Q*/
386 }
387
388 /* put vertex data into vbuf */
389 st_no_flush_pipe_buffer_write(st,
390 st->bitmap.vbuf,
391 st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
392 sizeof st->bitmap.vertices,
393 st->bitmap.vertices);
394
395 return st->bitmap.vbuf_slot++ * sizeof st->bitmap.vertices;
396 }
397
398
399
400 /**
401 * Render a glBitmap by drawing a textured quad
402 */
403 static void
404 draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
405 GLsizei width, GLsizei height,
406 struct pipe_texture *pt,
407 const GLfloat *color)
408 {
409 struct st_context *st = ctx->st;
410 struct pipe_context *pipe = ctx->st->pipe;
411 struct cso_context *cso = ctx->st->cso_context;
412 struct st_fragment_program *stfp;
413 GLuint maxSize;
414 GLuint offset;
415
416 stfp = combined_bitmap_fragment_program(ctx);
417
418 /* As an optimization, Mesa's fragment programs will sometimes get the
419 * primary color from a statevar/constant rather than a varying variable.
420 * when that's the case, we need to ensure that we use the 'color'
421 * parameter and not the current attribute color (which may have changed
422 * through glRasterPos and state validation.
423 * So, we force the proper color here. Not elegant, but it works.
424 */
425 {
426 GLfloat colorSave[4];
427 COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
428 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
429 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
430 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
431 }
432
433
434 /* limit checks */
435 /* XXX if the bitmap is larger than the max texture size, break
436 * it up into chunks.
437 */
438 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
439 assert(width <= (GLsizei)maxSize);
440 assert(height <= (GLsizei)maxSize);
441
442 cso_save_rasterizer(cso);
443 cso_save_samplers(cso);
444 cso_save_sampler_textures(cso);
445 cso_save_viewport(cso);
446 cso_save_fragment_shader(cso);
447 cso_save_vertex_shader(cso);
448
449 /* rasterizer state: just scissor */
450 st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
451 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
452
453 /* fragment shader state: TEX lookup program */
454 cso_set_fragment_shader_handle(cso, stfp->driver_shader);
455
456 /* vertex shader state: position + texcoord pass-through */
457 cso_set_vertex_shader_handle(cso, st->bitmap.vs);
458
459 /* user samplers, plus our bitmap sampler */
460 {
461 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
462 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_samplers);
463 uint i;
464 for (i = 0; i < st->state.num_samplers; i++) {
465 samplers[i] = &st->state.samplers[i];
466 }
467 samplers[stfp->bitmap_sampler] = &st->bitmap.sampler;
468 cso_set_samplers(cso, num, (const struct pipe_sampler_state **) samplers);
469 }
470
471 /* user textures, plus the bitmap texture */
472 {
473 struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
474 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_textures);
475 memcpy(textures, st->state.sampler_texture, sizeof(textures));
476 textures[stfp->bitmap_sampler] = pt;
477 cso_set_sampler_textures(cso, num, textures);
478 }
479
480 /* viewport state: viewport matching window dims */
481 {
482 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
483 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
484 const GLfloat width = (GLfloat)fb->Width;
485 const GLfloat height = (GLfloat)fb->Height;
486 struct pipe_viewport_state vp;
487 vp.scale[0] = 0.5f * width;
488 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
489 vp.scale[2] = 1.0f;
490 vp.scale[3] = 1.0f;
491 vp.translate[0] = 0.5f * width;
492 vp.translate[1] = 0.5f * height;
493 vp.translate[2] = 0.0f;
494 vp.translate[3] = 0.0f;
495 cso_set_viewport(cso, &vp);
496 }
497
498 /* draw textured quad */
499 offset = setup_bitmap_vertex_data(st, x, y, width, height, z, color);
500
501 util_draw_vertex_buffer(pipe, st->bitmap.vbuf, offset,
502 PIPE_PRIM_TRIANGLE_FAN,
503 4, /* verts */
504 3); /* attribs/vert */
505
506
507 /* restore state */
508 cso_restore_rasterizer(cso);
509 cso_restore_samplers(cso);
510 cso_restore_sampler_textures(cso);
511 cso_restore_viewport(cso);
512 cso_restore_fragment_shader(cso);
513 cso_restore_vertex_shader(cso);
514 }
515
516
517 static void
518 reset_cache(struct st_context *st)
519 {
520 struct pipe_context *pipe = st->pipe;
521 struct pipe_screen *screen = pipe->screen;
522 struct bitmap_cache *cache = st->bitmap.cache;
523
524 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
525 cache->empty = GL_TRUE;
526
527 cache->xmin = 1000000;
528 cache->xmax = -1000000;
529 cache->ymin = 1000000;
530 cache->ymax = -1000000;
531
532 if (cache->trans) {
533 screen->tex_transfer_destroy(cache->trans);
534 cache->trans = NULL;
535 }
536
537 assert(!cache->texture);
538
539 /* allocate a new texture */
540 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
541 st->bitmap.tex_format, 0,
542 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
543 1, PIPE_TEXTURE_USAGE_SAMPLER);
544
545 }
546
547 static void
548 create_cache_trans(struct st_context *st)
549 {
550 struct pipe_context *pipe = st->pipe;
551 struct pipe_screen *screen = pipe->screen;
552 struct bitmap_cache *cache = st->bitmap.cache;
553
554 if (cache->trans)
555 return;
556
557 /* Map the texture transfer.
558 * Subsequent glBitmap calls will write into the texture image.
559 */
560 cache->trans = st_no_flush_get_tex_transfer(st, cache->texture, 0, 0, 0,
561 PIPE_TRANSFER_WRITE, 0, 0,
562 BITMAP_CACHE_WIDTH,
563 BITMAP_CACHE_HEIGHT);
564 cache->buffer = screen->transfer_map(screen, cache->trans);
565
566 /* init image to all 0xff */
567 memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
568 }
569
570
571 /**
572 * If there's anything in the bitmap cache, draw/flush it now.
573 */
574 void
575 st_flush_bitmap_cache(struct st_context *st)
576 {
577 if (!st->bitmap.cache->empty) {
578 struct bitmap_cache *cache = st->bitmap.cache;
579
580 if (st->ctx->DrawBuffer) {
581 struct pipe_context *pipe = st->pipe;
582 struct pipe_screen *screen = pipe->screen;
583
584 assert(cache->xmin <= cache->xmax);
585
586 /* printf("flush size %d x %d at %d, %d\n",
587 cache->xmax - cache->xmin,
588 cache->ymax - cache->ymin,
589 cache->xpos, cache->ypos);
590 */
591
592 /* The texture transfer has been mapped until now.
593 * So unmap and release the texture transfer before drawing.
594 */
595 if (cache->trans) {
596 screen->transfer_unmap(screen, cache->trans);
597 cache->buffer = NULL;
598
599 screen->tex_transfer_destroy(cache->trans);
600 cache->trans = NULL;
601 }
602
603 draw_bitmap_quad(st->ctx,
604 cache->xpos,
605 cache->ypos,
606 cache->zpos,
607 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
608 cache->texture,
609 cache->color);
610 }
611
612 /* release/free the texture */
613 pipe_texture_reference(&cache->texture, NULL);
614
615 reset_cache(st);
616 }
617 }
618
619 /* Flush bitmap cache and release vertex buffer.
620 */
621 void
622 st_flush_bitmap( struct st_context *st )
623 {
624 st_flush_bitmap_cache(st);
625
626 /* Release vertex buffer to avoid synchronous rendering if we were
627 * to map it in the next frame.
628 */
629 pipe_buffer_reference(&st->bitmap.vbuf, NULL);
630 st->bitmap.vbuf_slot = 0;
631 }
632
633
634 /**
635 * Try to accumulate this glBitmap call in the bitmap cache.
636 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
637 */
638 static GLboolean
639 accum_bitmap(struct st_context *st,
640 GLint x, GLint y, GLsizei width, GLsizei height,
641 const struct gl_pixelstore_attrib *unpack,
642 const GLubyte *bitmap )
643 {
644 struct bitmap_cache *cache = st->bitmap.cache;
645 int px = -999, py;
646 const GLfloat z = st->ctx->Current.RasterPos[2];
647
648 if (width > BITMAP_CACHE_WIDTH ||
649 height > BITMAP_CACHE_HEIGHT)
650 return GL_FALSE; /* too big to cache */
651
652 if (!cache->empty) {
653 px = x - cache->xpos; /* pos in buffer */
654 py = y - cache->ypos;
655 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
656 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
657 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
658 ((fabs(z - cache->zpos) > Z_EPSILON))) {
659 /* This bitmap would extend beyond cache bounds, or the bitmap
660 * color is changing
661 * so flush and continue.
662 */
663 st_flush_bitmap_cache(st);
664 }
665 }
666
667 if (cache->empty) {
668 /* Initialize. Center bitmap vertically in the buffer. */
669 px = 0;
670 py = (BITMAP_CACHE_HEIGHT - height) / 2;
671 cache->xpos = x;
672 cache->ypos = y - py;
673 cache->zpos = z;
674 cache->empty = GL_FALSE;
675 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
676 }
677
678 assert(px != -999);
679
680 if (x < cache->xmin)
681 cache->xmin = x;
682 if (y < cache->ymin)
683 cache->ymin = y;
684 if (x + width > cache->xmax)
685 cache->xmax = x + width;
686 if (y + height > cache->ymax)
687 cache->ymax = y + height;
688
689 /* create the transfer if needed */
690 create_cache_trans(st);
691
692 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
693 cache->buffer, BITMAP_CACHE_WIDTH);
694
695 return GL_TRUE; /* accumulated */
696 }
697
698
699
700 /**
701 * Called via ctx->Driver.Bitmap()
702 */
703 static void
704 st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
705 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
706 {
707 struct st_context *st = ctx->st;
708 struct pipe_texture *pt;
709
710 if (width == 0 || height == 0)
711 return;
712
713 st_validate_state(st);
714
715 if (!st->bitmap.vs) {
716 /* create pass-through vertex shader now */
717 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
718 TGSI_SEMANTIC_COLOR,
719 TGSI_SEMANTIC_GENERIC };
720 const uint semantic_indexes[] = { 0, 0, 0 };
721 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
722 semantic_names,
723 semantic_indexes);
724 }
725
726 if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
727 return;
728
729 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
730 if (pt) {
731 assert(pt->target == PIPE_TEXTURE_2D);
732 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
733 width, height, pt,
734 st->ctx->Current.RasterColor);
735 /* release/free the texture */
736 pipe_texture_reference(&pt, NULL);
737 }
738 }
739
740
741 /** Per-context init */
742 void
743 st_init_bitmap_functions(struct dd_function_table *functions)
744 {
745 functions->Bitmap = st_Bitmap;
746 }
747
748
749 /** Per-context init */
750 void
751 st_init_bitmap(struct st_context *st)
752 {
753 struct pipe_sampler_state *sampler = &st->bitmap.sampler;
754 struct pipe_context *pipe = st->pipe;
755 struct pipe_screen *screen = pipe->screen;
756
757 /* init sampler state once */
758 memset(sampler, 0, sizeof(*sampler));
759 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
760 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
761 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
762 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
763 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
764 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
765 sampler->normalized_coords = 1;
766
767 /* init baseline rasterizer state once */
768 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
769 st->bitmap.rasterizer.gl_rasterization_rules = 1;
770
771 /* find a usable texture format */
772 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM, PIPE_TEXTURE_2D,
773 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
774 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
775 }
776 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM, PIPE_TEXTURE_2D,
777 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
778 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
779 }
780 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM, PIPE_TEXTURE_2D,
781 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
782 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
783 }
784 else {
785 /* XXX support more formats */
786 assert(0);
787 }
788
789 /* alloc bitmap cache object */
790 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
791
792 reset_cache(st);
793 }
794
795
796 /** Per-context tear-down */
797 void
798 st_destroy_bitmap(struct st_context *st)
799 {
800 struct pipe_context *pipe = st->pipe;
801 struct pipe_screen *screen = pipe->screen;
802 struct bitmap_cache *cache = st->bitmap.cache;
803
804
805
806 if (st->bitmap.vs) {
807 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
808 st->bitmap.vs = NULL;
809 }
810
811 if (st->bitmap.vbuf) {
812 pipe_buffer_reference(&st->bitmap.vbuf, NULL);
813 st->bitmap.vbuf = NULL;
814 }
815
816 if (cache) {
817 if (cache->trans) {
818 screen->transfer_unmap(screen, cache->trans);
819 screen->tex_transfer_destroy(cache->trans);
820 }
821 pipe_texture_reference(&st->bitmap.cache->texture, NULL);
822 _mesa_free(st->bitmap.cache);
823 st->bitmap.cache = NULL;
824 }
825 }