• 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/dlist.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 "util/u_upload_mgr.h"
59 #include "program/prog_instruction.h"
60 #include "cso_cache/cso_context.h"
61 
62 
63 /**
64  * glBitmaps are drawn as textured quads.  The user's bitmap pattern
65  * is stored in a texture image.  An alpha8 texture format is used.
66  * The fragment shader samples a bit (texel) from the texture, then
67  * discards the fragment if the bit is off.
68  *
69  * Note that we actually store the inverse image of the bitmap to
70  * simplify the fragment program.  An "on" bit gets stored as texel=0x0
71  * and an "off" bit is stored as texel=0xff.  Then we kill the
72  * fragment if the negated texel value is less than zero.
73  */
74 
75 
76 /**
77  * The bitmap cache attempts to accumulate multiple glBitmap calls in a
78  * buffer which is then rendered en mass upon a flush, state change, etc.
79  * A wide, short buffer is used to target the common case of a series
80  * of glBitmap calls being used to draw text.
81  */
82 static GLboolean UseBitmapCache = GL_TRUE;
83 
84 
85 #define BITMAP_CACHE_WIDTH  512
86 #define BITMAP_CACHE_HEIGHT 32
87 
88 
89 /** Epsilon for Z comparisons */
90 #define Z_EPSILON 1e-06
91 
92 
93 /**
94  * Copy user-provide bitmap bits into texture buffer, expanding
95  * bits into texels.
96  * "On" bits will set texels to 0x0.
97  * "Off" bits will not modify texels.
98  * Note that the image is actually going to be upside down in
99  * the texture.  We deal with that with texcoords.
100  */
101 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,ubyte * destBuffer,uint destStride)102 unpack_bitmap(struct st_context *st,
103               GLint px, GLint py, GLsizei width, GLsizei height,
104               const struct gl_pixelstore_attrib *unpack,
105               const GLubyte *bitmap,
106               ubyte *destBuffer, uint destStride)
107 {
108    destBuffer += py * destStride + px;
109 
110    _mesa_expand_bitmap(width, height, unpack, bitmap,
111                        destBuffer, destStride, 0x0);
112 }
113 
114 
115 /**
116  * Create a texture which represents a bitmap image.
117  */
118 static struct pipe_resource *
make_bitmap_texture(struct gl_context * ctx,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap)119 make_bitmap_texture(struct gl_context *ctx, GLsizei width, GLsizei height,
120                     const struct gl_pixelstore_attrib *unpack,
121                     const GLubyte *bitmap)
122 {
123    struct st_context *st = st_context(ctx);
124    struct pipe_context *pipe = st->pipe;
125    struct pipe_transfer *transfer;
126    ubyte *dest;
127    struct pipe_resource *pt;
128 
129    /* PBO source... */
130    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
131    if (!bitmap) {
132       return NULL;
133    }
134 
135    /**
136     * Create texture to hold bitmap pattern.
137     */
138    pt = st_texture_create(st, st->internal_target, st->bitmap.tex_format,
139                           0, width, height, 1, 1, 0,
140                           PIPE_BIND_SAMPLER_VIEW);
141    if (!pt) {
142       _mesa_unmap_pbo_source(ctx, unpack);
143       return NULL;
144    }
145 
146    dest = pipe_transfer_map(st->pipe, pt, 0, 0,
147                             PIPE_MAP_WRITE,
148                             0, 0, width, height, &transfer);
149 
150    /* Put image into texture transfer */
151    memset(dest, 0xff, height * transfer->stride);
152    unpack_bitmap(st, 0, 0, width, height, unpack, bitmap,
153                  dest, transfer->stride);
154 
155    _mesa_unmap_pbo_source(ctx, unpack);
156 
157    /* Release transfer */
158    pipe_transfer_unmap(pipe, transfer);
159    return pt;
160 }
161 
162 
163 /**
164  * Setup pipeline state prior to rendering the bitmap textured quad.
165  */
166 static void
setup_render_state(struct gl_context * ctx,struct pipe_sampler_view * sv,const GLfloat * color,bool atlas)167 setup_render_state(struct gl_context *ctx,
168                    struct pipe_sampler_view *sv,
169                    const GLfloat *color,
170                    bool atlas)
171 {
172    struct st_context *st = st_context(ctx);
173    struct cso_context *cso = st->cso_context;
174    struct st_fp_variant *fpv;
175    struct st_fp_variant_key key;
176 
177    memset(&key, 0, sizeof(key));
178    key.st = st->has_shareable_shaders ? NULL : st;
179    key.bitmap = GL_TRUE;
180    key.clamp_color = st->clamp_frag_color_in_shader &&
181                      ctx->Color._ClampFragmentColor;
182    key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
183 
184    fpv = st_get_fp_variant(st, st->fp, &key);
185 
186    /* As an optimization, Mesa's fragment programs will sometimes get the
187     * primary color from a statevar/constant rather than a varying variable.
188     * when that's the case, we need to ensure that we use the 'color'
189     * parameter and not the current attribute color (which may have changed
190     * through glRasterPos and state validation.
191     * So, we force the proper color here.  Not elegant, but it works.
192     */
193    {
194       GLfloat colorSave[4];
195       COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
196       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
197       st_upload_constants(st, &st->fp->Base);
198       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
199    }
200 
201    cso_save_state(cso, (CSO_BIT_RASTERIZER |
202                         CSO_BIT_FRAGMENT_SAMPLERS |
203                         CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
204                         CSO_BIT_VIEWPORT |
205                         CSO_BIT_STREAM_OUTPUTS |
206                         CSO_BIT_VERTEX_ELEMENTS |
207                         CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
208                         CSO_BITS_ALL_SHADERS));
209 
210 
211    /* rasterizer state: just scissor */
212    st->bitmap.rasterizer.scissor = ctx->Scissor.EnableFlags & 1;
213    cso_set_rasterizer(cso, &st->bitmap.rasterizer);
214 
215    /* fragment shader state: TEX lookup program */
216    cso_set_fragment_shader_handle(cso, fpv->base.driver_shader);
217 
218    /* vertex shader state: position + texcoord pass-through */
219    cso_set_vertex_shader_handle(cso, st->passthrough_vs);
220 
221    /* disable other shaders */
222    cso_set_tessctrl_shader_handle(cso, NULL);
223    cso_set_tesseval_shader_handle(cso, NULL);
224    cso_set_geometry_shader_handle(cso, NULL);
225 
226    /* user samplers, plus our bitmap sampler */
227    {
228       struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
229       uint num = MAX2(fpv->bitmap_sampler + 1,
230                       st->state.num_frag_samplers);
231       uint i;
232       for (i = 0; i < st->state.num_frag_samplers; i++) {
233          samplers[i] = &st->state.frag_samplers[i];
234       }
235       if (atlas)
236          samplers[fpv->bitmap_sampler] = &st->bitmap.atlas_sampler;
237       else
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       uint num = MAX2(fpv->bitmap_sampler + 1,
247                       st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
248       memcpy(sampler_views, st->state.frag_sampler_views,
249              sizeof(sampler_views));
250       sampler_views[fpv->bitmap_sampler] = sv;
251       cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
252    }
253 
254    /* viewport state: viewport matching window dims */
255    cso_set_viewport_dims(cso, st->state.fb_width,
256                          st->state.fb_height,
257                          st->state.fb_orientation == Y_0_TOP);
258 
259    st->util_velems.count = 3;
260    cso_set_vertex_elements(cso, &st->util_velems);
261 
262    cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
263 }
264 
265 
266 /**
267  * Restore pipeline state after rendering the bitmap textured quad.
268  */
269 static void
restore_render_state(struct gl_context * ctx)270 restore_render_state(struct gl_context *ctx)
271 {
272    struct st_context *st = st_context(ctx);
273    struct cso_context *cso = st->cso_context;
274 
275    cso_restore_state(cso);
276 }
277 
278 
279 /**
280  * Render a glBitmap by drawing a textured quad
281  */
282 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)283 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
284                  GLsizei width, GLsizei height,
285                  struct pipe_sampler_view *sv,
286                  const GLfloat *color)
287 {
288    struct st_context *st = st_context(ctx);
289    struct pipe_context *pipe = st->pipe;
290    const float fb_width = (float) st->state.fb_width;
291    const float fb_height = (float) st->state.fb_height;
292    const float x0 = (float) x;
293    const float x1 = (float) (x + width);
294    const float y0 = (float) y;
295    const float y1 = (float) (y + height);
296    float sLeft = 0.0f, sRight = 1.0f;
297    float tTop = 0.0f, tBot = 1.0f - tTop;
298    const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
299    const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
300    const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
301    const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
302 
303    /* limit checks */
304    {
305       /* XXX if the bitmap is larger than the max texture size, break
306        * it up into chunks.
307        */
308       ASSERTED GLuint maxSize =
309          pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
310       assert(width <= (GLsizei) maxSize);
311       assert(height <= (GLsizei) maxSize);
312    }
313 
314    setup_render_state(ctx, sv, color, false);
315 
316    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
317    z = z * 2.0f - 1.0f;
318 
319    if (sv->texture->target == PIPE_TEXTURE_RECT) {
320       /* use non-normalized texcoords */
321       sRight = (float) width;
322       tBot = (float) height;
323    }
324 
325    if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
326                      sLeft, tBot, sRight, tTop, color, 0)) {
327       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBitmap");
328    }
329 
330    restore_render_state(ctx);
331 
332    /* We uploaded modified constants, need to invalidate them. */
333    st->dirty |= ST_NEW_FS_CONSTANTS;
334 }
335 
336 
337 static void
reset_cache(struct st_context * st)338 reset_cache(struct st_context *st)
339 {
340    struct st_bitmap_cache *cache = &st->bitmap.cache;
341 
342    /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
343    cache->empty = GL_TRUE;
344 
345    cache->xmin = 1000000;
346    cache->xmax = -1000000;
347    cache->ymin = 1000000;
348    cache->ymax = -1000000;
349 
350    assert(!cache->texture);
351 
352    /* allocate a new texture */
353    cache->texture = st_texture_create(st, st->internal_target,
354                                       st->bitmap.tex_format, 0,
355                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
356                                       1, 1, 0,
357 				      PIPE_BIND_SAMPLER_VIEW);
358 }
359 
360 
361 /** Print bitmap image to stdout (debug) */
362 static void
print_cache(const struct st_bitmap_cache * cache)363 print_cache(const struct st_bitmap_cache *cache)
364 {
365    int i, j, k;
366 
367    for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
368       k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
369       for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
370          if (cache->buffer[k])
371             printf("X");
372          else
373             printf(" ");
374          k++;
375       }
376       printf("\n");
377    }
378 }
379 
380 
381 /**
382  * Create gallium pipe_transfer object for the bitmap cache.
383  */
384 static void
create_cache_trans(struct st_context * st)385 create_cache_trans(struct st_context *st)
386 {
387    struct pipe_context *pipe = st->pipe;
388    struct st_bitmap_cache *cache = &st->bitmap.cache;
389 
390    if (cache->trans)
391       return;
392 
393    /* Map the texture transfer.
394     * Subsequent glBitmap calls will write into the texture image.
395     */
396    cache->buffer = pipe_transfer_map(pipe, cache->texture, 0, 0,
397                                      PIPE_MAP_WRITE, 0, 0,
398                                      BITMAP_CACHE_WIDTH,
399                                      BITMAP_CACHE_HEIGHT, &cache->trans);
400 
401    /* init image to all 0xff */
402    memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
403 }
404 
405 
406 /**
407  * If there's anything in the bitmap cache, draw/flush it now.
408  */
409 void
st_flush_bitmap_cache(struct st_context * st)410 st_flush_bitmap_cache(struct st_context *st)
411 {
412    struct st_bitmap_cache *cache = &st->bitmap.cache;
413 
414    if (!cache->empty) {
415       struct pipe_context *pipe = st->pipe;
416       struct pipe_sampler_view *sv;
417 
418       assert(cache->xmin <= cache->xmax);
419 
420       if (0)
421          printf("flush bitmap, size %d x %d  at %d, %d\n",
422                 cache->xmax - cache->xmin,
423                 cache->ymax - cache->ymin,
424                 cache->xpos, cache->ypos);
425 
426       /* The texture transfer has been mapped until now.
427        * So unmap and release the texture transfer before drawing.
428        */
429       if (cache->trans && cache->buffer) {
430          if (0)
431             print_cache(cache);
432          pipe_transfer_unmap(pipe, cache->trans);
433          cache->buffer = NULL;
434          cache->trans = NULL;
435       }
436 
437       sv = st_create_texture_sampler_view(st->pipe, cache->texture);
438       if (sv) {
439          draw_bitmap_quad(st->ctx,
440                           cache->xpos,
441                           cache->ypos,
442                           cache->zpos,
443                           BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
444                           sv,
445                           cache->color);
446 
447          pipe_sampler_view_reference(&sv, NULL);
448       }
449 
450       /* release/free the texture */
451       pipe_resource_reference(&cache->texture, NULL);
452 
453       reset_cache(st);
454    }
455 }
456 
457 
458 /**
459  * Try to accumulate this glBitmap call in the bitmap cache.
460  * \return  GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
461  */
462 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)463 accum_bitmap(struct gl_context *ctx,
464              GLint x, GLint y, GLsizei width, GLsizei height,
465              const struct gl_pixelstore_attrib *unpack,
466              const GLubyte *bitmap )
467 {
468    struct st_context *st = ctx->st;
469    struct st_bitmap_cache *cache = &st->bitmap.cache;
470    int px = -999, py = -999;
471    const GLfloat z = ctx->Current.RasterPos[2];
472 
473    if (width > BITMAP_CACHE_WIDTH ||
474        height > BITMAP_CACHE_HEIGHT)
475       return GL_FALSE; /* too big to cache */
476 
477    if (!cache->empty) {
478       px = x - cache->xpos;  /* pos in buffer */
479       py = y - cache->ypos;
480       if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
481           py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
482           !TEST_EQ_4V(ctx->Current.RasterColor, cache->color) ||
483           ((fabsf(z - cache->zpos) > Z_EPSILON))) {
484          /* This bitmap would extend beyond cache bounds, or the bitmap
485           * color is changing
486           * so flush and continue.
487           */
488          st_flush_bitmap_cache(st);
489       }
490    }
491 
492    if (cache->empty) {
493       /* Initialize.  Center bitmap vertically in the buffer. */
494       px = 0;
495       py = (BITMAP_CACHE_HEIGHT - height) / 2;
496       cache->xpos = x;
497       cache->ypos = y - py;
498       cache->zpos = z;
499       cache->empty = GL_FALSE;
500       COPY_4FV(cache->color, ctx->Current.RasterColor);
501    }
502 
503    assert(px != -999);
504    assert(py != -999);
505 
506    if (x < cache->xmin)
507       cache->xmin = x;
508    if (y < cache->ymin)
509       cache->ymin = y;
510    if (x + width > cache->xmax)
511       cache->xmax = x + width;
512    if (y + height > cache->ymax)
513       cache->ymax = y + height;
514 
515    /* create the transfer if needed */
516    create_cache_trans(st);
517 
518    /* PBO source... */
519    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
520    if (!bitmap) {
521       return FALSE;
522    }
523 
524    unpack_bitmap(st, px, py, width, height, unpack, bitmap,
525                  cache->buffer, BITMAP_CACHE_WIDTH);
526 
527    _mesa_unmap_pbo_source(ctx, unpack);
528 
529    return GL_TRUE; /* accumulated */
530 }
531 
532 
533 /**
534  * One-time init for drawing bitmaps.
535  */
536 static void
init_bitmap_state(struct st_context * st)537 init_bitmap_state(struct st_context *st)
538 {
539    struct pipe_context *pipe = st->pipe;
540    struct pipe_screen *screen = pipe->screen;
541 
542    /* This function should only be called once */
543    assert(!st->bitmap.tex_format);
544 
545    assert(st->internal_target == PIPE_TEXTURE_2D ||
546           st->internal_target == PIPE_TEXTURE_RECT);
547 
548    /* init sampler state once */
549    memset(&st->bitmap.sampler, 0, sizeof(st->bitmap.sampler));
550    st->bitmap.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
551    st->bitmap.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
552    st->bitmap.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
553    st->bitmap.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
554    st->bitmap.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
555    st->bitmap.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
556    st->bitmap.sampler.normalized_coords = st->internal_target == PIPE_TEXTURE_2D;
557 
558    st->bitmap.atlas_sampler = st->bitmap.sampler;
559    st->bitmap.atlas_sampler.normalized_coords = 0;
560 
561    /* init baseline rasterizer state once */
562    memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
563    st->bitmap.rasterizer.half_pixel_center = 1;
564    st->bitmap.rasterizer.bottom_edge_rule = 1;
565    st->bitmap.rasterizer.depth_clip_near = 1;
566    st->bitmap.rasterizer.depth_clip_far = 1;
567 
568    /* find a usable texture format */
569    if (screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
570                                    st->internal_target, 0, 0,
571                                    PIPE_BIND_SAMPLER_VIEW)) {
572       st->bitmap.tex_format = PIPE_FORMAT_R8_UNORM;
573    }
574    else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
575                                         st->internal_target, 0, 0,
576                                         PIPE_BIND_SAMPLER_VIEW)) {
577       st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
578    }
579    else {
580       /* XXX support more formats */
581       assert(0);
582    }
583 
584    /* Create the vertex shader */
585    st_make_passthrough_vertex_shader(st);
586 
587    reset_cache(st);
588 }
589 
590 
591 /**
592  * Called via ctx->Driver.Bitmap()
593  */
594 static void
st_Bitmap(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,const struct gl_pixelstore_attrib * unpack,const GLubyte * bitmap)595 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
596           GLsizei width, GLsizei height,
597           const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
598 {
599    struct st_context *st = st_context(ctx);
600    struct pipe_resource *pt;
601 
602    assert(width > 0);
603    assert(height > 0);
604 
605    st_invalidate_readpix_cache(st);
606 
607    if (!st->bitmap.tex_format) {
608       init_bitmap_state(st);
609    }
610 
611    /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
612     * for bitmap drawing uses no constants and the FS constants are
613     * explicitly uploaded in the draw_bitmap_quad() function.
614     */
615    if ((st->dirty | ctx->NewDriverState) & ~ST_NEW_CONSTANTS &
616        ST_PIPELINE_RENDER_STATE_MASK ||
617        st->gfx_shaders_may_be_dirty) {
618       st_validate_state(st, ST_PIPELINE_META);
619    }
620 
621    if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
622       return;
623 
624    pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
625    if (pt) {
626       struct pipe_sampler_view *sv =
627          st_create_texture_sampler_view(st->pipe, pt);
628 
629       assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
630 
631       if (sv) {
632          draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
633                           width, height, sv, ctx->Current.RasterColor);
634 
635          pipe_sampler_view_reference(&sv, NULL);
636       }
637 
638       /* release/free the texture */
639       pipe_resource_reference(&pt, NULL);
640    }
641 }
642 
643 
644 /**
645  * Called via ctx->Driver.DrawAtlasBitmap()
646  */
647 static void
st_DrawAtlasBitmaps(struct gl_context * ctx,const struct gl_bitmap_atlas * atlas,GLuint count,const GLubyte * ids)648 st_DrawAtlasBitmaps(struct gl_context *ctx,
649                     const struct gl_bitmap_atlas *atlas,
650                     GLuint count, const GLubyte *ids)
651 {
652    struct st_context *st = st_context(ctx);
653    struct pipe_context *pipe = st->pipe;
654    struct st_texture_object *stObj = st_texture_object(atlas->texObj);
655    struct pipe_sampler_view *sv;
656    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
657    const float z = ctx->Current.RasterPos[2] * 2.0f - 1.0f;
658    const float *color = ctx->Current.RasterColor;
659    const float clip_x_scale = 2.0f / st->state.fb_width;
660    const float clip_y_scale = 2.0f / st->state.fb_height;
661    const unsigned num_verts = count * 4;
662    const unsigned num_vert_bytes = num_verts * sizeof(struct st_util_vertex);
663    struct st_util_vertex *verts;
664    struct pipe_vertex_buffer vb = {0};
665    unsigned i;
666 
667    if (!st->bitmap.tex_format) {
668       init_bitmap_state(st);
669    }
670 
671    st_flush_bitmap_cache(st);
672 
673    st_validate_state(st, ST_PIPELINE_META);
674    st_invalidate_readpix_cache(st);
675 
676    sv = st_create_texture_sampler_view(pipe, stObj->pt);
677    if (!sv) {
678       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCallLists(bitmap text)");
679       return;
680    }
681 
682    setup_render_state(ctx, sv, color, true);
683 
684    vb.stride = sizeof(struct st_util_vertex);
685 
686    u_upload_alloc(pipe->stream_uploader, 0, num_vert_bytes, 4,
687                   &vb.buffer_offset, &vb.buffer.resource, (void **) &verts);
688 
689    if (unlikely(!verts)) {
690       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCallLists(bitmap text)");
691       goto out;
692    }
693 
694    /* build quads vertex data */
695    for (i = 0; i < count; i++) {
696       const GLfloat epsilon = 0.0001F;
697       const struct gl_bitmap_glyph *g = &atlas->glyphs[ids[i]];
698       const float xmove = g->xmove, ymove = g->ymove;
699       const float xorig = g->xorig, yorig = g->yorig;
700       const float s0 = g->x, t0 = g->y;
701       const float s1 = s0 + g->w, t1 = t0 + g->h;
702       const float x0 = util_ifloor(ctx->Current.RasterPos[0] - xorig + epsilon);
703       const float y0 = util_ifloor(ctx->Current.RasterPos[1] - yorig + epsilon);
704       const float x1 = x0 + g->w, y1 = y0 + g->h;
705       const float clip_x0 = x0 * clip_x_scale - 1.0f;
706       const float clip_y0 = y0 * clip_y_scale - 1.0f;
707       const float clip_x1 = x1 * clip_x_scale - 1.0f;
708       const float clip_y1 = y1 * clip_y_scale - 1.0f;
709 
710       /* lower-left corner */
711       verts->x = clip_x0;
712       verts->y = clip_y0;
713       verts->z = z;
714       verts->r = color[0];
715       verts->g = color[1];
716       verts->b = color[2];
717       verts->a = color[3];
718       verts->s = s0;
719       verts->t = t0;
720       verts++;
721 
722       /* lower-right corner */
723       verts->x = clip_x1;
724       verts->y = clip_y0;
725       verts->z = z;
726       verts->r = color[0];
727       verts->g = color[1];
728       verts->b = color[2];
729       verts->a = color[3];
730       verts->s = s1;
731       verts->t = t0;
732       verts++;
733 
734       /* upper-right corner */
735       verts->x = clip_x1;
736       verts->y = clip_y1;
737       verts->z = z;
738       verts->r = color[0];
739       verts->g = color[1];
740       verts->b = color[2];
741       verts->a = color[3];
742       verts->s = s1;
743       verts->t = t1;
744       verts++;
745 
746       /* upper-left corner */
747       verts->x = clip_x0;
748       verts->y = clip_y1;
749       verts->z = z;
750       verts->r = color[0];
751       verts->g = color[1];
752       verts->b = color[2];
753       verts->a = color[3];
754       verts->s = s0;
755       verts->t = t1;
756       verts++;
757 
758       /* Update the raster position */
759       ctx->Current.RasterPos[0] += xmove;
760       ctx->Current.RasterPos[1] += ymove;
761    }
762 
763    u_upload_unmap(pipe->stream_uploader);
764 
765    cso_set_vertex_buffers(st->cso_context, 0, 1, &vb);
766    cso_draw_arrays(st->cso_context, PIPE_PRIM_QUADS, 0, num_verts);
767 
768 out:
769    restore_render_state(ctx);
770 
771    pipe_resource_reference(&vb.buffer.resource, NULL);
772 
773    pipe_sampler_view_reference(&sv, NULL);
774 
775    /* We uploaded modified constants, need to invalidate them. */
776    st->dirty |= ST_NEW_FS_CONSTANTS;
777 }
778 
779 
780 
781 /** Per-context init */
782 void
st_init_bitmap_functions(struct dd_function_table * functions)783 st_init_bitmap_functions(struct dd_function_table *functions)
784 {
785    functions->Bitmap = st_Bitmap;
786    functions->DrawAtlasBitmaps = st_DrawAtlasBitmaps;
787 }
788 
789 
790 /** Per-context tear-down */
791 void
st_destroy_bitmap(struct st_context * st)792 st_destroy_bitmap(struct st_context *st)
793 {
794    struct pipe_context *pipe = st->pipe;
795    struct st_bitmap_cache *cache = &st->bitmap.cache;
796 
797    if (cache->trans && cache->buffer) {
798       pipe_transfer_unmap(pipe, cache->trans);
799    }
800    pipe_resource_reference(&st->bitmap.cache.texture, NULL);
801 }
802