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