st/mesa: move the setup_bitmap_vertex_data() code into draw_bitmap_quad()
[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
180 /**
181 * Setup pipeline state prior to rendering the bitmap textured quad.
182 */
183 static void
184 setup_render_state(struct gl_context *ctx,
185 struct pipe_sampler_view *sv,
186 const GLfloat *color)
187 {
188 struct st_context *st = st_context(ctx);
189 struct cso_context *cso = st->cso_context;
190 struct st_fp_variant *fpv;
191 struct st_fp_variant_key key;
192
193 memset(&key, 0, sizeof(key));
194 key.st = st->has_shareable_shaders ? NULL : st;
195 key.bitmap = GL_TRUE;
196 key.clamp_color = st->clamp_frag_color_in_shader &&
197 st->ctx->Color._ClampFragmentColor;
198
199 fpv = st_get_fp_variant(st, st->fp, &key);
200
201 /* As an optimization, Mesa's fragment programs will sometimes get the
202 * primary color from a statevar/constant rather than a varying variable.
203 * when that's the case, we need to ensure that we use the 'color'
204 * parameter and not the current attribute color (which may have changed
205 * through glRasterPos and state validation.
206 * So, we force the proper color here. Not elegant, but it works.
207 */
208 {
209 GLfloat colorSave[4];
210 COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
211 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
212 st_upload_constants(st, st->fp->Base.Base.Parameters,
213 PIPE_SHADER_FRAGMENT);
214 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
215 }
216
217 cso_save_rasterizer(cso);
218 cso_save_fragment_samplers(cso);
219 cso_save_fragment_sampler_views(cso);
220 cso_save_viewport(cso);
221 cso_save_fragment_shader(cso);
222 cso_save_stream_outputs(cso);
223 cso_save_vertex_shader(cso);
224 cso_save_tessctrl_shader(cso);
225 cso_save_tesseval_shader(cso);
226 cso_save_geometry_shader(cso);
227 cso_save_vertex_elements(cso);
228 cso_save_aux_vertex_buffer_slot(cso);
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 samplers[fpv->bitmap_sampler] =
255 &st->bitmap.samplers[sv->texture->target != PIPE_TEXTURE_RECT];
256 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
257 (const struct pipe_sampler_state **) samplers);
258 }
259
260 /* user textures, plus the bitmap texture */
261 {
262 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
263 uint num = MAX2(fpv->bitmap_sampler + 1,
264 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
265 memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
266 sizeof(sampler_views));
267 sampler_views[fpv->bitmap_sampler] = sv;
268 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
269 }
270
271 /* viewport state: viewport matching window dims */
272 {
273 const GLboolean invert = st->state.fb_orientation == Y_0_TOP;
274 const GLfloat width = (GLfloat)st->state.framebuffer.width;
275 const GLfloat height = (GLfloat)st->state.framebuffer.height;
276 struct pipe_viewport_state vp;
277 vp.scale[0] = 0.5f * width;
278 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
279 vp.scale[2] = 0.5f;
280 vp.translate[0] = 0.5f * width;
281 vp.translate[1] = 0.5f * height;
282 vp.translate[2] = 0.5f;
283 cso_set_viewport(cso, &vp);
284 }
285
286 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
287 cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
288 }
289
290
291 /**
292 * Restore pipeline state after rendering the bitmap textured quad.
293 */
294 static void
295 restore_render_state(struct gl_context *ctx)
296 {
297 struct st_context *st = st_context(ctx);
298 struct cso_context *cso = st->cso_context;
299
300 cso_restore_rasterizer(cso);
301 cso_restore_fragment_samplers(cso);
302 cso_restore_fragment_sampler_views(cso);
303 cso_restore_viewport(cso);
304 cso_restore_fragment_shader(cso);
305 cso_restore_vertex_shader(cso);
306 cso_restore_tessctrl_shader(cso);
307 cso_restore_tesseval_shader(cso);
308 cso_restore_geometry_shader(cso);
309 cso_restore_vertex_elements(cso);
310 cso_restore_aux_vertex_buffer_slot(cso);
311 cso_restore_stream_outputs(cso);
312 }
313
314
315 /**
316 * Render a glBitmap by drawing a textured quad
317 */
318 static void
319 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
320 GLsizei width, GLsizei height,
321 struct pipe_sampler_view *sv,
322 const GLfloat *color)
323 {
324 struct st_context *st = st_context(ctx);
325 struct pipe_context *pipe = st->pipe;
326 struct pipe_resource *vbuf = NULL;
327 const float fb_width = (float) st->state.framebuffer.width;
328 const float fb_height = (float) st->state.framebuffer.height;
329 const float x0 = (float) x;
330 const float x1 = (float) (x + width);
331 const float y0 = (float) y;
332 const float y1 = (float) (y + height);
333 float sLeft = 0.0f, sRight = 1.0f;
334 float tTop = 0.0f, tBot = 1.0f - tTop;
335 const float clip_x0 = x0 / fb_width * 2.0f - 1.0f;
336 const float clip_y0 = y0 / fb_height * 2.0f - 1.0f;
337 const float clip_x1 = x1 / fb_width * 2.0f - 1.0f;
338 const float clip_y1 = y1 / fb_height * 2.0f - 1.0f;
339 float (*vertices)[3][4]; /**< vertex pos + color + texcoord */
340 unsigned offset, i;
341
342 /* limit checks */
343 {
344 /* XXX if the bitmap is larger than the max texture size, break
345 * it up into chunks.
346 */
347 GLuint maxSize = 1 << (pipe->screen->get_param(pipe->screen,
348 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
349 assert(width <= (GLsizei) maxSize);
350 assert(height <= (GLsizei) maxSize);
351 }
352
353 setup_render_state(ctx, sv, color);
354
355 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
356 z = z * 2.0f - 1.0f;
357
358 if (sv->texture->target == PIPE_TEXTURE_RECT) {
359 /* use non-normalized texcoords */
360 sRight = (float) width;
361 tBot = (float) height;
362 }
363
364 u_upload_alloc(st->uploader, 0, 4 * sizeof(vertices[0]), 4,
365 &offset, &vbuf, (void **) &vertices);
366 if (!vbuf) {
367 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBitmap");
368 restore_render_state(ctx);
369 return;
370 }
371
372 /* Positions are in clip coords since we need to do clipping in case
373 * the bitmap quad goes beyond the window bounds.
374 */
375 vertices[0][0][0] = clip_x0;
376 vertices[0][0][1] = clip_y0;
377 vertices[0][2][0] = sLeft;
378 vertices[0][2][1] = tTop;
379
380 vertices[1][0][0] = clip_x1;
381 vertices[1][0][1] = clip_y0;
382 vertices[1][2][0] = sRight;
383 vertices[1][2][1] = tTop;
384
385 vertices[2][0][0] = clip_x1;
386 vertices[2][0][1] = clip_y1;
387 vertices[2][2][0] = sRight;
388 vertices[2][2][1] = tBot;
389
390 vertices[3][0][0] = clip_x0;
391 vertices[3][0][1] = clip_y1;
392 vertices[3][2][0] = sLeft;
393 vertices[3][2][1] = tBot;
394
395 /* same for all verts: */
396 for (i = 0; i < 4; i++) {
397 vertices[i][0][2] = z;
398 vertices[i][0][3] = 1.0f;
399 vertices[i][1][0] = color[0];
400 vertices[i][1][1] = color[1];
401 vertices[i][1][2] = color[2];
402 vertices[i][1][3] = color[3];
403 vertices[i][2][2] = 0.0; /*R*/
404 vertices[i][2][3] = 1.0; /*Q*/
405 }
406
407 u_upload_unmap(st->uploader);
408
409 util_draw_vertex_buffer(pipe, st->cso_context, vbuf,
410 cso_get_aux_vertex_buffer_slot(st->cso_context),
411 offset,
412 PIPE_PRIM_TRIANGLE_FAN,
413 4, /* verts */
414 3); /* attribs/vert */
415
416 restore_render_state(ctx);
417
418 pipe_resource_reference(&vbuf, NULL);
419
420 /* We uploaded modified constants, need to invalidate them. */
421 st->dirty.mesa |= _NEW_PROGRAM_CONSTANTS;
422 }
423
424
425 static void
426 reset_cache(struct st_context *st)
427 {
428 struct bitmap_cache *cache = st->bitmap.cache;
429
430 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
431 cache->empty = GL_TRUE;
432
433 cache->xmin = 1000000;
434 cache->xmax = -1000000;
435 cache->ymin = 1000000;
436 cache->ymax = -1000000;
437
438 assert(!cache->texture);
439
440 /* allocate a new texture */
441 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
442 st->bitmap.tex_format, 0,
443 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
444 1, 1, 0,
445 PIPE_BIND_SAMPLER_VIEW);
446 }
447
448
449 /** Print bitmap image to stdout (debug) */
450 static void
451 print_cache(const struct bitmap_cache *cache)
452 {
453 int i, j, k;
454
455 for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
456 k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
457 for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
458 if (cache->buffer[k])
459 printf("X");
460 else
461 printf(" ");
462 k++;
463 }
464 printf("\n");
465 }
466 }
467
468
469 /**
470 * Create gallium pipe_transfer object for the bitmap cache.
471 */
472 static void
473 create_cache_trans(struct st_context *st)
474 {
475 struct pipe_context *pipe = st->pipe;
476 struct bitmap_cache *cache = st->bitmap.cache;
477
478 if (cache->trans)
479 return;
480
481 /* Map the texture transfer.
482 * Subsequent glBitmap calls will write into the texture image.
483 */
484 cache->buffer = pipe_transfer_map(pipe, cache->texture, 0, 0,
485 PIPE_TRANSFER_WRITE, 0, 0,
486 BITMAP_CACHE_WIDTH,
487 BITMAP_CACHE_HEIGHT, &cache->trans);
488
489 /* init image to all 0xff */
490 memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
491 }
492
493
494 /**
495 * If there's anything in the bitmap cache, draw/flush it now.
496 */
497 void
498 st_flush_bitmap_cache(struct st_context *st)
499 {
500 if (!st->bitmap.cache->empty) {
501 struct bitmap_cache *cache = st->bitmap.cache;
502 struct pipe_context *pipe = st->pipe;
503 struct pipe_sampler_view *sv;
504
505 assert(cache->xmin <= cache->xmax);
506
507 if (0)
508 printf("flush bitmap, size %d x %d at %d, %d\n",
509 cache->xmax - cache->xmin,
510 cache->ymax - cache->ymin,
511 cache->xpos, cache->ypos);
512
513 /* The texture transfer has been mapped until now.
514 * So unmap and release the texture transfer before drawing.
515 */
516 if (cache->trans && cache->buffer) {
517 if (0)
518 print_cache(cache);
519 pipe_transfer_unmap(pipe, cache->trans);
520 cache->buffer = NULL;
521 cache->trans = NULL;
522 }
523
524 sv = st_create_texture_sampler_view(st->pipe, cache->texture);
525 if (sv) {
526 draw_bitmap_quad(st->ctx,
527 cache->xpos,
528 cache->ypos,
529 cache->zpos,
530 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
531 sv,
532 cache->color);
533
534 pipe_sampler_view_reference(&sv, NULL);
535 }
536
537 /* release/free the texture */
538 pipe_resource_reference(&cache->texture, NULL);
539
540 reset_cache(st);
541 }
542 }
543
544
545 /**
546 * Try to accumulate this glBitmap call in the bitmap cache.
547 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
548 */
549 static GLboolean
550 accum_bitmap(struct gl_context *ctx,
551 GLint x, GLint y, GLsizei width, GLsizei height,
552 const struct gl_pixelstore_attrib *unpack,
553 const GLubyte *bitmap )
554 {
555 struct st_context *st = ctx->st;
556 struct bitmap_cache *cache = st->bitmap.cache;
557 int px = -999, py = -999;
558 const GLfloat z = st->ctx->Current.RasterPos[2];
559
560 if (width > BITMAP_CACHE_WIDTH ||
561 height > BITMAP_CACHE_HEIGHT)
562 return GL_FALSE; /* too big to cache */
563
564 if (!cache->empty) {
565 px = x - cache->xpos; /* pos in buffer */
566 py = y - cache->ypos;
567 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
568 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
569 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
570 ((fabs(z - cache->zpos) > Z_EPSILON))) {
571 /* This bitmap would extend beyond cache bounds, or the bitmap
572 * color is changing
573 * so flush and continue.
574 */
575 st_flush_bitmap_cache(st);
576 }
577 }
578
579 if (cache->empty) {
580 /* Initialize. Center bitmap vertically in the buffer. */
581 px = 0;
582 py = (BITMAP_CACHE_HEIGHT - height) / 2;
583 cache->xpos = x;
584 cache->ypos = y - py;
585 cache->zpos = z;
586 cache->empty = GL_FALSE;
587 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
588 }
589
590 assert(px != -999);
591 assert(py != -999);
592
593 if (x < cache->xmin)
594 cache->xmin = x;
595 if (y < cache->ymin)
596 cache->ymin = y;
597 if (x + width > cache->xmax)
598 cache->xmax = x + width;
599 if (y + height > cache->ymax)
600 cache->ymax = y + height;
601
602 /* create the transfer if needed */
603 create_cache_trans(st);
604
605 /* PBO source... */
606 bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
607 if (!bitmap) {
608 return FALSE;
609 }
610
611 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
612 cache->buffer, BITMAP_CACHE_WIDTH);
613
614 _mesa_unmap_pbo_source(ctx, unpack);
615
616 return GL_TRUE; /* accumulated */
617 }
618
619
620
621 /**
622 * Called via ctx->Driver.Bitmap()
623 */
624 static void
625 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
626 GLsizei width, GLsizei height,
627 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
628 {
629 struct st_context *st = st_context(ctx);
630 struct pipe_resource *pt;
631
632 assert(width > 0);
633 assert(height > 0);
634
635 /* We only need to validate state of the st dirty flags are set or
636 * any non-_NEW_PROGRAM_CONSTANTS mesa flags are set. The VS we use
637 * for bitmap drawing uses no constants and the FS constants are
638 * explicitly uploaded in the draw_bitmap_quad() function.
639 */
640 if ((st->dirty.mesa & ~_NEW_PROGRAM_CONSTANTS) || st->dirty.st) {
641 st_validate_state(st);
642 }
643
644 if (!st->bitmap.vs) {
645 /* create pass-through vertex shader now */
646 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
647 TGSI_SEMANTIC_COLOR,
648 st->needs_texcoord_semantic ? TGSI_SEMANTIC_TEXCOORD :
649 TGSI_SEMANTIC_GENERIC };
650 const uint semantic_indexes[] = { 0, 0, 0 };
651 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
652 semantic_names,
653 semantic_indexes,
654 FALSE);
655 }
656
657 if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
658 return;
659
660 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
661 if (pt) {
662 struct pipe_sampler_view *sv =
663 st_create_texture_sampler_view(st->pipe, pt);
664
665 assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
666
667 if (sv) {
668 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
669 width, height, sv,
670 st->ctx->Current.RasterColor);
671
672 pipe_sampler_view_reference(&sv, NULL);
673 }
674
675 /* release/free the texture */
676 pipe_resource_reference(&pt, NULL);
677 }
678 }
679
680
681 /** Per-context init */
682 void
683 st_init_bitmap_functions(struct dd_function_table *functions)
684 {
685 functions->Bitmap = st_Bitmap;
686 }
687
688
689 /** Per-context init */
690 void
691 st_init_bitmap(struct st_context *st)
692 {
693 struct pipe_sampler_state *sampler = &st->bitmap.samplers[0];
694 struct pipe_context *pipe = st->pipe;
695 struct pipe_screen *screen = pipe->screen;
696
697 /* init sampler state once */
698 memset(sampler, 0, sizeof(*sampler));
699 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
700 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
701 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
702 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
703 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
704 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
705 st->bitmap.samplers[1] = *sampler;
706 st->bitmap.samplers[1].normalized_coords = 1;
707
708 /* init baseline rasterizer state once */
709 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
710 st->bitmap.rasterizer.half_pixel_center = 1;
711 st->bitmap.rasterizer.bottom_edge_rule = 1;
712 st->bitmap.rasterizer.depth_clip = 1;
713
714 /* find a usable texture format */
715 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM,
716 PIPE_TEXTURE_2D, 0,
717 PIPE_BIND_SAMPLER_VIEW)) {
718 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
719 }
720 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
721 PIPE_TEXTURE_2D, 0,
722 PIPE_BIND_SAMPLER_VIEW)) {
723 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
724 }
725 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM,
726 PIPE_TEXTURE_2D, 0,
727 PIPE_BIND_SAMPLER_VIEW)) {
728 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
729 }
730 else {
731 /* XXX support more formats */
732 assert(0);
733 }
734
735 /* alloc bitmap cache object */
736 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
737
738 reset_cache(st);
739 }
740
741
742 /** Per-context tear-down */
743 void
744 st_destroy_bitmap(struct st_context *st)
745 {
746 struct pipe_context *pipe = st->pipe;
747 struct bitmap_cache *cache = st->bitmap.cache;
748
749 if (st->bitmap.vs) {
750 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
751 st->bitmap.vs = NULL;
752 }
753
754 if (cache) {
755 if (cache->trans && cache->buffer) {
756 pipe_transfer_unmap(pipe, cache->trans);
757 }
758 pipe_resource_reference(&st->bitmap.cache->texture, NULL);
759 free(st->bitmap.cache);
760 st->bitmap.cache = NULL;
761 }
762 }