Merge commit 'origin/master' into gallium-0.2
[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 "pipe/p_context.h"
51 #include "pipe/p_defines.h"
52 #include "pipe/p_inlines.h"
53 #include "util/u_tile.h"
54 #include "util/u_draw_quad.h"
55 #include "util/u_simple_shaders.h"
56 #include "shader/prog_instruction.h"
57 #include "cso_cache/cso_context.h"
58
59
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 struct pipe_texture *texture;
96 struct pipe_surface *surf;
97
98 GLboolean empty;
99
100 /** An I8 texture image: */
101 ubyte *buffer;
102 };
103
104
105
106
107 /**
108 * Make fragment program for glBitmap:
109 * Sample the texture and kill the fragment if the bit is 0.
110 * This program will be combined with the user's fragment program.
111 */
112 static struct st_fragment_program *
113 make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
114 {
115 struct st_fragment_program *stfp;
116 struct gl_program *p;
117 GLuint ic = 0;
118
119 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
120 if (!p)
121 return NULL;
122
123 p->NumInstructions = 3;
124
125 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
126 if (!p->Instructions) {
127 ctx->Driver.DeleteProgram(ctx, p);
128 return NULL;
129 }
130 _mesa_init_instructions(p->Instructions, p->NumInstructions);
131
132 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
133 p->Instructions[ic].Opcode = OPCODE_TEX;
134 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
135 p->Instructions[ic].DstReg.Index = 0;
136 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
137 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
138 p->Instructions[ic].TexSrcUnit = samplerIndex;
139 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
140 ic++;
141
142 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
143 p->Instructions[ic].Opcode = OPCODE_KIL;
144 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
145 p->Instructions[ic].SrcReg[0].Index = 0;
146 p->Instructions[ic].SrcReg[0].NegateBase = NEGATE_XYZW;
147 ic++;
148
149 /* END; */
150 p->Instructions[ic++].Opcode = OPCODE_END;
151
152 assert(ic == p->NumInstructions);
153
154 p->InputsRead = FRAG_BIT_TEX0;
155 p->OutputsWritten = 0x0;
156 p->SamplersUsed = (1 << samplerIndex);
157
158 stfp = (struct st_fragment_program *) p;
159 stfp->Base.UsesKill = GL_TRUE;
160 st_translate_fragment_program(ctx->st, stfp, NULL);
161
162 return stfp;
163 }
164
165
166 static int
167 find_free_bit(uint bitfield)
168 {
169 int i;
170 for (i = 0; i < 32; i++) {
171 if ((bitfield & (1 << i)) == 0) {
172 return i;
173 }
174 }
175 return -1;
176 }
177
178
179 /**
180 * Combine basic bitmap fragment program with the user-defined program.
181 */
182 static struct st_fragment_program *
183 combined_bitmap_fragment_program(GLcontext *ctx)
184 {
185 struct st_context *st = ctx->st;
186 struct st_fragment_program *stfp = st->fp;
187
188 if (!stfp->bitmap_program) {
189 /*
190 * Generate new program which is the user-defined program prefixed
191 * with the bitmap sampler/kill instructions.
192 */
193 struct st_fragment_program *bitmap_prog;
194 uint sampler;
195
196 sampler = find_free_bit(st->fp->Base.Base.SamplersUsed);
197 bitmap_prog = make_bitmap_fragment_program(ctx, sampler);
198
199 stfp->bitmap_program = (struct st_fragment_program *)
200 _mesa_combine_programs(ctx,
201 &bitmap_prog->Base.Base, &stfp->Base.Base);
202 stfp->bitmap_program->bitmap_sampler = sampler;
203
204 /* done with this after combining */
205 st_reference_fragprog(st, &bitmap_prog, NULL);
206
207 #if 0
208 {
209 struct gl_program *p = &stfp->bitmap_program->Base.Base;
210 printf("Combined bitmap program:\n");
211 _mesa_print_program(p);
212 printf("InputsRead: 0x%x\n", p->InputsRead);
213 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
214 _mesa_print_parameter_list(p->Parameters);
215 }
216 #endif
217
218 /* translate to TGSI tokens */
219 st_translate_fragment_program(st, stfp->bitmap_program, NULL);
220 }
221
222 /* Ideally we'd have updated the pipe constants during the normal
223 * st/atom mechanism. But we can't since this is specific to glBitmap.
224 */
225 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
226
227 return stfp->bitmap_program;
228 }
229
230
231 /**
232 * Copy user-provide bitmap bits into texture buffer, expanding
233 * bits into texels.
234 * "On" bits will set texels to 0xff.
235 * "Off" bits will not modify texels.
236 * Note that the image is actually going to be upside down in
237 * the texture. We deal with that with texcoords.
238 */
239 static void
240 unpack_bitmap(struct st_context *st,
241 GLint px, GLint py, GLsizei width, GLsizei height,
242 const struct gl_pixelstore_attrib *unpack,
243 const GLubyte *bitmap,
244 ubyte *destBuffer, uint destStride)
245 {
246 GLint row, col;
247
248 #define SET_PIXEL(COL, ROW) \
249 destBuffer[(py + (ROW)) * destStride + px + (COL)] = 0x0;
250
251 for (row = 0; row < height; row++) {
252 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
253 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
254
255 if (unpack->LsbFirst) {
256 /* Lsb first */
257 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
258 for (col = 0; col < width; col++) {
259
260 if (*src & mask) {
261 SET_PIXEL(col, row);
262 }
263
264 if (mask == 128U) {
265 src++;
266 mask = 1U;
267 }
268 else {
269 mask = mask << 1;
270 }
271 }
272
273 /* get ready for next row */
274 if (mask != 1)
275 src++;
276 }
277 else {
278 /* Msb first */
279 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
280 for (col = 0; col < width; col++) {
281
282 if (*src & mask) {
283 SET_PIXEL(col, row);
284 }
285
286 if (mask == 1U) {
287 src++;
288 mask = 128U;
289 }
290 else {
291 mask = mask >> 1;
292 }
293 }
294
295 /* get ready for next row */
296 if (mask != 128)
297 src++;
298 }
299
300 } /* row */
301
302 #undef SET_PIXEL
303 }
304
305
306 /**
307 * Create a texture which represents a bitmap image.
308 */
309 static struct pipe_texture *
310 make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
311 const struct gl_pixelstore_attrib *unpack,
312 const GLubyte *bitmap)
313 {
314 struct pipe_context *pipe = ctx->st->pipe;
315 struct pipe_screen *screen = pipe->screen;
316 struct pipe_surface *surface;
317 ubyte *dest;
318 struct pipe_texture *pt;
319
320 /* PBO source... */
321 bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
322 if (!bitmap) {
323 return NULL;
324 }
325
326 /**
327 * Create texture to hold bitmap pattern.
328 */
329 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, ctx->st->bitmap.tex_format,
330 0, width, height, 1, 0,
331 PIPE_TEXTURE_USAGE_SAMPLER);
332 if (!pt) {
333 _mesa_unmap_bitmap_pbo(ctx, unpack);
334 return NULL;
335 }
336
337 surface = screen->get_tex_surface(screen, pt, 0, 0, 0,
338 PIPE_BUFFER_USAGE_CPU_WRITE);
339
340 /* map texture surface */
341 dest = screen->surface_map(screen, surface, PIPE_BUFFER_USAGE_CPU_WRITE);
342
343 /* Put image into texture surface */
344 memset(dest, 0xff, height * surface->stride);
345 unpack_bitmap(ctx->st, 0, 0, width, height, unpack, bitmap,
346 dest, surface->stride);
347
348 _mesa_unmap_bitmap_pbo(ctx, unpack);
349
350 /* Release surface */
351 screen->surface_unmap(screen, surface);
352 pipe_surface_reference(&surface, NULL);
353
354 return pt;
355 }
356
357
358 static void
359 setup_bitmap_vertex_data(struct st_context *st,
360 int x, int y, int width, int height,
361 float z, const float color[4])
362 {
363 struct pipe_context *pipe = st->pipe;
364 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
365 const GLfloat fb_width = (GLfloat)fb->Width;
366 const GLfloat fb_height = (GLfloat)fb->Height;
367 const GLfloat x0 = (GLfloat)x;
368 const GLfloat x1 = (GLfloat)(x + width);
369 const GLfloat y0 = (GLfloat)y;
370 const GLfloat y1 = (GLfloat)(y + height);
371 const GLfloat sLeft = (GLfloat)0.0, sRight = (GLfloat)1.0;
372 const GLfloat tTop = (GLfloat)0.0, tBot = (GLfloat)1.0 - tTop;
373 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
374 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
375 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
376 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
377 GLuint i;
378 void *buf;
379
380 if (!st->bitmap.vbuf) {
381 st->bitmap.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
382 sizeof(st->bitmap.vertices));
383 }
384
385 /* Positions are in clip coords since we need to do clipping in case
386 * the bitmap quad goes beyond the window bounds.
387 */
388 st->bitmap.vertices[0][0][0] = clip_x0;
389 st->bitmap.vertices[0][0][1] = clip_y0;
390 st->bitmap.vertices[0][2][0] = sLeft;
391 st->bitmap.vertices[0][2][1] = tTop;
392
393 st->bitmap.vertices[1][0][0] = clip_x1;
394 st->bitmap.vertices[1][0][1] = clip_y0;
395 st->bitmap.vertices[1][2][0] = sRight;
396 st->bitmap.vertices[1][2][1] = tTop;
397
398 st->bitmap.vertices[2][0][0] = clip_x1;
399 st->bitmap.vertices[2][0][1] = clip_y1;
400 st->bitmap.vertices[2][2][0] = sRight;
401 st->bitmap.vertices[2][2][1] = tBot;
402
403 st->bitmap.vertices[3][0][0] = clip_x0;
404 st->bitmap.vertices[3][0][1] = clip_y1;
405 st->bitmap.vertices[3][2][0] = sLeft;
406 st->bitmap.vertices[3][2][1] = tBot;
407
408 /* same for all verts: */
409 for (i = 0; i < 4; i++) {
410 st->bitmap.vertices[i][0][2] = z;
411 st->bitmap.vertices[i][0][3] = 1.0;
412 st->bitmap.vertices[i][1][0] = color[0];
413 st->bitmap.vertices[i][1][1] = color[1];
414 st->bitmap.vertices[i][1][2] = color[2];
415 st->bitmap.vertices[i][1][3] = color[3];
416 st->bitmap.vertices[i][2][2] = 0.0; /*R*/
417 st->bitmap.vertices[i][2][3] = 1.0; /*Q*/
418 }
419
420 /* put vertex data into vbuf */
421 buf = pipe_buffer_map(pipe->screen, st->bitmap.vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
422 memcpy(buf, st->bitmap.vertices, sizeof(st->bitmap.vertices));
423 pipe_buffer_unmap(pipe->screen, st->bitmap.vbuf);
424 }
425
426
427
428 /**
429 * Render a glBitmap by drawing a textured quad
430 */
431 static void
432 draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
433 GLsizei width, GLsizei height,
434 struct pipe_texture *pt,
435 const GLfloat *color)
436 {
437 struct st_context *st = ctx->st;
438 struct pipe_context *pipe = ctx->st->pipe;
439 struct cso_context *cso = ctx->st->cso_context;
440 struct st_fragment_program *stfp;
441 GLuint maxSize;
442
443 stfp = combined_bitmap_fragment_program(ctx);
444
445 /* limit checks */
446 /* XXX if the bitmap is larger than the max texture size, break
447 * it up into chunks.
448 */
449 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
450 assert(width <= (GLsizei)maxSize);
451 assert(height <= (GLsizei)maxSize);
452
453 cso_save_rasterizer(cso);
454 cso_save_samplers(cso);
455 cso_save_sampler_textures(cso);
456 cso_save_viewport(cso);
457 cso_save_fragment_shader(cso);
458 cso_save_vertex_shader(cso);
459
460 /* rasterizer state: just scissor */
461 st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
462 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
463
464 /* fragment shader state: TEX lookup program */
465 cso_set_fragment_shader_handle(cso, stfp->driver_shader);
466
467 /* vertex shader state: position + texcoord pass-through */
468 cso_set_vertex_shader_handle(cso, st->bitmap.vs);
469
470 /* user samplers, plus our bitmap sampler */
471 {
472 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
473 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_samplers);
474 uint i;
475 for (i = 0; i < st->state.num_samplers; i++) {
476 samplers[i] = &st->state.samplers[i];
477 }
478 samplers[stfp->bitmap_sampler] = &st->bitmap.sampler;
479 cso_set_samplers(cso, num, (const struct pipe_sampler_state **) samplers); }
480
481 /* user textures, plus the bitmap texture */
482 {
483 struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
484 uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_textures);
485 memcpy(textures, st->state.sampler_texture, sizeof(textures));
486 textures[stfp->bitmap_sampler] = pt;
487 cso_set_sampler_textures(cso, num, textures);
488 }
489
490 /* viewport state: viewport matching window dims */
491 {
492 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
493 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
494 const GLfloat width = (GLfloat)fb->Width;
495 const GLfloat height = (GLfloat)fb->Height;
496 struct pipe_viewport_state vp;
497 vp.scale[0] = 0.5f * width;
498 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
499 vp.scale[2] = 1.0f;
500 vp.scale[3] = 1.0f;
501 vp.translate[0] = 0.5f * width;
502 vp.translate[1] = 0.5f * height;
503 vp.translate[2] = 0.0f;
504 vp.translate[3] = 0.0f;
505 cso_set_viewport(cso, &vp);
506 }
507
508 /* draw textured quad */
509 setup_bitmap_vertex_data(st, x, y, width, height,
510 ctx->Current.RasterPos[2],
511 color);
512
513 util_draw_vertex_buffer(pipe, st->bitmap.vbuf,
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_sampler_textures(cso);
523 cso_restore_viewport(cso);
524 cso_restore_fragment_shader(cso);
525 cso_restore_vertex_shader(cso);
526 }
527
528
529 static void
530 reset_cache(struct st_context *st)
531 {
532 struct pipe_context *pipe = st->pipe;
533 struct pipe_screen *screen = pipe->screen;
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->surf)
545 screen->tex_surface_release(screen, &cache->surf);
546
547 assert(!cache->texture);
548
549 /* allocate a new texture */
550 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
551 st->bitmap.tex_format, 0,
552 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
553 1, 0,
554 PIPE_TEXTURE_USAGE_SAMPLER);
555
556 /* Map the texture surface.
557 * Subsequent glBitmap calls will write into the texture image.
558 */
559 cache->surf = screen->get_tex_surface(screen, cache->texture, 0, 0, 0,
560 PIPE_BUFFER_USAGE_CPU_WRITE);
561 cache->buffer = screen->surface_map(screen, cache->surf,
562 PIPE_BUFFER_USAGE_CPU_WRITE);
563
564 /* init image to all 0xff */
565 memset(cache->buffer, 0xff, BITMAP_CACHE_WIDTH * BITMAP_CACHE_HEIGHT);
566 }
567
568
569 /**
570 * If there's anything in the bitmap cache, draw/flush it now.
571 */
572 void
573 st_flush_bitmap_cache(struct st_context *st)
574 {
575 if (!st->bitmap.cache->empty) {
576 struct bitmap_cache *cache = st->bitmap.cache;
577
578 if (st->ctx->DrawBuffer) {
579 struct pipe_context *pipe = st->pipe;
580 struct pipe_screen *screen = pipe->screen;
581
582 assert(cache->xmin <= cache->xmax);
583 /*
584 printf("flush size %d x %d at %d, %d\n",
585 cache->xmax - cache->xmin,
586 cache->ymax - cache->ymin,
587 cache->xpos, cache->ypos);
588 */
589
590 /* The texture surface has been mapped until now.
591 * So unmap and release the texture surface before drawing.
592 */
593 screen->surface_unmap(screen, cache->surf);
594 cache->buffer = NULL;
595
596 screen->tex_surface_release(screen, &cache->surf);
597
598 draw_bitmap_quad(st->ctx,
599 cache->xpos,
600 cache->ypos,
601 st->ctx->Current.RasterPos[2],
602 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
603 cache->texture,
604 cache->color);
605 }
606
607 /* release/free the texture */
608 pipe_texture_reference(&cache->texture, NULL);
609
610 reset_cache(st);
611 }
612 }
613
614
615 /**
616 * Try to accumulate this glBitmap call in the bitmap cache.
617 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
618 */
619 static GLboolean
620 accum_bitmap(struct st_context *st,
621 GLint x, GLint y, GLsizei width, GLsizei height,
622 const struct gl_pixelstore_attrib *unpack,
623 const GLubyte *bitmap )
624 {
625 struct bitmap_cache *cache = st->bitmap.cache;
626 int px = -999, py;
627
628 if (width > BITMAP_CACHE_WIDTH ||
629 height > BITMAP_CACHE_HEIGHT)
630 return GL_FALSE; /* too big to cache */
631
632 if (!cache->empty) {
633 px = x - cache->xpos; /* pos in buffer */
634 py = y - cache->ypos;
635 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
636 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
637 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color)) {
638 /* This bitmap would extend beyond cache bounds, or the bitmap
639 * color is changing
640 * so flush and continue.
641 */
642 st_flush_bitmap_cache(st);
643 }
644 }
645
646 if (cache->empty) {
647 /* Initialize. Center bitmap vertically in the buffer. */
648 px = 0;
649 py = (BITMAP_CACHE_HEIGHT - height) / 2;
650 cache->xpos = x;
651 cache->ypos = y - py;
652 cache->empty = GL_FALSE;
653 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
654 }
655
656 assert(px != -999);
657
658 if (x < cache->xmin)
659 cache->xmin = x;
660 if (y < cache->ymin)
661 cache->ymin = y;
662 if (x + width > cache->xmax)
663 cache->xmax = x + width;
664 if (y + height > cache->ymax)
665 cache->ymax = y + height;
666
667 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
668 cache->buffer, BITMAP_CACHE_WIDTH);
669
670 return GL_TRUE; /* accumulated */
671 }
672
673
674
675 /**
676 * Called via ctx->Driver.Bitmap()
677 */
678 static void
679 st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
680 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
681 {
682 struct st_context *st = ctx->st;
683 struct pipe_texture *pt;
684
685 if (width == 0 || height == 0)
686 return;
687
688 st_validate_state(st);
689
690 if (!st->bitmap.vs) {
691 /* create pass-through vertex shader now */
692 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
693 TGSI_SEMANTIC_COLOR,
694 TGSI_SEMANTIC_GENERIC };
695 const uint semantic_indexes[] = { 0, 0, 0 };
696 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
697 semantic_names,
698 semantic_indexes,
699 &st->bitmap.vert_shader);
700 }
701
702 if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
703 return;
704
705 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
706 if (pt) {
707 assert(pt->target == PIPE_TEXTURE_2D);
708 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
709 width, height, pt,
710 st->ctx->Current.RasterColor);
711 /* release/free the texture */
712 pipe_texture_reference(&pt, NULL);
713 }
714 }
715
716
717 /** Per-context init */
718 void
719 st_init_bitmap_functions(struct dd_function_table *functions)
720 {
721 functions->Bitmap = st_Bitmap;
722 }
723
724
725 /** Per-context init */
726 void
727 st_init_bitmap(struct st_context *st)
728 {
729 struct pipe_sampler_state *sampler = &st->bitmap.sampler;
730 struct pipe_context *pipe = st->pipe;
731 struct pipe_screen *screen = pipe->screen;
732
733 /* init sampler state once */
734 memset(sampler, 0, sizeof(*sampler));
735 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
736 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
737 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
738 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
739 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
740 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
741 sampler->normalized_coords = 1;
742
743 /* init baseline rasterizer state once */
744 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
745 st->bitmap.rasterizer.gl_rasterization_rules = 1;
746 st->bitmap.rasterizer.bypass_vs = 1;
747
748 /* find a usable texture format */
749 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM, PIPE_TEXTURE_2D,
750 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
751 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
752 }
753 else {
754 /* XXX support more formats */
755 assert(0);
756 }
757
758 /* alloc bitmap cache object */
759 st->bitmap.cache = CALLOC_STRUCT(bitmap_cache);
760
761 reset_cache(st);
762 }
763
764
765 /** Per-context tear-down */
766 void
767 st_destroy_bitmap(struct st_context *st)
768 {
769 struct pipe_context *pipe = st->pipe;
770 struct pipe_screen *screen = pipe->screen;
771 struct bitmap_cache *cache = st->bitmap.cache;
772
773 screen->surface_unmap(screen, cache->surf);
774 screen->tex_surface_release(screen, &cache->surf);
775
776 if (st->bitmap.vs) {
777 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
778 st->bitmap.vs = NULL;
779 }
780
781 if (st->bitmap.vbuf) {
782 pipe_buffer_reference(pipe->screen, &st->bitmap.vbuf, NULL);
783 st->bitmap.vbuf = NULL;
784 }
785
786 if (st->bitmap.cache) {
787 pipe_texture_release(&st->bitmap.cache->texture);
788 FREE(st->bitmap.cache);
789 st->bitmap.cache = NULL;
790 }
791 }