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_texture_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_texture_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 pipe_context *pipe = st->pipe;
174    struct cso_context *cso = st->cso_context;
175    struct st_fp_variant *fpv;
176    struct st_fp_variant_key key;
177 
178    memset(&key, 0, sizeof(key));
179    key.st = st->has_shareable_shaders ? NULL : st;
180    key.bitmap = GL_TRUE;
181    key.clamp_color = st->clamp_frag_color_in_shader &&
182                      ctx->Color._ClampFragmentColor;
183    key.lower_alpha_func = COMPARE_FUNC_ALWAYS;
184 
185    fpv = st_get_fp_variant(st, st->fp, &key);
186 
187    /* As an optimization, Mesa's fragment programs will sometimes get the
188     * primary color from a statevar/constant rather than a varying variable.
189     * when that's the case, we need to ensure that we use the 'color'
190     * parameter and not the current attribute color (which may have changed
191     * through glRasterPos and state validation.
192     * So, we force the proper color here.  Not elegant, but it works.
193     */
194    {
195       GLfloat colorSave[4];
196       COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
197       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
198       st_upload_constants(st, &st->fp->Base, MESA_SHADER_FRAGMENT);
199       COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
200    }
201 
202    cso_save_state(cso, (CSO_BIT_RASTERIZER |
203                         CSO_BIT_FRAGMENT_SAMPLERS |
204                         CSO_BIT_VIEWPORT |
205                         CSO_BIT_STREAM_OUTPUTS |
206                         CSO_BIT_VERTEX_ELEMENTS |
207                         CSO_BITS_ALL_SHADERS));
208 
209 
210    /* rasterizer state: just scissor */
211    st->bitmap.rasterizer.scissor = ctx->Scissor.EnableFlags & 1;
212    cso_set_rasterizer(cso, &st->bitmap.rasterizer);
213 
214    /* fragment shader state: TEX lookup program */
215    cso_set_fragment_shader_handle(cso, fpv->base.driver_shader);
216 
217    /* vertex shader state: position + texcoord pass-through */
218    cso_set_vertex_shader_handle(cso, st->passthrough_vs);
219 
220    /* disable other shaders */
221    cso_set_tessctrl_shader_handle(cso, NULL);
222    cso_set_tesseval_shader_handle(cso, NULL);
223    cso_set_geometry_shader_handle(cso, NULL);
224 
225    /* user samplers, plus our bitmap sampler */
226    {
227       struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
228       uint num = MAX2(fpv->bitmap_sampler + 1,
229                       st->state.num_frag_samplers);
230       uint i;
231       for (i = 0; i < st->state.num_frag_samplers; i++) {
232          samplers[i] = &st->state.frag_samplers[i];
233       }
234       if (atlas)
235          samplers[fpv->bitmap_sampler] = &st->bitmap.atlas_sampler;
236       else
237          samplers[fpv->bitmap_sampler] = &st->bitmap.sampler;
238       cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
239                        (const struct pipe_sampler_state **) samplers);
240    }
241 
242    /* user textures, plus the bitmap texture */
243    {
244       struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
245       unsigned num_views =
246          st_get_sampler_views(st, PIPE_SHADER_FRAGMENT,
247                               ctx->FragmentProgram._Current, 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    st->dirty |= ST_NEW_VERTEX_ARRAYS |
284                 ST_NEW_FS_SAMPLER_VIEWS;
285 }
286 
287 
288 /**
289  * Render a glBitmap by drawing a textured quad
290  */
291 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)292 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
293                  GLsizei width, GLsizei height,
294                  struct pipe_sampler_view *sv,
295                  const GLfloat *color)
296 {
297    struct st_context *st = st_context(ctx);
298    const float fb_width = (float) st->state.fb_width;
299    const float fb_height = (float) st->state.fb_height;
300    const float x0 = (float) x;
301    const float x1 = (float) (x + width);
302    const float y0 = (float) y;
303    const float y1 = (float) (y + height);
304    float sLeft = 0.0f, sRight = 1.0f;
305    float tTop = 0.0f, tBot = 1.0f - tTop;
306    const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
307    const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
308    const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
309    const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
310 
311    /* limit checks */
312    {
313       /* XXX if the bitmap is larger than the max texture size, break
314        * it up into chunks.
315        */
316       ASSERTED GLuint maxSize =
317          st->screen->get_param(st->screen, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
318       assert(width <= (GLsizei) maxSize);
319       assert(height <= (GLsizei) maxSize);
320    }
321 
322    setup_render_state(ctx, sv, color, false);
323 
324    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
325    z = z * 2.0f - 1.0f;
326 
327    if (sv->texture->target == PIPE_TEXTURE_RECT) {
328       /* use non-normalized texcoords */
329       sRight = (float) width;
330       tBot = (float) height;
331    }
332 
333    if (!st_draw_quad(st, clip_x0, clip_y0, clip_x1, clip_y1, z,
334                      sLeft, tBot, sRight, tTop, color, 0)) {
335       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBitmap");
336    }
337 
338    restore_render_state(ctx);
339 
340    /* We uploaded modified constants, need to invalidate them. */
341    st->dirty |= ST_NEW_FS_CONSTANTS;
342 }
343 
344 
345 static void
reset_cache(struct st_context * st)346 reset_cache(struct st_context *st)
347 {
348    struct st_bitmap_cache *cache = &st->bitmap.cache;
349 
350    /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
351    cache->empty = GL_TRUE;
352 
353    cache->xmin = 1000000;
354    cache->xmax = -1000000;
355    cache->ymin = 1000000;
356    cache->ymax = -1000000;
357 
358    assert(!cache->texture);
359 
360    /* allocate a new texture */
361    cache->texture = st_texture_create(st, st->internal_target,
362                                       st->bitmap.tex_format, 0,
363                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
364                                       1, 1, 0,
365 				      PIPE_BIND_SAMPLER_VIEW);
366 }
367 
368 
369 /** Print bitmap image to stdout (debug) */
370 static void
print_cache(const struct st_bitmap_cache * cache)371 print_cache(const struct st_bitmap_cache *cache)
372 {
373    int i, j, k;
374 
375    for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
376       k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
377       for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
378          if (cache->buffer[k])
379             printf("X");
380          else
381             printf(" ");
382          k++;
383       }
384       printf("\n");
385    }
386 }
387 
388 
389 /**
390  * Create gallium pipe_transfer object for the bitmap cache.
391  */
392 static void
create_cache_trans(struct st_context * st)393 create_cache_trans(struct st_context *st)
394 {
395    struct pipe_context *pipe = st->pipe;
396    struct st_bitmap_cache *cache = &st->bitmap.cache;
397 
398    if (cache->trans)
399       return;
400 
401    /* Map the texture transfer.
402     * Subsequent glBitmap calls will write into the texture image.
403     */
404    cache->buffer = pipe_texture_map(pipe, cache->texture, 0, 0,
405                                      PIPE_MAP_WRITE, 0, 0,
406                                      BITMAP_CACHE_WIDTH,
407                                      BITMAP_CACHE_HEIGHT, &cache->trans);
408 
409    /* init image to all 0xff */
410    memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
411 }
412 
413 
414 /**
415  * If there's anything in the bitmap cache, draw/flush it now.
416  */
417 void
st_flush_bitmap_cache(struct st_context * st)418 st_flush_bitmap_cache(struct st_context *st)
419 {
420    struct st_bitmap_cache *cache = &st->bitmap.cache;
421 
422    if (!cache->empty) {
423       struct pipe_context *pipe = st->pipe;
424       struct pipe_sampler_view *sv;
425 
426       assert(cache->xmin <= cache->xmax);
427 
428       if (0)
429          printf("flush bitmap, size %d x %d  at %d, %d\n",
430                 cache->xmax - cache->xmin,
431                 cache->ymax - cache->ymin,
432                 cache->xpos, cache->ypos);
433 
434       /* The texture transfer has been mapped until now.
435        * So unmap and release the texture transfer before drawing.
436        */
437       if (cache->trans && cache->buffer) {
438          if (0)
439             print_cache(cache);
440          pipe_texture_unmap(pipe, cache->trans);
441          cache->buffer = NULL;
442          cache->trans = NULL;
443       }
444 
445       sv = st_create_texture_sampler_view(st->pipe, cache->texture);
446       if (sv) {
447          draw_bitmap_quad(st->ctx,
448                           cache->xpos,
449                           cache->ypos,
450                           cache->zpos,
451                           BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
452                           sv,
453                           cache->color);
454       }
455 
456       /* release/free the texture */
457       pipe_resource_reference(&cache->texture, NULL);
458 
459       reset_cache(st);
460    }
461 }
462 
463 
464 /**
465  * Try to accumulate this glBitmap call in the bitmap cache.
466  * \return  GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
467  */
468 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)469 accum_bitmap(struct gl_context *ctx,
470              GLint x, GLint y, GLsizei width, GLsizei height,
471              const struct gl_pixelstore_attrib *unpack,
472              const GLubyte *bitmap )
473 {
474    struct st_context *st = ctx->st;
475    struct st_bitmap_cache *cache = &st->bitmap.cache;
476    int px = -999, py = -999;
477    const GLfloat z = ctx->Current.RasterPos[2];
478 
479    if (width > BITMAP_CACHE_WIDTH ||
480        height > BITMAP_CACHE_HEIGHT)
481       return GL_FALSE; /* too big to cache */
482 
483    if (!cache->empty) {
484       px = x - cache->xpos;  /* pos in buffer */
485       py = y - cache->ypos;
486       if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
487           py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
488           !TEST_EQ_4V(ctx->Current.RasterColor, cache->color) ||
489           ((fabsf(z - cache->zpos) > Z_EPSILON))) {
490          /* This bitmap would extend beyond cache bounds, or the bitmap
491           * color is changing
492           * so flush and continue.
493           */
494          st_flush_bitmap_cache(st);
495       }
496    }
497 
498    if (cache->empty) {
499       /* Initialize.  Center bitmap vertically in the buffer. */
500       px = 0;
501       py = (BITMAP_CACHE_HEIGHT - height) / 2;
502       cache->xpos = x;
503       cache->ypos = y - py;
504       cache->zpos = z;
505       cache->empty = GL_FALSE;
506       COPY_4FV(cache->color, ctx->Current.RasterColor);
507    }
508 
509    assert(px != -999);
510    assert(py != -999);
511 
512    if (x < cache->xmin)
513       cache->xmin = x;
514    if (y < cache->ymin)
515       cache->ymin = y;
516    if (x + width > cache->xmax)
517       cache->xmax = x + width;
518    if (y + height > cache->ymax)
519       cache->ymax = y + height;
520 
521    /* create the transfer if needed */
522    create_cache_trans(st);
523 
524    /* PBO source... */
525    bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
526    if (!bitmap) {
527       return FALSE;
528    }
529 
530    unpack_bitmap(st, px, py, width, height, unpack, bitmap,
531                  cache->buffer, BITMAP_CACHE_WIDTH);
532 
533    _mesa_unmap_pbo_source(ctx, unpack);
534 
535    return GL_TRUE; /* accumulated */
536 }
537 
538 
539 /**
540  * One-time init for drawing bitmaps.
541  */
542 static void
init_bitmap_state(struct st_context * st)543 init_bitmap_state(struct st_context *st)
544 {
545    struct pipe_screen *screen = st->screen;
546 
547    /* This function should only be called once */
548    assert(!st->bitmap.tex_format);
549 
550    assert(st->internal_target == PIPE_TEXTURE_2D ||
551           st->internal_target == PIPE_TEXTURE_RECT);
552 
553    /* init sampler state once */
554    memset(&st->bitmap.sampler, 0, sizeof(st->bitmap.sampler));
555    st->bitmap.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
556    st->bitmap.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
557    st->bitmap.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
558    st->bitmap.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
559    st->bitmap.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
560    st->bitmap.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
561    st->bitmap.sampler.normalized_coords = st->internal_target == PIPE_TEXTURE_2D;
562 
563    st->bitmap.atlas_sampler = st->bitmap.sampler;
564    st->bitmap.atlas_sampler.normalized_coords = 0;
565 
566    /* init baseline rasterizer state once */
567    memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
568    st->bitmap.rasterizer.half_pixel_center = 1;
569    st->bitmap.rasterizer.bottom_edge_rule = 1;
570    st->bitmap.rasterizer.depth_clip_near = 1;
571    st->bitmap.rasterizer.depth_clip_far = 1;
572 
573    /* find a usable texture format */
574    if (screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
575                                    st->internal_target, 0, 0,
576                                    PIPE_BIND_SAMPLER_VIEW)) {
577       st->bitmap.tex_format = PIPE_FORMAT_R8_UNORM;
578    }
579    else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
580                                         st->internal_target, 0, 0,
581                                         PIPE_BIND_SAMPLER_VIEW)) {
582       st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
583    }
584    else {
585       /* XXX support more formats */
586       assert(0);
587    }
588 
589    /* Create the vertex shader */
590    st_make_passthrough_vertex_shader(st);
591 
592    reset_cache(st);
593 }
594 
595 
596 /**
597  * Called via ctx->Driver.Bitmap()
598  */
599 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)600 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
601           GLsizei width, GLsizei height,
602           const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
603 {
604    struct st_context *st = st_context(ctx);
605    struct pipe_resource *pt;
606 
607    assert(width > 0);
608    assert(height > 0);
609 
610    st_invalidate_readpix_cache(st);
611 
612    if (!st->bitmap.tex_format) {
613       init_bitmap_state(st);
614    }
615 
616    /* We only need to validate any non-ST_NEW_CONSTANTS state. The VS we use
617     * for bitmap drawing uses no constants and the FS constants are
618     * explicitly uploaded in the draw_bitmap_quad() function.
619     */
620    if ((st->dirty | ctx->NewDriverState) & st->active_states &
621        ~ST_NEW_CONSTANTS & ST_PIPELINE_RENDER_STATE_MASK ||
622        st->gfx_shaders_may_be_dirty) {
623       st_validate_state(st, ST_PIPELINE_META);
624    }
625 
626    if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
627       return;
628 
629    pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
630    if (pt) {
631       struct pipe_sampler_view *sv =
632          st_create_texture_sampler_view(st->pipe, pt);
633 
634       assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
635 
636       if (sv) {
637          draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
638                           width, height, sv, ctx->Current.RasterColor);
639       }
640 
641       /* release/free the texture */
642       pipe_resource_reference(&pt, NULL);
643    }
644 }
645 
646 
647 /**
648  * Called via ctx->Driver.DrawAtlasBitmap()
649  */
650 static void
st_DrawAtlasBitmaps(struct gl_context * ctx,const struct gl_bitmap_atlas * atlas,GLuint count,const GLubyte * ids)651 st_DrawAtlasBitmaps(struct gl_context *ctx,
652                     const struct gl_bitmap_atlas *atlas,
653                     GLuint count, const GLubyte *ids)
654 {
655    struct st_context *st = st_context(ctx);
656    struct pipe_context *pipe = st->pipe;
657    struct st_texture_object *stObj = st_texture_object(atlas->texObj);
658    struct pipe_sampler_view *sv;
659    /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
660    const float z = ctx->Current.RasterPos[2] * 2.0f - 1.0f;
661    const float *color = ctx->Current.RasterColor;
662    const float clip_x_scale = 2.0f / st->state.fb_width;
663    const float clip_y_scale = 2.0f / st->state.fb_height;
664    const unsigned num_verts = count * 4;
665    const unsigned num_vert_bytes = num_verts * sizeof(struct st_util_vertex);
666    struct st_util_vertex *verts;
667    struct pipe_vertex_buffer vb = {0};
668    unsigned i;
669 
670    if (!st->bitmap.tex_format) {
671       init_bitmap_state(st);
672    }
673 
674    st_flush_bitmap_cache(st);
675 
676    st_validate_state(st, ST_PIPELINE_META);
677    st_invalidate_readpix_cache(st);
678 
679    sv = st_create_texture_sampler_view(pipe, stObj->pt);
680    if (!sv) {
681       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCallLists(bitmap text)");
682       return;
683    }
684 
685    setup_render_state(ctx, sv, color, true);
686 
687    vb.stride = sizeof(struct st_util_vertex);
688 
689    u_upload_alloc(pipe->stream_uploader, 0, num_vert_bytes, 4,
690                   &vb.buffer_offset, &vb.buffer.resource, (void **) &verts);
691 
692    if (unlikely(!verts)) {
693       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCallLists(bitmap text)");
694       goto out;
695    }
696 
697    /* build quads vertex data */
698    for (i = 0; i < count; i++) {
699       const GLfloat epsilon = 0.0001F;
700       const struct gl_bitmap_glyph *g = &atlas->glyphs[ids[i]];
701       const float xmove = g->xmove, ymove = g->ymove;
702       const float xorig = g->xorig, yorig = g->yorig;
703       const float s0 = g->x, t0 = g->y;
704       const float s1 = s0 + g->w, t1 = t0 + g->h;
705       const float x0 = util_ifloor(ctx->Current.RasterPos[0] - xorig + epsilon);
706       const float y0 = util_ifloor(ctx->Current.RasterPos[1] - yorig + epsilon);
707       const float x1 = x0 + g->w, y1 = y0 + g->h;
708       const float clip_x0 = x0 * clip_x_scale - 1.0f;
709       const float clip_y0 = y0 * clip_y_scale - 1.0f;
710       const float clip_x1 = x1 * clip_x_scale - 1.0f;
711       const float clip_y1 = y1 * clip_y_scale - 1.0f;
712 
713       /* lower-left corner */
714       verts->x = clip_x0;
715       verts->y = clip_y0;
716       verts->z = z;
717       verts->r = color[0];
718       verts->g = color[1];
719       verts->b = color[2];
720       verts->a = color[3];
721       verts->s = s0;
722       verts->t = t0;
723       verts++;
724 
725       /* lower-right corner */
726       verts->x = clip_x1;
727       verts->y = clip_y0;
728       verts->z = z;
729       verts->r = color[0];
730       verts->g = color[1];
731       verts->b = color[2];
732       verts->a = color[3];
733       verts->s = s1;
734       verts->t = t0;
735       verts++;
736 
737       /* upper-right corner */
738       verts->x = clip_x1;
739       verts->y = clip_y1;
740       verts->z = z;
741       verts->r = color[0];
742       verts->g = color[1];
743       verts->b = color[2];
744       verts->a = color[3];
745       verts->s = s1;
746       verts->t = t1;
747       verts++;
748 
749       /* upper-left corner */
750       verts->x = clip_x0;
751       verts->y = clip_y1;
752       verts->z = z;
753       verts->r = color[0];
754       verts->g = color[1];
755       verts->b = color[2];
756       verts->a = color[3];
757       verts->s = s0;
758       verts->t = t1;
759       verts++;
760 
761       /* Update the raster position */
762       ctx->Current.RasterPos[0] += xmove;
763       ctx->Current.RasterPos[1] += ymove;
764       ctx->PopAttribState |= GL_CURRENT_BIT;
765    }
766 
767    u_upload_unmap(pipe->stream_uploader);
768 
769    cso_set_vertex_buffers(st->cso_context, 0, 1, &vb);
770    st->last_num_vbuffers = MAX2(st->last_num_vbuffers, 1);
771 
772    cso_draw_arrays(st->cso_context, PIPE_PRIM_QUADS, 0, num_verts);
773 
774 out:
775    restore_render_state(ctx);
776 
777    pipe_resource_reference(&vb.buffer.resource, NULL);
778 
779    /* We uploaded modified constants, need to invalidate them. */
780    st->dirty |= ST_NEW_FS_CONSTANTS;
781 }
782 
783 
784 
785 /** Per-context init */
786 void
st_init_bitmap_functions(struct dd_function_table * functions)787 st_init_bitmap_functions(struct dd_function_table *functions)
788 {
789    functions->Bitmap = st_Bitmap;
790    functions->DrawAtlasBitmaps = st_DrawAtlasBitmaps;
791 }
792 
793 
794 /** Per-context tear-down */
795 void
st_destroy_bitmap(struct st_context * st)796 st_destroy_bitmap(struct st_context *st)
797 {
798    struct pipe_context *pipe = st->pipe;
799    struct st_bitmap_cache *cache = &st->bitmap.cache;
800 
801    if (cache->trans && cache->buffer) {
802       pipe_texture_unmap(pipe, cache->trans);
803    }
804    pipe_resource_reference(&st->bitmap.cache.texture, NULL);
805 }
806