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