st/mesa: implement glBitmap shader transformation using tgsi_transform_shader
[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;
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, fpv->parameters, PIPE_SHADER_FRAGMENT);
291 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
292 }
293
294
295 /* limit checks */
296 /* XXX if the bitmap is larger than the max texture size, break
297 * it up into chunks.
298 */
299 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
300 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
301 assert(width <= (GLsizei)maxSize);
302 assert(height <= (GLsizei)maxSize);
303
304 cso_save_rasterizer(cso);
305 cso_save_fragment_samplers(cso);
306 cso_save_fragment_sampler_views(cso);
307 cso_save_viewport(cso);
308 cso_save_fragment_shader(cso);
309 cso_save_stream_outputs(cso);
310 cso_save_vertex_shader(cso);
311 cso_save_tessctrl_shader(cso);
312 cso_save_tesseval_shader(cso);
313 cso_save_geometry_shader(cso);
314 cso_save_vertex_elements(cso);
315 cso_save_aux_vertex_buffer_slot(cso);
316
317 /* rasterizer state: just scissor */
318 st->bitmap.rasterizer.scissor = ctx->Scissor.EnableFlags & 1;
319 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
320
321 /* fragment shader state: TEX lookup program */
322 cso_set_fragment_shader_handle(cso, fpv->driver_shader);
323
324 /* vertex shader state: position + texcoord pass-through */
325 cso_set_vertex_shader_handle(cso, st->bitmap.vs);
326
327 /* disable other shaders */
328 cso_set_tessctrl_shader_handle(cso, NULL);
329 cso_set_tesseval_shader_handle(cso, NULL);
330 cso_set_geometry_shader_handle(cso, NULL);
331
332 /* user samplers, plus our bitmap sampler */
333 {
334 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
335 uint num = MAX2(fpv->bitmap_sampler + 1,
336 st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
337 uint i;
338 for (i = 0; i < st->state.num_samplers[PIPE_SHADER_FRAGMENT]; i++) {
339 samplers[i] = &st->state.samplers[PIPE_SHADER_FRAGMENT][i];
340 }
341 samplers[fpv->bitmap_sampler] =
342 &st->bitmap.samplers[sv->texture->target != PIPE_TEXTURE_RECT];
343 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
344 (const struct pipe_sampler_state **) samplers);
345 }
346
347 /* user textures, plus the bitmap texture */
348 {
349 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
350 uint num = MAX2(fpv->bitmap_sampler + 1,
351 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
352 memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
353 sizeof(sampler_views));
354 sampler_views[fpv->bitmap_sampler] = sv;
355 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
356 }
357
358 /* viewport state: viewport matching window dims */
359 {
360 const GLboolean invert = st->state.fb_orientation == Y_0_TOP;
361 const GLfloat width = (GLfloat)st->state.framebuffer.width;
362 const GLfloat height = (GLfloat)st->state.framebuffer.height;
363 struct pipe_viewport_state vp;
364 vp.scale[0] = 0.5f * width;
365 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
366 vp.scale[2] = 0.5f;
367 vp.translate[0] = 0.5f * width;
368 vp.translate[1] = 0.5f * height;
369 vp.translate[2] = 0.5f;
370 cso_set_viewport(cso, &vp);
371 }
372
373 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
374 cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
375
376 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
377 z = z * 2.0f - 1.0f;
378
379 /* draw textured quad */
380 setup_bitmap_vertex_data(st, sv->texture->target != PIPE_TEXTURE_RECT,
381 x, y, width, height, z, color, &vbuf, &offset);
382
383 if (vbuf) {
384 util_draw_vertex_buffer(pipe, st->cso_context, vbuf,
385 cso_get_aux_vertex_buffer_slot(st->cso_context),
386 offset,
387 PIPE_PRIM_TRIANGLE_FAN,
388 4, /* verts */
389 3); /* attribs/vert */
390 }
391
392 /* restore state */
393 cso_restore_rasterizer(cso);
394 cso_restore_fragment_samplers(cso);
395 cso_restore_fragment_sampler_views(cso);
396 cso_restore_viewport(cso);
397 cso_restore_fragment_shader(cso);
398 cso_restore_vertex_shader(cso);
399 cso_restore_tessctrl_shader(cso);
400 cso_restore_tesseval_shader(cso);
401 cso_restore_geometry_shader(cso);
402 cso_restore_vertex_elements(cso);
403 cso_restore_aux_vertex_buffer_slot(cso);
404 cso_restore_stream_outputs(cso);
405
406 pipe_resource_reference(&vbuf, NULL);
407 }
408
409
410 static void
411 reset_cache(struct st_context *st)
412 {
413 struct bitmap_cache *cache = st->bitmap.cache;
414
415 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
416 cache->empty = GL_TRUE;
417
418 cache->xmin = 1000000;
419 cache->xmax = -1000000;
420 cache->ymin = 1000000;
421 cache->ymax = -1000000;
422
423 assert(!cache->texture);
424
425 /* allocate a new texture */
426 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
427 st->bitmap.tex_format, 0,
428 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
429 1, 1, 0,
430 PIPE_BIND_SAMPLER_VIEW);
431 }
432
433
434 /** Print bitmap image to stdout (debug) */
435 static void
436 print_cache(const struct bitmap_cache *cache)
437 {
438 int i, j, k;
439
440 for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
441 k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
442 for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
443 if (cache->buffer[k])
444 printf("X");
445 else
446 printf(" ");
447 k++;
448 }
449 printf("\n");
450 }
451 }
452
453
454 /**
455 * Create gallium pipe_transfer object for the bitmap cache.
456 */
457 static void
458 create_cache_trans(struct st_context *st)
459 {
460 struct pipe_context *pipe = st->pipe;
461 struct bitmap_cache *cache = st->bitmap.cache;
462
463 if (cache->trans)
464 return;
465
466 /* Map the texture transfer.
467 * Subsequent glBitmap calls will write into the texture image.
468 */
469 cache->buffer = pipe_transfer_map(pipe, cache->texture, 0, 0,
470 PIPE_TRANSFER_WRITE, 0, 0,
471 BITMAP_CACHE_WIDTH,
472 BITMAP_CACHE_HEIGHT, &cache->trans);
473
474 /* init image to all 0xff */
475 memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
476 }
477
478
479 /**
480 * If there's anything in the bitmap cache, draw/flush it now.
481 */
482 void
483 st_flush_bitmap_cache(struct st_context *st)
484 {
485 if (!st->bitmap.cache->empty) {
486 struct bitmap_cache *cache = st->bitmap.cache;
487
488 struct pipe_context *pipe = st->pipe;
489 struct pipe_sampler_view *sv;
490
491 assert(cache->xmin <= cache->xmax);
492
493 /* printf("flush size %d x %d at %d, %d\n",
494 cache->xmax - cache->xmin,
495 cache->ymax - cache->ymin,
496 cache->xpos, cache->ypos);
497 */
498
499 /* The texture transfer has been mapped until now.
500 * So unmap and release the texture transfer before drawing.
501 */
502 if (cache->trans && cache->buffer) {
503 if (0)
504 print_cache(cache);
505 pipe_transfer_unmap(pipe, cache->trans);
506 cache->buffer = NULL;
507 cache->trans = NULL;
508 }
509
510 sv = st_create_texture_sampler_view(st->pipe, cache->texture);
511 if (sv) {
512 draw_bitmap_quad(st->ctx,
513 cache->xpos,
514 cache->ypos,
515 cache->zpos,
516 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
517 sv,
518 cache->color);
519
520 pipe_sampler_view_reference(&sv, NULL);
521 }
522
523 /* release/free the texture */
524 pipe_resource_reference(&cache->texture, NULL);
525
526 reset_cache(st);
527 }
528 }
529
530
531 /**
532 * Try to accumulate this glBitmap call in the bitmap cache.
533 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
534 */
535 static GLboolean
536 accum_bitmap(struct gl_context *ctx,
537 GLint x, GLint y, GLsizei width, GLsizei height,
538 const struct gl_pixelstore_attrib *unpack,
539 const GLubyte *bitmap )
540 {
541 struct st_context *st = ctx->st;
542 struct bitmap_cache *cache = st->bitmap.cache;
543 int px = -999, py = -999;
544 const GLfloat z = st->ctx->Current.RasterPos[2];
545
546 if (width > BITMAP_CACHE_WIDTH ||
547 height > BITMAP_CACHE_HEIGHT)
548 return GL_FALSE; /* too big to cache */
549
550 if (!cache->empty) {
551 px = x - cache->xpos; /* pos in buffer */
552 py = y - cache->ypos;
553 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
554 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
555 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
556 ((fabs(z - cache->zpos) > Z_EPSILON))) {
557 /* This bitmap would extend beyond cache bounds, or the bitmap
558 * color is changing
559 * so flush and continue.
560 */
561 st_flush_bitmap_cache(st);
562 }
563 }
564
565 if (cache->empty) {
566 /* Initialize. Center bitmap vertically in the buffer. */
567 px = 0;
568 py = (BITMAP_CACHE_HEIGHT - height) / 2;
569 cache->xpos = x;
570 cache->ypos = y - py;
571 cache->zpos = z;
572 cache->empty = GL_FALSE;
573 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
574 }
575
576 assert(px != -999);
577 assert(py != -999);
578
579 if (x < cache->xmin)
580 cache->xmin = x;
581 if (y < cache->ymin)
582 cache->ymin = y;
583 if (x + width > cache->xmax)
584 cache->xmax = x + width;
585 if (y + height > cache->ymax)
586 cache->ymax = y + height;
587
588 /* create the transfer if needed */
589 create_cache_trans(st);
590
591 /* PBO source... */
592 bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
593 if (!bitmap) {
594 return FALSE;
595 }
596
597 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
598 cache->buffer, BITMAP_CACHE_WIDTH);
599
600 _mesa_unmap_pbo_source(ctx, unpack);
601
602 return GL_TRUE; /* accumulated */
603 }
604
605
606
607 /**
608 * Called via ctx->Driver.Bitmap()
609 */
610 static void
611 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
612 GLsizei width, GLsizei height,
613 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
614 {
615 struct st_context *st = st_context(ctx);
616 struct pipe_resource *pt;
617
618 if (width == 0 || height == 0)
619 return;
620
621 st_validate_state(st);
622
623 if (!st->bitmap.vs) {
624 /* create pass-through vertex shader now */
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 if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
637 return;
638
639 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
640 if (pt) {
641 struct pipe_sampler_view *sv =
642 st_create_texture_sampler_view(st->pipe, pt);
643
644 assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
645
646 if (sv) {
647 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
648 width, height, sv,
649 st->ctx->Current.RasterColor);
650
651 pipe_sampler_view_reference(&sv, NULL);
652 }
653
654 /* release/free the texture */
655 pipe_resource_reference(&pt, NULL);
656 }
657 }
658
659
660 /** Per-context init */
661 void
662 st_init_bitmap_functions(struct dd_function_table *functions)
663 {
664 functions->Bitmap = st_Bitmap;
665 }
666
667
668 /** Per-context init */
669 void
670 st_init_bitmap(struct st_context *st)
671 {
672 struct pipe_sampler_state *sampler = &st->bitmap.samplers[0];
673 struct pipe_context *pipe = st->pipe;
674 struct pipe_screen *screen = pipe->screen;
675
676 /* init sampler state once */
677 memset(sampler, 0, sizeof(*sampler));
678 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
679 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
680 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
681 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
682 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
683 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
684 st->bitmap.samplers[1] = *sampler;
685 st->bitmap.samplers[1].normalized_coords = 1;
686
687 /* init baseline rasterizer state once */
688 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
689 st->bitmap.rasterizer.half_pixel_center = 1;
690 st->bitmap.rasterizer.bottom_edge_rule = 1;
691 st->bitmap.rasterizer.depth_clip = 1;
692
693 /* find a usable texture format */
694 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM,
695 PIPE_TEXTURE_2D, 0,
696 PIPE_BIND_SAMPLER_VIEW)) {
697 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
698 }
699 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
700 PIPE_TEXTURE_2D, 0,
701 PIPE_BIND_SAMPLER_VIEW)) {
702 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
703 }
704 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM,
705 PIPE_TEXTURE_2D, 0,
706 PIPE_BIND_SAMPLER_VIEW)) {
707 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
708 }
709 else {
710 /* XXX support more formats */
711 assert(0);
712 }
713
714 /* alloc bitmap cache object */
715 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
716
717 reset_cache(st);
718 }
719
720
721 /** Per-context tear-down */
722 void
723 st_destroy_bitmap(struct st_context *st)
724 {
725 struct pipe_context *pipe = st->pipe;
726 struct bitmap_cache *cache = st->bitmap.cache;
727
728 if (st->bitmap.vs) {
729 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
730 st->bitmap.vs = NULL;
731 }
732
733 if (cache) {
734 if (cache->trans && cache->buffer) {
735 pipe_transfer_unmap(pipe, cache->trans);
736 }
737 pipe_resource_reference(&st->bitmap.cache->texture, NULL);
738 free(st->bitmap.cache);
739 st->bitmap.cache = NULL;
740 }
741 }