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