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