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