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