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