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