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