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