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