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