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