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