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