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