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