34809ad71636a58492a5a8fa89fb24f4ac3bd50e
[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 struct bitmap_cache *cache = st->bitmap.cache;
501
502 if (cache && !cache->empty) {
503 struct pipe_context *pipe = st->pipe;
504 struct pipe_sampler_view *sv;
505
506 assert(cache->xmin <= cache->xmax);
507
508 if (0)
509 printf("flush bitmap, size %d x %d at %d, %d\n",
510 cache->xmax - cache->xmin,
511 cache->ymax - cache->ymin,
512 cache->xpos, cache->ypos);
513
514 /* The texture transfer has been mapped until now.
515 * So unmap and release the texture transfer before drawing.
516 */
517 if (cache->trans && cache->buffer) {
518 if (0)
519 print_cache(cache);
520 pipe_transfer_unmap(pipe, cache->trans);
521 cache->buffer = NULL;
522 cache->trans = NULL;
523 }
524
525 sv = st_create_texture_sampler_view(st->pipe, cache->texture);
526 if (sv) {
527 draw_bitmap_quad(st->ctx,
528 cache->xpos,
529 cache->ypos,
530 cache->zpos,
531 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
532 sv,
533 cache->color);
534
535 pipe_sampler_view_reference(&sv, NULL);
536 }
537
538 /* release/free the texture */
539 pipe_resource_reference(&cache->texture, NULL);
540
541 reset_cache(st);
542 }
543 }
544
545
546 /**
547 * Try to accumulate this glBitmap call in the bitmap cache.
548 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
549 */
550 static GLboolean
551 accum_bitmap(struct gl_context *ctx,
552 GLint x, GLint y, GLsizei width, GLsizei height,
553 const struct gl_pixelstore_attrib *unpack,
554 const GLubyte *bitmap )
555 {
556 struct st_context *st = ctx->st;
557 struct bitmap_cache *cache = st->bitmap.cache;
558 int px = -999, py = -999;
559 const GLfloat z = st->ctx->Current.RasterPos[2];
560
561 if (width > BITMAP_CACHE_WIDTH ||
562 height > BITMAP_CACHE_HEIGHT)
563 return GL_FALSE; /* too big to cache */
564
565 if (!cache->empty) {
566 px = x - cache->xpos; /* pos in buffer */
567 py = y - cache->ypos;
568 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
569 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
570 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
571 ((fabs(z - cache->zpos) > Z_EPSILON))) {
572 /* This bitmap would extend beyond cache bounds, or the bitmap
573 * color is changing
574 * so flush and continue.
575 */
576 st_flush_bitmap_cache(st);
577 }
578 }
579
580 if (cache->empty) {
581 /* Initialize. Center bitmap vertically in the buffer. */
582 px = 0;
583 py = (BITMAP_CACHE_HEIGHT - height) / 2;
584 cache->xpos = x;
585 cache->ypos = y - py;
586 cache->zpos = z;
587 cache->empty = GL_FALSE;
588 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
589 }
590
591 assert(px != -999);
592 assert(py != -999);
593
594 if (x < cache->xmin)
595 cache->xmin = x;
596 if (y < cache->ymin)
597 cache->ymin = y;
598 if (x + width > cache->xmax)
599 cache->xmax = x + width;
600 if (y + height > cache->ymax)
601 cache->ymax = y + height;
602
603 /* create the transfer if needed */
604 create_cache_trans(st);
605
606 /* PBO source... */
607 bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
608 if (!bitmap) {
609 return FALSE;
610 }
611
612 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
613 cache->buffer, BITMAP_CACHE_WIDTH);
614
615 _mesa_unmap_pbo_source(ctx, unpack);
616
617 return GL_TRUE; /* accumulated */
618 }
619
620
621 /**
622 * One-time init for drawing bitmaps.
623 */
624 static void
625 init_bitmap_state(struct st_context *st)
626 {
627 struct pipe_sampler_state *sampler = &st->bitmap.samplers[0];
628 struct pipe_context *pipe = st->pipe;
629 struct pipe_screen *screen = pipe->screen;
630
631 /* This function should only be called once */
632 assert(st->bitmap.cache == NULL);
633
634 /* alloc bitmap cache object */
635 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
636
637 /* init sampler state once */
638 memset(sampler, 0, sizeof(*sampler));
639 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
640 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
641 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
642 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
643 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
644 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
645 st->bitmap.samplers[1] = *sampler;
646 st->bitmap.samplers[1].normalized_coords = 1;
647
648 /* init baseline rasterizer state once */
649 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
650 st->bitmap.rasterizer.half_pixel_center = 1;
651 st->bitmap.rasterizer.bottom_edge_rule = 1;
652 st->bitmap.rasterizer.depth_clip = 1;
653
654 /* find a usable texture format */
655 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM,
656 PIPE_TEXTURE_2D, 0,
657 PIPE_BIND_SAMPLER_VIEW)) {
658 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
659 }
660 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
661 PIPE_TEXTURE_2D, 0,
662 PIPE_BIND_SAMPLER_VIEW)) {
663 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
664 }
665 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM,
666 PIPE_TEXTURE_2D, 0,
667 PIPE_BIND_SAMPLER_VIEW)) {
668 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
669 }
670 else {
671 /* XXX support more formats */
672 assert(0);
673 }
674
675 /* Create the vertex shader */
676 {
677 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
678 TGSI_SEMANTIC_COLOR,
679 st->needs_texcoord_semantic ? TGSI_SEMANTIC_TEXCOORD :
680 TGSI_SEMANTIC_GENERIC };
681 const uint semantic_indexes[] = { 0, 0, 0 };
682 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
683 semantic_names,
684 semantic_indexes,
685 FALSE);
686 }
687
688 reset_cache(st);
689 }
690
691
692 /**
693 * Called via ctx->Driver.Bitmap()
694 */
695 static void
696 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
697 GLsizei width, GLsizei height,
698 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
699 {
700 struct st_context *st = st_context(ctx);
701 struct pipe_resource *pt;
702
703 assert(width > 0);
704 assert(height > 0);
705
706 if (!st->bitmap.cache) {
707 init_bitmap_state(st);
708 }
709
710 /* We only need to validate state of the st dirty flags are set or
711 * any non-_NEW_PROGRAM_CONSTANTS mesa flags are set. The VS we use
712 * for bitmap drawing uses no constants and the FS constants are
713 * explicitly uploaded in the draw_bitmap_quad() function.
714 */
715 if ((st->dirty.mesa & ~_NEW_PROGRAM_CONSTANTS) || st->dirty.st) {
716 st_validate_state(st);
717 }
718
719 if (UseBitmapCache && accum_bitmap(ctx, x, y, width, height, unpack, bitmap))
720 return;
721
722 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
723 if (pt) {
724 struct pipe_sampler_view *sv =
725 st_create_texture_sampler_view(st->pipe, pt);
726
727 assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
728
729 if (sv) {
730 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
731 width, height, sv,
732 st->ctx->Current.RasterColor);
733
734 pipe_sampler_view_reference(&sv, NULL);
735 }
736
737 /* release/free the texture */
738 pipe_resource_reference(&pt, NULL);
739 }
740 }
741
742
743 /** Per-context init */
744 void
745 st_init_bitmap_functions(struct dd_function_table *functions)
746 {
747 functions->Bitmap = st_Bitmap;
748 }
749
750
751 /** Per-context tear-down */
752 void
753 st_destroy_bitmap(struct st_context *st)
754 {
755 struct pipe_context *pipe = st->pipe;
756 struct bitmap_cache *cache = st->bitmap.cache;
757
758 if (st->bitmap.vs) {
759 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
760 st->bitmap.vs = NULL;
761 }
762
763 if (cache) {
764 if (cache->trans && cache->buffer) {
765 pipe_transfer_unmap(pipe, cache->trans);
766 }
767 pipe_resource_reference(&st->bitmap.cache->texture, NULL);
768 free(st->bitmap.cache);
769 st->bitmap.cache = NULL;
770 }
771 }