mesa/st: support using PIPE_TEXTURE_RECT internally
[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(GLcontext *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 */
189 static struct st_fragment_program *
190 combined_bitmap_fragment_program(GLcontext *ctx)
191 {
192 struct st_context *st = st_context(ctx);
193 struct st_fragment_program *stfp = st->fp;
194
195 if (!stfp->bitmap_program) {
196 /*
197 * Generate new program which is the user-defined program prefixed
198 * with the bitmap sampler/kill instructions.
199 */
200 struct st_fragment_program *bitmap_prog;
201 uint sampler;
202
203 sampler = find_free_bit(st->fp->Base.Base.SamplersUsed);
204 bitmap_prog = make_bitmap_fragment_program(ctx, sampler);
205
206 stfp->bitmap_program = (struct st_fragment_program *)
207 _mesa_combine_programs(ctx,
208 &bitmap_prog->Base.Base, &stfp->Base.Base);
209 stfp->bitmap_program->bitmap_sampler = sampler;
210
211 /* done with this after combining */
212 st_reference_fragprog(st, &bitmap_prog, NULL);
213
214 #if 0
215 {
216 struct gl_program *p = &stfp->bitmap_program->Base.Base;
217 printf("Combined bitmap program:\n");
218 _mesa_print_program(p);
219 printf("InputsRead: 0x%x\n", p->InputsRead);
220 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
221 _mesa_print_parameter_list(p->Parameters);
222 }
223 #endif
224
225 /* translate to TGSI tokens */
226 st_translate_fragment_program(st, stfp->bitmap_program);
227 }
228
229 return stfp->bitmap_program;
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(GLcontext *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,
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, 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
326 if(!normalized)
327 {
328 sRight = width;
329 tBot = height;
330 }
331
332 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
333 * no_flush) updates to buffers where we know there is no conflict
334 * with previous data. Currently using max_slots > 1 will cause
335 * synchronous rendering if the driver flushes its command buffers
336 * between one bitmap and the next. Our flush hook below isn't
337 * sufficient to catch this as the driver doesn't tell us when it
338 * flushes its own command buffers. Until this gets fixed, pay the
339 * price of allocating a new buffer for each bitmap cache-flush to
340 * avoid synchronous rendering.
341 */
342 const GLuint max_slots = 1; /* 4096 / sizeof(st->bitmap.vertices); */
343 GLuint i;
344
345 if (st->bitmap.vbuf_slot >= max_slots) {
346 pipe_resource_reference(&st->bitmap.vbuf, NULL);
347 st->bitmap.vbuf_slot = 0;
348 }
349
350 if (!st->bitmap.vbuf) {
351 st->bitmap.vbuf = pipe_buffer_create(pipe->screen,
352 PIPE_BIND_VERTEX_BUFFER,
353 max_slots * 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 * sizeof st->bitmap.vertices,
395 sizeof st->bitmap.vertices,
396 st->bitmap.vertices);
397
398 return st->bitmap.vbuf_slot++ * sizeof st->bitmap.vertices;
399 }
400
401
402
403 /**
404 * Render a glBitmap by drawing a textured quad
405 */
406 static void
407 draw_bitmap_quad(GLcontext *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_fragment_program *stfp;
416 GLuint maxSize;
417 GLuint offset;
418
419 stfp = combined_bitmap_fragment_program(ctx);
420
421 /* As an optimization, Mesa's fragment programs will sometimes get the
422 * primary color from a statevar/constant rather than a varying variable.
423 * when that's the case, we need to ensure that we use the 'color'
424 * parameter and not the current attribute color (which may have changed
425 * through glRasterPos and state validation.
426 * So, we force the proper color here. Not elegant, but it works.
427 */
428 {
429 GLfloat colorSave[4];
430 COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
431 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
432 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
433 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
434 }
435
436
437 /* limit checks */
438 /* XXX if the bitmap is larger than the max texture size, break
439 * it up into chunks.
440 */
441 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
442 assert(width <= (GLsizei)maxSize);
443 assert(height <= (GLsizei)maxSize);
444
445 cso_save_rasterizer(cso);
446 cso_save_samplers(cso);
447 cso_save_fragment_sampler_views(cso);
448 cso_save_viewport(cso);
449 cso_save_fragment_shader(cso);
450 cso_save_vertex_shader(cso);
451 cso_save_vertex_elements(cso);
452
453 /* rasterizer state: just scissor */
454 st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
455 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
456
457 /* fragment shader state: TEX lookup program */
458 cso_set_fragment_shader_handle(cso, stfp->driver_shader);
459
460 /* vertex shader state: position + texcoord pass-through */
461 cso_set_vertex_shader_handle(cso, st->bitmap.vs);
462
463 /* user samplers, plus our bitmap sampler */
464 {
465 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
466 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_samplers);
467 uint i;
468 for (i = 0; i < st->state.num_samplers; i++) {
469 samplers[i] = &st->state.samplers[i];
470 }
471 samplers[stfp->bitmap_sampler] = &st->bitmap.samplers[sv->texture->target != PIPE_TEXTURE_RECT];
472 cso_set_samplers(cso, num, (const struct pipe_sampler_state **) samplers);
473 }
474
475 /* user textures, plus the bitmap texture */
476 {
477 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
478 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_textures);
479 memcpy(sampler_views, st->state.sampler_views, sizeof(sampler_views));
480 sampler_views[stfp->bitmap_sampler] = sv;
481 cso_set_fragment_sampler_views(cso, num, sampler_views);
482 }
483
484 /* viewport state: viewport matching window dims */
485 {
486 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
487 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
488 const GLfloat width = (GLfloat)fb->Width;
489 const GLfloat height = (GLfloat)fb->Height;
490 struct pipe_viewport_state vp;
491 vp.scale[0] = 0.5f * width;
492 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
493 vp.scale[2] = 0.5f;
494 vp.scale[3] = 1.0f;
495 vp.translate[0] = 0.5f * width;
496 vp.translate[1] = 0.5f * height;
497 vp.translate[2] = 0.5f;
498 vp.translate[3] = 0.0f;
499 cso_set_viewport(cso, &vp);
500 }
501
502 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
503
504 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
505 z = z * 2.0 - 1.0;
506
507 /* draw textured quad */
508 offset = setup_bitmap_vertex_data(st, sv->texture->target != PIPE_TEXTURE_RECT, x, y, width, height, z, color);
509
510 util_draw_vertex_buffer(pipe, st->bitmap.vbuf, offset,
511 PIPE_PRIM_TRIANGLE_FAN,
512 4, /* verts */
513 3); /* attribs/vert */
514
515
516 /* restore state */
517 cso_restore_rasterizer(cso);
518 cso_restore_samplers(cso);
519 cso_restore_fragment_sampler_views(cso);
520 cso_restore_viewport(cso);
521 cso_restore_fragment_shader(cso);
522 cso_restore_vertex_shader(cso);
523 cso_restore_vertex_elements(cso);
524 }
525
526
527 static void
528 reset_cache(struct st_context *st)
529 {
530 struct pipe_context *pipe = st->pipe;
531 struct bitmap_cache *cache = st->bitmap.cache;
532
533 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
534 cache->empty = GL_TRUE;
535
536 cache->xmin = 1000000;
537 cache->xmax = -1000000;
538 cache->ymin = 1000000;
539 cache->ymax = -1000000;
540
541 if (cache->trans) {
542 pipe->transfer_destroy(pipe, cache->trans);
543 cache->trans = NULL;
544 }
545
546 assert(!cache->texture);
547
548 /* allocate a new texture */
549 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
550 st->bitmap.tex_format, 0,
551 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
552 1,
553 PIPE_BIND_SAMPLER_VIEW);
554 }
555
556
557 /** Print bitmap image to stdout (debug) */
558 static void
559 print_cache(const struct bitmap_cache *cache)
560 {
561 int i, j, k;
562
563 for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
564 k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
565 for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
566 if (cache->buffer[k])
567 printf("X");
568 else
569 printf(" ");
570 k++;
571 }
572 printf("\n");
573 }
574 }
575
576
577 static void
578 create_cache_trans(struct st_context *st)
579 {
580 struct pipe_context *pipe = st->pipe;
581 struct bitmap_cache *cache = st->bitmap.cache;
582
583 if (cache->trans)
584 return;
585
586 /* Map the texture transfer.
587 * Subsequent glBitmap calls will write into the texture image.
588 */
589 cache->trans = pipe_get_transfer(st->pipe, cache->texture, 0, 0, 0,
590 PIPE_TRANSFER_WRITE, 0, 0,
591 BITMAP_CACHE_WIDTH,
592 BITMAP_CACHE_HEIGHT);
593 cache->buffer = pipe_transfer_map(pipe, cache->trans);
594
595 /* init image to all 0xff */
596 memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
597 }
598
599
600 /**
601 * If there's anything in the bitmap cache, draw/flush it now.
602 */
603 void
604 st_flush_bitmap_cache(struct st_context *st)
605 {
606 if (!st->bitmap.cache->empty) {
607 struct bitmap_cache *cache = st->bitmap.cache;
608
609 if (st->ctx->DrawBuffer) {
610 struct pipe_context *pipe = st->pipe;
611 struct pipe_sampler_view *sv;
612
613 assert(cache->xmin <= cache->xmax);
614
615 /* printf("flush size %d x %d at %d, %d\n",
616 cache->xmax - cache->xmin,
617 cache->ymax - cache->ymin,
618 cache->xpos, cache->ypos);
619 */
620
621 /* The texture transfer has been mapped until now.
622 * So unmap and release the texture transfer before drawing.
623 */
624 if (cache->trans) {
625 if (0)
626 print_cache(cache);
627 pipe_transfer_unmap(pipe, cache->trans);
628 cache->buffer = NULL;
629
630 pipe->transfer_destroy(pipe, cache->trans);
631 cache->trans = NULL;
632 }
633
634 sv = st_create_texture_sampler_view(st->pipe, cache->texture);
635 if (sv) {
636 draw_bitmap_quad(st->ctx,
637 cache->xpos,
638 cache->ypos,
639 cache->zpos,
640 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
641 sv,
642 cache->color);
643
644 pipe_sampler_view_reference(&sv, NULL);
645 }
646 }
647
648 /* release/free the texture */
649 pipe_resource_reference(&cache->texture, NULL);
650
651 reset_cache(st);
652 }
653 }
654
655 /* Flush bitmap cache and release vertex buffer.
656 */
657 void
658 st_flush_bitmap( struct st_context *st )
659 {
660 st_flush_bitmap_cache(st);
661
662 /* Release vertex buffer to avoid synchronous rendering if we were
663 * to map it in the next frame.
664 */
665 pipe_resource_reference(&st->bitmap.vbuf, NULL);
666 st->bitmap.vbuf_slot = 0;
667 }
668
669
670 /**
671 * Try to accumulate this glBitmap call in the bitmap cache.
672 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
673 */
674 static GLboolean
675 accum_bitmap(struct st_context *st,
676 GLint x, GLint y, GLsizei width, GLsizei height,
677 const struct gl_pixelstore_attrib *unpack,
678 const GLubyte *bitmap )
679 {
680 struct bitmap_cache *cache = st->bitmap.cache;
681 int px = -999, py = -999;
682 const GLfloat z = st->ctx->Current.RasterPos[2];
683
684 if (width > BITMAP_CACHE_WIDTH ||
685 height > BITMAP_CACHE_HEIGHT)
686 return GL_FALSE; /* too big to cache */
687
688 if (!cache->empty) {
689 px = x - cache->xpos; /* pos in buffer */
690 py = y - cache->ypos;
691 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
692 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
693 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
694 ((fabs(z - cache->zpos) > Z_EPSILON))) {
695 /* This bitmap would extend beyond cache bounds, or the bitmap
696 * color is changing
697 * so flush and continue.
698 */
699 st_flush_bitmap_cache(st);
700 }
701 }
702
703 if (cache->empty) {
704 /* Initialize. Center bitmap vertically in the buffer. */
705 px = 0;
706 py = (BITMAP_CACHE_HEIGHT - height) / 2;
707 cache->xpos = x;
708 cache->ypos = y - py;
709 cache->zpos = z;
710 cache->empty = GL_FALSE;
711 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
712 }
713
714 assert(px != -999);
715 assert(py != -999);
716
717 if (x < cache->xmin)
718 cache->xmin = x;
719 if (y < cache->ymin)
720 cache->ymin = y;
721 if (x + width > cache->xmax)
722 cache->xmax = x + width;
723 if (y + height > cache->ymax)
724 cache->ymax = y + height;
725
726 /* create the transfer if needed */
727 create_cache_trans(st);
728
729 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
730 cache->buffer, BITMAP_CACHE_WIDTH);
731
732 return GL_TRUE; /* accumulated */
733 }
734
735
736
737 /**
738 * Called via ctx->Driver.Bitmap()
739 */
740 static void
741 st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
742 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
743 {
744 struct st_context *st = st_context(ctx);
745 struct pipe_resource *pt;
746
747 if (width == 0 || height == 0)
748 return;
749
750 st_validate_state(st);
751
752 if (!st->bitmap.vs) {
753 /* create pass-through vertex shader now */
754 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
755 TGSI_SEMANTIC_COLOR,
756 TGSI_SEMANTIC_GENERIC };
757 const uint semantic_indexes[] = { 0, 0, 0 };
758 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
759 semantic_names,
760 semantic_indexes);
761 }
762
763 if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
764 return;
765
766 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
767 if (pt) {
768 struct pipe_sampler_view *sv = st_create_texture_sampler_view(st->pipe, pt);
769
770 assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
771
772 if (sv) {
773 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
774 width, height, sv,
775 st->ctx->Current.RasterColor);
776
777 pipe_sampler_view_reference(&sv, NULL);
778 }
779
780 /* release/free the texture */
781 pipe_resource_reference(&pt, NULL);
782 }
783 }
784
785
786 /** Per-context init */
787 void
788 st_init_bitmap_functions(struct dd_function_table *functions)
789 {
790 functions->Bitmap = st_Bitmap;
791 }
792
793
794 /** Per-context init */
795 void
796 st_init_bitmap(struct st_context *st)
797 {
798 struct pipe_sampler_state *sampler = &st->bitmap.samplers[0];
799 struct pipe_context *pipe = st->pipe;
800 struct pipe_screen *screen = pipe->screen;
801
802 /* init sampler state once */
803 memset(sampler, 0, sizeof(*sampler));
804 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
805 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
806 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
807 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
808 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
809 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
810 st->bitmap.samplers[1] = *sampler;
811 st->bitmap.samplers[1].normalized_coords = 1;
812
813 /* init baseline rasterizer state once */
814 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
815 st->bitmap.rasterizer.gl_rasterization_rules = 1;
816
817 /* find a usable texture format */
818 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM, PIPE_TEXTURE_2D, 0,
819 PIPE_BIND_SAMPLER_VIEW, 0)) {
820 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
821 }
822 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM, PIPE_TEXTURE_2D, 0,
823 PIPE_BIND_SAMPLER_VIEW, 0)) {
824 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
825 }
826 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM, PIPE_TEXTURE_2D, 0,
827 PIPE_BIND_SAMPLER_VIEW, 0)) {
828 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
829 }
830 else {
831 /* XXX support more formats */
832 assert(0);
833 }
834
835 /* alloc bitmap cache object */
836 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
837
838 reset_cache(st);
839 }
840
841
842 /** Per-context tear-down */
843 void
844 st_destroy_bitmap(struct st_context *st)
845 {
846 struct pipe_context *pipe = st->pipe;
847 struct bitmap_cache *cache = st->bitmap.cache;
848
849
850
851 if (st->bitmap.vs) {
852 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
853 st->bitmap.vs = NULL;
854 }
855
856 if (st->bitmap.vbuf) {
857 pipe_resource_reference(&st->bitmap.vbuf, NULL);
858 st->bitmap.vbuf = NULL;
859 }
860
861 if (cache) {
862 if (cache->trans) {
863 pipe_transfer_unmap(pipe, cache->trans);
864 pipe->transfer_destroy(pipe, cache->trans);
865 }
866 pipe_resource_reference(&st->bitmap.cache->texture, NULL);
867 free(st->bitmap.cache);
868 st->bitmap.cache = NULL;
869 }
870 }
871
872 #endif /* FEATURE_drawpix */