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