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