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