• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
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 VMWARE 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/errors.h"
34 
35 #include "main/image.h"
36 #include "main/bufferobj.h"
37 #include "main/framebuffer.h"
38 #include "main/macros.h"
39 #include "main/pbo.h"
40 #include "program/program.h"
41 #include "program/prog_print.h"
42 
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_atom_constbuf.h"
46 #include "st_draw.h"
47 #include "st_program.h"
48 #include "st_cb_bitmap.h"
49 #include "st_cb_drawpixels.h"
50 #include "st_sampler_view.h"
51 #include "st_texture.h"
52 #include "st_util.h"
53 
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "pipe/p_shader_tokens.h"
57 #include "util/u_inlines.h"
58 #include "program/prog_instruction.h"
59 #include "cso_cache/cso_context.h"
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 
88 /** Epsilon for Z comparisons */
89 #define Z_EPSILON 1e-06
90 
91 static void
92 init_bitmap_state(struct st_context *st);
93 
94 /**
95  * Copy user-provide bitmap bits into texture buffer, expanding
96  * bits into texels.
97  * "On" bits will set texels to 0x0.
98  * "Off" bits will not modify texels.
99  * Note that the image is actually going to be upside down in
100  * the texture.  We deal with that with texcoords.
101  */
102 static void
unpack_bitmap(struct st_context * st,GLint px,GLint py,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap,uint8_t * destBuffer,uint destStride)103 unpack_bitmap(struct st_context *st,
104               GLint px, GLint py, GLsizei width, GLsizei height,
105               const struct gl_pixelstore_attrib *unpack,
106               const GLubyte *bitmap,
107               uint8_t *destBuffer, uint destStride)
108 {
109    destBuffer += py * destStride + px;
110 
111    _mesa_expand_bitmap(width, height, unpack, bitmap,
112                        destBuffer, destStride, 0x0);
113 }
114 
115 
116 /**
117  * Create a texture which represents a bitmap image.
118  */
119 struct pipe_resource *
st_make_bitmap_texture(struct gl_context * ctx,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap)120 st_make_bitmap_texture(struct gl_context *ctx, GLsizei width, GLsizei height,
121                        const struct gl_pixelstore_attrib *unpack,
122                        const GLubyte *bitmap)
123 {
124    struct st_context *st = st_context(ctx);
125    struct pipe_context *pipe = st->pipe;
126    struct pipe_transfer *transfer;
127    uint8_t *dest;
128    struct pipe_resource *pt;
129 
130    if (!st->bitmap.tex_format)
131       init_bitmap_state(st);
132 
133    /* PBO source... */
134    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
135    if (!bitmap) {
136       return NULL;
137    }
138 
139    /**
140     * Create texture to hold bitmap pattern.
141     */
142    pt = st_texture_create(st, st->internal_target, st->bitmap.tex_format,
143                           0, width, height, 1, 1, 0,
144                           PIPE_BIND_SAMPLER_VIEW, false);
145    if (!pt) {
146       _mesa_unmap_pbo_source(ctx, unpack);
147       return NULL;
148    }
149 
150    dest = pipe_texture_map(st->pipe, pt, 0, 0,
151                             PIPE_MAP_WRITE,
152                             0, 0, width, height, &transfer);
153 
154    /* Put image into texture transfer */
155    memset(dest, 0xff, height * transfer->stride);
156    unpack_bitmap(st, 0, 0, width, height, unpack, bitmap,
157                  dest, transfer->stride);
158 
159    _mesa_unmap_pbo_source(ctx, unpack);
160 
161    /* Release transfer */
162    pipe_texture_unmap(pipe, transfer);
163    return pt;
164 }
165 
166 
167 /**
168  * Setup pipeline state prior to rendering the bitmap textured quad.
169  */
170 static void
setup_render_state(struct gl_context * ctx,struct pipe_sampler_view * sv,const GLfloat * color,struct gl_program * fp,bool scissor_enabled,bool clamp_frag_color)171 setup_render_state(struct gl_context *ctx,
172                    struct pipe_sampler_view *sv,
173                    const GLfloat *color, struct gl_program *fp,
174                    bool scissor_enabled, bool clamp_frag_color)
175 {
176    struct st_context *st = st_context(ctx);
177    struct pipe_context *pipe = st->pipe;
178    struct cso_context *cso = st->cso_context;
179    struct st_fp_variant *fpv;
180    struct st_fp_variant_key key;
181 
182    memset(&key, 0, sizeof(key));
183    key.st = st->has_shareable_shaders ? NULL : st;
184    key.bitmap = GL_TRUE;
185    key.clamp_color = st->clamp_frag_color_in_shader &&
186                      clamp_frag_color;
187    key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
188 
189    fpv = st_get_fp_variant(st, fp, &key);
190 
191    /* As an optimization, Mesa's fragment programs will sometimes get the
192     * primary color from a statevar/constant rather than a varying variable.
193     * when that's the case, we need to ensure that we use the 'color'
194     * parameter and not the current attribute color (which may have changed
195     * through glRasterPos and state validation.
196     * So, we force the proper color here.  Not elegant, but it works.
197     */
198    {
199       GLfloat colorSave[4];
200       COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
201       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
202       st_upload_constants(st, fp, MESA_SHADER_FRAGMENT);
203       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
204    }
205 
206    cso_save_state(cso, (CSO_BIT_RASTERIZER |
207                         CSO_BIT_FRAGMENT_SAMPLERS |
208                         CSO_BIT_VIEWPORT |
209                         CSO_BIT_STREAM_OUTPUTS |
210                         CSO_BIT_VERTEX_ELEMENTS |
211                         CSO_BITS_ALL_SHADERS));
212 
213 
214    /* rasterizer state: just scissor */
215    st->bitmap.rasterizer.scissor = scissor_enabled;
216    cso_set_rasterizer(cso, &st->bitmap.rasterizer);
217 
218    /* fragment shader state: TEX lookup program */
219    cso_set_fragment_shader_handle(cso, fpv->base.driver_shader);
220 
221    /* vertex shader state: position + texcoord pass-through */
222    cso_set_vertex_shader_handle(cso, st->passthrough_vs);
223 
224    /* disable other shaders */
225    cso_set_tessctrl_shader_handle(cso, NULL);
226    cso_set_tesseval_shader_handle(cso, NULL);
227    cso_set_geometry_shader_handle(cso, NULL);
228 
229    /* user samplers, plus our bitmap sampler */
230    {
231       struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
232       uint num = MAX2(fpv->bitmap_sampler + 1,
233                       st->state.num_frag_samplers);
234       uint i;
235       for (i = 0; i < st->state.num_frag_samplers; i++) {
236          samplers[i] = &st->state.frag_samplers[i];
237       }
238       samplers[fpv->bitmap_sampler] = &st->bitmap.sampler;
239       cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
240                        (const struct pipe_sampler_state **) samplers);
241    }
242 
243    /* user textures, plus the bitmap texture */
244    {
245       struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
246       unsigned num_views =
247          st_get_sampler_views(st, PIPE_SHADER_FRAGMENT, fp, sampler_views);
248 
249       num_views = MAX2(fpv->bitmap_sampler + 1, num_views);
250       sampler_views[fpv->bitmap_sampler] = sv;
251       pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num_views, 0,
252                               true, sampler_views);
253       st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = num_views;
254    }
255 
256    /* viewport state: viewport matching window dims */
257    cso_set_viewport_dims(cso, st->state.fb_width,
258                          st->state.fb_height,
259                          st->state.fb_orientation == Y_0_TOP);
260 
261    st->util_velems.count = 3;
262    cso_set_vertex_elements(cso, &st->util_velems);
263 
264    cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
265 }
266 
267 
268 /**
269  * Restore pipeline state after rendering the bitmap textured quad.
270  */
271 static void
restore_render_state(struct gl_context * ctx)272 restore_render_state(struct gl_context *ctx)
273 {
274    struct st_context *st = st_context(ctx);
275    struct cso_context *cso = st->cso_context;
276 
277    /* Unbind all because st/mesa won't do it if the current shader doesn't
278     * use them.
279     */
280    cso_restore_state(cso, CSO_UNBIND_FS_SAMPLERVIEWS);
281    st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = 0;
282 
283    ctx->Array.NewVertexElements = true;
284    ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS |
285                           ST_NEW_FS_SAMPLER_VIEWS;
286 }
287 
288 
289 /**
290  * Render a glBitmap by drawing a textured quad
291  *
292  * take_ownership means the callee will be resposible for unreferencing sv.
293  */
294 static void
draw_bitmap_quad(struct gl_context * ctx,GLint x,GLint y,GLfloat z,GLsizei width,GLsizei height,struct pipe_sampler_view * sv,const GLfloat * color,struct gl_program * fp,bool scissor_enabled,bool clamp_frag_color)295 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
296                  GLsizei width, GLsizei height,
297                  struct pipe_sampler_view *sv, const GLfloat *color,
298                  struct gl_program *fp, bool scissor_enabled,
299                  bool clamp_frag_color)
300 {
301    struct st_context *st = st_context(ctx);
302    const float fb_width = (float) st->state.fb_width;
303    const float fb_height = (float) st->state.fb_height;
304    const float x0 = (float) x;
305    const float x1 = (float) (x + width);
306    const float y0 = (float) y;
307    const float y1 = (float) (y + height);
308    float sLeft = 0.0f, sRight = 1.0f;
309    float tTop = 0.0f, tBot = 1.0f - tTop;
310    const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
311    const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
312    const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
313    const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
314 
315    /* limit checks */
316    {
317       /* XXX if the bitmap is larger than the max texture size, break
318        * it up into chunks.
319        */
320       ASSERTED GLuint maxSize =
321          st->screen->get_param(st->screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
322       assert(width <= (GLsizei) maxSize);
323       assert(height <= (GLsizei) maxSize);
324    }
325 
326    if (sv->texture->target == PIPE_TEXTURE_RECT) {
327       /* use non-normalized texcoords */
328       sRight = (float) width;
329       tBot = (float) height;
330    }
331 
332    setup_render_state(ctx, sv, color, fp, scissor_enabled, clamp_frag_color);
333 
334    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
335    z = z * 2.0f - 1.0f;
336 
337    if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
338                      sLeft, tBot, sRight, tTop, color, 0)) {
339       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBitmap");
340    }
341 
342    restore_render_state(ctx);
343 
344    /* We uploaded modified constants, need to invalidate them. */
345    ctx->NewDriverState |= ST_NEW_FS_CONSTANTS;
346 }
347 
348 
349 static void
reset_cache(struct st_context * st)350 reset_cache(struct st_context *st)
351 {
352    struct st_bitmap_cache *cache = &st->bitmap.cache;
353 
354    /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
355    cache->empty = GL_TRUE;
356 
357    cache->xmin = 1000000;
358    cache->xmax = -1000000;
359    cache->ymin = 1000000;
360    cache->ymax = -1000000;
361 
362    _mesa_reference_program(st->ctx, &cache->fp, NULL);
363 
364    assert(!cache->texture);
365 
366    /* allocate a new texture */
367    cache->texture = st_texture_create(st, st->internal_target,
368                                       st->bitmap.tex_format, 0,
369                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
370                                       1, 1, 0,
371                                       PIPE_BIND_SAMPLER_VIEW,
372                                       false);
373 }
374 
375 
376 /** Print bitmap image to stdout (debug) */
377 static void
print_cache(const struct st_bitmap_cache * cache)378 print_cache(const struct st_bitmap_cache *cache)
379 {
380    int i, j, k;
381 
382    for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
383       k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
384       for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
385          if (cache->buffer[k])
386             printf("X");
387          else
388             printf(" ");
389          k++;
390       }
391       printf("\n");
392    }
393 }
394 
395 
396 /**
397  * Create gallium pipe_transfer object for the bitmap cache.
398  */
399 static void
create_cache_trans(struct st_context * st)400 create_cache_trans(struct st_context *st)
401 {
402    struct pipe_context *pipe = st->pipe;
403    struct st_bitmap_cache *cache = &st->bitmap.cache;
404 
405    if (cache->trans)
406       return;
407 
408    /* Map the texture transfer.
409     * Subsequent glBitmap calls will write into the texture image.
410     */
411    cache->buffer = pipe_texture_map(pipe, cache->texture, 0, 0,
412                                      PIPE_MAP_WRITE, 0, 0,
413                                      BITMAP_CACHE_WIDTH,
414                                      BITMAP_CACHE_HEIGHT, &cache->trans);
415 
416    /* init image to all 0xff */
417    memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
418 }
419 
420 
421 /**
422  * If there's anything in the bitmap cache, draw/flush it now.
423  */
424 void
st_flush_bitmap_cache(struct st_context * st)425 st_flush_bitmap_cache(struct st_context *st)
426 {
427    struct st_bitmap_cache *cache = &st->bitmap.cache;
428 
429    if (!cache->empty) {
430       struct pipe_context *pipe = st->pipe;
431       struct pipe_sampler_view *sv;
432 
433       assert(cache->xmin <= cache->xmax);
434 
435       if (0)
436          printf("flush bitmap, size %d x %d  at %d, %d\n",
437                 cache->xmax - cache->xmin,
438                 cache->ymax - cache->ymin,
439                 cache->xpos, cache->ypos);
440 
441       /* The texture transfer has been mapped until now.
442        * So unmap and release the texture transfer before drawing.
443        */
444       if (cache->trans && cache->buffer) {
445          if (0)
446             print_cache(cache);
447          pipe_texture_unmap(pipe, cache->trans);
448          cache->buffer = NULL;
449          cache->trans = NULL;
450       }
451 
452       sv = st_create_texture_sampler_view(st->pipe, cache->texture);
453       if (sv) {
454          draw_bitmap_quad(st->ctx,
455                           cache->xpos,
456                           cache->ypos,
457                           cache->zpos,
458                           BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
459                           sv,
460                           cache->color,
461                           cache->fp,
462                           cache->scissor_enabled,
463                           cache->clamp_frag_color);
464       }
465 
466       /* release/free the texture */
467       pipe_resource_reference(&cache->texture, NULL);
468 
469       reset_cache(st);
470    }
471 }
472 
473 
474 /**
475  * Try to accumulate this glBitmap call in the bitmap cache.
476  * \return  GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
477  */
478 static GLboolean
accum_bitmap(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap)479 accum_bitmap(struct gl_context *ctx,
480              GLint x, GLint y, GLsizei width, GLsizei height,
481              const struct gl_pixelstore_attrib *unpack,
482              const GLubyte *bitmap )
483 {
484    struct st_context *st = ctx->st;
485    struct st_bitmap_cache *cache = &st->bitmap.cache;
486    int px = -999, py = -999;
487    const GLfloat z = ctx->Current.RasterPos[2];
488 
489    if (width > BITMAP_CACHE_WIDTH ||
490        height > BITMAP_CACHE_HEIGHT)
491       return GL_FALSE; /* too big to cache */
492 
493    bool scissor_enabled = ctx->Scissor.EnableFlags & 0x1;
494    bool clamp_frag_color = ctx->Color._ClampFragmentColor;
495 
496    if (!cache->empty) {
497       px = x - cache->xpos;  /* pos in buffer */
498       py = y - cache->ypos;
499       if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
500           py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
501           !TEST_EQ_4V(ctx->Current.RasterColor, cache->color) ||
502           ctx->FragmentProgram._Current != cache->fp ||
503           scissor_enabled != cache->scissor_enabled ||
504           clamp_frag_color != cache->clamp_frag_color ||
505           ((fabsf(z - cache->zpos) > Z_EPSILON))) {
506          /* This bitmap would extend beyond cache bounds, or the bitmap
507           * color is changing
508           * so flush and continue.
509           */
510          st_flush_bitmap_cache(st);
511       }
512    }
513 
514    if (cache->empty) {
515       /* Initialize.  Center bitmap vertically in the buffer. */
516       px = 0;
517       py = (BITMAP_CACHE_HEIGHT - height) / 2;
518       cache->xpos = x;
519       cache->ypos = y - py;
520       cache->zpos = z;
521       cache->empty = GL_FALSE;
522       COPY_4FV(cache->color, ctx->Current.RasterColor);
523       _mesa_reference_program(ctx, &cache->fp, ctx->FragmentProgram._Current);
524       cache->scissor_enabled = scissor_enabled;
525       cache->clamp_frag_color = clamp_frag_color;
526    }
527 
528    assert(px != -999);
529    assert(py != -999);
530 
531    if (x < cache->xmin)
532       cache->xmin = x;
533    if (y < cache->ymin)
534       cache->ymin = y;
535    if (x + width > cache->xmax)
536       cache->xmax = x + width;
537    if (y + height > cache->ymax)
538       cache->ymax = y + height;
539 
540    /* create the transfer if needed */
541    create_cache_trans(st);
542 
543    /* PBO source... */
544    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
545    if (!bitmap) {
546       return false;
547    }
548 
549    unpack_bitmap(st, px, py, width, height, unpack, bitmap,
550                  cache->buffer, BITMAP_CACHE_WIDTH);
551 
552    _mesa_unmap_pbo_source(ctx, unpack);
553 
554    return GL_TRUE; /* accumulated */
555 }
556 
557 
558 /**
559  * One-time init for drawing bitmaps.
560  */
561 static void
init_bitmap_state(struct st_context * st)562 init_bitmap_state(struct st_context *st)
563 {
564    struct pipe_screen *screen = st->screen;
565 
566    /* This function should only be called once */
567    assert(!st->bitmap.tex_format);
568 
569    assert(st->internal_target == PIPE_TEXTURE_2D ||
570           st->internal_target == PIPE_TEXTURE_RECT);
571 
572    /* init sampler state once */
573    memset(&st->bitmap.sampler, 0, sizeof(st->bitmap.sampler));
574    st->bitmap.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
575    st->bitmap.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
576    st->bitmap.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
577    st->bitmap.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
578    st->bitmap.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
579    st->bitmap.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
580    st->bitmap.sampler.unnormalized_coords = !(st->internal_target == PIPE_TEXTURE_2D ||
581                                               (st->internal_target == PIPE_TEXTURE_RECT &&
582                                                st->lower_rect_tex));
583 
584    /* init baseline rasterizer state once */
585    memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
586    st->bitmap.rasterizer.half_pixel_center = 1;
587    st->bitmap.rasterizer.bottom_edge_rule = 1;
588    st->bitmap.rasterizer.depth_clip_near = 1;
589    st->bitmap.rasterizer.depth_clip_far = 1;
590 
591    /* find a usable texture format */
592    if (screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
593                                    st->internal_target, 0, 0,
594                                    PIPE_BIND_SAMPLER_VIEW)) {
595       st->bitmap.tex_format = PIPE_FORMAT_R8_UNORM;
596    }
597    else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
598                                         st->internal_target, 0, 0,
599                                         PIPE_BIND_SAMPLER_VIEW)) {
600       st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
601    }
602    else {
603       /* XXX support more formats */
604       assert(0);
605    }
606 
607    /* Create the vertex shader */
608    st_make_passthrough_vertex_shader(st);
609 
610    reset_cache(st);
611 }
612 
613 void
st_Bitmap(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap,struct pipe_resource * tex)614 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
615           GLsizei width, GLsizei height,
616           const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap,
617           struct pipe_resource *tex)
618 {
619    struct st_context *st = st_context(ctx);
620 
621    assert(width > 0);
622    assert(height > 0);
623 
624    st_invalidate_readpix_cache(st);
625    if (tex)
626       st_flush_bitmap_cache(st);
627 
628    if (!st->bitmap.tex_format) {
629       init_bitmap_state(st);
630    }
631 
632    /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
633     * for bitmap drawing uses no constants and the FS constants are
634     * explicitly uploaded in the draw_bitmap_quad() function.
635     */
636    st_validate_state(st, ST_PIPELINE_META_STATE_MASK & ~ST_NEW_CONSTANTS);
637 
638    struct pipe_sampler_view *view = NULL;
639 
640    if (!tex) {
641       if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
642          return;
643 
644       struct pipe_resource *pt =
645          st_make_bitmap_texture(ctx, width, height, unpack, bitmap);
646       if (!pt)
647          return;
648 
649       assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
650 
651       view = st_create_texture_sampler_view(st->pipe, pt);
652       /* unreference the texture because it's referenced by sv */
653       pipe_resource_reference(&pt, NULL);
654    } else {
655       /* tex comes from a display list. */
656       view = st_create_texture_sampler_view(st->pipe, tex);
657    }
658 
659    if (view) {
660       draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
661                        width, height, view, ctx->Current.RasterColor,
662                        ctx->FragmentProgram._Current,
663                        ctx->Scissor.EnableFlags & 0x1,
664                        ctx->Color._ClampFragmentColor);
665    }
666 }
667 
668 /** Per-context tear-down */
669 void
st_destroy_bitmap(struct st_context * st)670 st_destroy_bitmap(struct st_context *st)
671 {
672    struct pipe_context *pipe = st->pipe;
673    struct st_bitmap_cache *cache = &st->bitmap.cache;
674 
675    if (cache->trans && cache->buffer) {
676       pipe_texture_unmap(pipe, cache->trans);
677    }
678    pipe_resource_reference(&st->bitmap.cache.texture, NULL);
679    _mesa_reference_program(st->ctx, &st->bitmap.cache.fp, NULL);
680 }
681