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