gallium: add start_slot parameter to set_vertex_buffers
[mesa.git] / src / mesa / state_tracker / st_cb_bitmap.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/mfeatures.h"
38 #include "main/pbo.h"
39 #include "program/program.h"
40 #include "program/prog_print.h"
41
42 #include "st_context.h"
43 #include "st_atom.h"
44 #include "st_atom_constbuf.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_draw_quad.h"
54 #include "util/u_simple_shaders.h"
55 #include "util/u_upload_mgr.h"
56 #include "program/prog_instruction.h"
57 #include "cso_cache/cso_context.h"
58
59
60 /**
61 * glBitmaps are drawn as textured quads. The user's bitmap pattern
62 * is stored in a texture image. An alpha8 texture format is used.
63 * The fragment shader samples a bit (texel) from the texture, then
64 * discards the fragment if the bit is off.
65 *
66 * Note that we actually store the inverse image of the bitmap to
67 * simplify the fragment program. An "on" bit gets stored as texel=0x0
68 * and an "off" bit is stored as texel=0xff. Then we kill the
69 * fragment if the negated texel value is less than zero.
70 */
71
72
73 /**
74 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
75 * buffer which is then rendered en mass upon a flush, state change, etc.
76 * A wide, short buffer is used to target the common case of a series
77 * of glBitmap calls being used to draw text.
78 */
79 static GLboolean UseBitmapCache = GL_TRUE;
80
81
82 #define BITMAP_CACHE_WIDTH 512
83 #define BITMAP_CACHE_HEIGHT 32
84
85 struct bitmap_cache
86 {
87 /** Window pos to render the cached image */
88 GLint xpos, ypos;
89 /** Bounds of region used in window coords */
90 GLint xmin, ymin, xmax, ymax;
91
92 GLfloat color[4];
93
94 /** Bitmap's Z position */
95 GLfloat zpos;
96
97 struct pipe_resource *texture;
98 struct pipe_transfer *trans;
99
100 GLboolean empty;
101
102 /** An I8 texture image: */
103 ubyte *buffer;
104 };
105
106
107 /** Epsilon for Z comparisons */
108 #define Z_EPSILON 1e-06
109
110
111 /**
112 * Make fragment program for glBitmap:
113 * Sample the texture and kill the fragment if the bit is 0.
114 * This program will be combined with the user's fragment program.
115 */
116 static struct st_fragment_program *
117 make_bitmap_fragment_program(struct gl_context *ctx, GLuint samplerIndex)
118 {
119 struct st_context *st = st_context(ctx);
120 struct st_fragment_program *stfp;
121 struct gl_program *p;
122 GLuint ic = 0;
123
124 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
125 if (!p)
126 return NULL;
127
128 p->NumInstructions = 3;
129
130 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
131 if (!p->Instructions) {
132 ctx->Driver.DeleteProgram(ctx, p);
133 return NULL;
134 }
135 _mesa_init_instructions(p->Instructions, p->NumInstructions);
136
137 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
138 p->Instructions[ic].Opcode = OPCODE_TEX;
139 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
140 p->Instructions[ic].DstReg.Index = 0;
141 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
142 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
143 p->Instructions[ic].TexSrcUnit = samplerIndex;
144 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
145 ic++;
146
147 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
148 p->Instructions[ic].Opcode = OPCODE_KIL;
149 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
150
151 if (st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM)
152 p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_XXXX;
153
154 p->Instructions[ic].SrcReg[0].Index = 0;
155 p->Instructions[ic].SrcReg[0].Negate = NEGATE_XYZW;
156 ic++;
157
158 /* END; */
159 p->Instructions[ic++].Opcode = OPCODE_END;
160
161 assert(ic == p->NumInstructions);
162
163 p->InputsRead = FRAG_BIT_TEX0;
164 p->OutputsWritten = 0x0;
165 p->SamplersUsed = (1 << samplerIndex);
166
167 stfp = (struct st_fragment_program *) p;
168 stfp->Base.UsesKill = GL_TRUE;
169
170 return stfp;
171 }
172
173
174 static struct gl_program *
175 make_bitmap_fragment_program_glsl(struct st_context *st,
176 struct st_fragment_program *orig,
177 GLuint samplerIndex)
178 {
179 struct gl_context *ctx = st->ctx;
180 struct st_fragment_program *fp = (struct st_fragment_program *)
181 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
182
183 if (!fp)
184 return NULL;
185
186 get_bitmap_visitor(fp, orig->glsl_to_tgsi, samplerIndex);
187 return &fp->Base.Base;
188 }
189
190
191 static int
192 find_free_bit(uint bitfield)
193 {
194 int i;
195 for (i = 0; i < 32; i++) {
196 if ((bitfield & (1 << i)) == 0) {
197 return i;
198 }
199 }
200 return -1;
201 }
202
203
204 /**
205 * Combine basic bitmap fragment program with the user-defined program.
206 * \param st current context
207 * \param fpIn the incoming fragment program
208 * \param fpOut the new fragment program which does fragment culling
209 * \param bitmap_sampler sampler number for the bitmap texture
210 */
211 void
212 st_make_bitmap_fragment_program(struct st_context *st,
213 struct gl_fragment_program *fpIn,
214 struct gl_fragment_program **fpOut,
215 GLuint *bitmap_sampler)
216 {
217 struct st_fragment_program *bitmap_prog;
218 struct st_fragment_program *stfpIn = (struct st_fragment_program *) fpIn;
219 struct gl_program *newProg;
220 uint sampler;
221
222 /*
223 * Generate new program which is the user-defined program prefixed
224 * with the bitmap sampler/kill instructions.
225 */
226 sampler = find_free_bit(fpIn->Base.SamplersUsed);
227
228 if (stfpIn->glsl_to_tgsi)
229 newProg = make_bitmap_fragment_program_glsl(st, stfpIn, sampler);
230 else {
231 bitmap_prog = make_bitmap_fragment_program(st->ctx, sampler);
232
233 newProg = _mesa_combine_programs(st->ctx,
234 &bitmap_prog->Base.Base,
235 &fpIn->Base);
236 /* done with this after combining */
237 st_reference_fragprog(st, &bitmap_prog, NULL);
238 }
239
240 #if 0
241 {
242 printf("Combined bitmap program:\n");
243 _mesa_print_program(newProg);
244 printf("InputsRead: 0x%x\n", newProg->InputsRead);
245 printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
246 _mesa_print_parameter_list(newProg->Parameters);
247 }
248 #endif
249
250 /* return results */
251 *fpOut = (struct gl_fragment_program *) newProg;
252 *bitmap_sampler = sampler;
253 }
254
255
256 /**
257 * Copy user-provide bitmap bits into texture buffer, expanding
258 * bits into texels.
259 * "On" bits will set texels to 0x0.
260 * "Off" bits will not modify texels.
261 * Note that the image is actually going to be upside down in
262 * the texture. We deal with that with texcoords.
263 */
264 static void
265 unpack_bitmap(struct st_context *st,
266 GLint px, GLint py, GLsizei width, GLsizei height,
267 const struct gl_pixelstore_attrib *unpack,
268 const GLubyte *bitmap,
269 ubyte *destBuffer, uint destStride)
270 {
271 destBuffer += py * destStride + px;
272
273 _mesa_expand_bitmap(width, height, unpack, bitmap,
274 destBuffer, destStride, 0x0);
275 }
276
277
278 /**
279 * Create a texture which represents a bitmap image.
280 */
281 static struct pipe_resource *
282 make_bitmap_texture(struct gl_context *ctx, GLsizei width, GLsizei height,
283 const struct gl_pixelstore_attrib *unpack,
284 const GLubyte *bitmap)
285 {
286 struct st_context *st = st_context(ctx);
287 struct pipe_context *pipe = st->pipe;
288 struct pipe_transfer *transfer;
289 ubyte *dest;
290 struct pipe_resource *pt;
291
292 /* PBO source... */
293 bitmap = _mesa_map_pbo_source(ctx, unpack, bitmap);
294 if (!bitmap) {
295 return NULL;
296 }
297
298 /**
299 * Create texture to hold bitmap pattern.
300 */
301 pt = st_texture_create(st, st->internal_target, st->bitmap.tex_format,
302 0, width, height, 1, 1,
303 PIPE_BIND_SAMPLER_VIEW);
304 if (!pt) {
305 _mesa_unmap_pbo_source(ctx, unpack);
306 return NULL;
307 }
308
309 dest = pipe_transfer_map(st->pipe, pt, 0, 0,
310 PIPE_TRANSFER_WRITE,
311 0, 0, width, height, &transfer);
312
313 /* Put image into texture transfer */
314 memset(dest, 0xff, height * transfer->stride);
315 unpack_bitmap(st, 0, 0, width, height, unpack, bitmap,
316 dest, transfer->stride);
317
318 _mesa_unmap_pbo_source(ctx, unpack);
319
320 /* Release transfer */
321 pipe_transfer_unmap(pipe, transfer);
322 return pt;
323 }
324
325 static void
326 setup_bitmap_vertex_data(struct st_context *st, bool normalized,
327 int x, int y, int width, int height,
328 float z, const float color[4],
329 struct pipe_resource **vbuf,
330 unsigned *vbuf_offset)
331 {
332 const GLfloat fb_width = (GLfloat)st->state.framebuffer.width;
333 const GLfloat fb_height = (GLfloat)st->state.framebuffer.height;
334 const GLfloat x0 = (GLfloat)x;
335 const GLfloat x1 = (GLfloat)(x + width);
336 const GLfloat y0 = (GLfloat)y;
337 const GLfloat y1 = (GLfloat)(y + height);
338 GLfloat sLeft = (GLfloat)0.0, sRight = (GLfloat)1.0;
339 GLfloat tTop = (GLfloat)0.0, tBot = (GLfloat)1.0 - tTop;
340 const GLfloat clip_x0 = (GLfloat)(x0 / fb_width * 2.0 - 1.0);
341 const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
342 const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
343 const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
344 GLuint i;
345 float (*vertices)[3][4]; /**< vertex pos + color + texcoord */
346
347 if(!normalized)
348 {
349 sRight = (GLfloat) width;
350 tBot = (GLfloat) height;
351 }
352
353 u_upload_alloc(st->uploader, 0, 4 * sizeof(vertices[0]), vbuf_offset, vbuf,
354 (void**)&vertices);
355 if (!vbuf) {
356 return;
357 }
358
359 /* Positions are in clip coords since we need to do clipping in case
360 * the bitmap quad goes beyond the window bounds.
361 */
362 vertices[0][0][0] = clip_x0;
363 vertices[0][0][1] = clip_y0;
364 vertices[0][2][0] = sLeft;
365 vertices[0][2][1] = tTop;
366
367 vertices[1][0][0] = clip_x1;
368 vertices[1][0][1] = clip_y0;
369 vertices[1][2][0] = sRight;
370 vertices[1][2][1] = tTop;
371
372 vertices[2][0][0] = clip_x1;
373 vertices[2][0][1] = clip_y1;
374 vertices[2][2][0] = sRight;
375 vertices[2][2][1] = tBot;
376
377 vertices[3][0][0] = clip_x0;
378 vertices[3][0][1] = clip_y1;
379 vertices[3][2][0] = sLeft;
380 vertices[3][2][1] = tBot;
381
382 /* same for all verts: */
383 for (i = 0; i < 4; i++) {
384 vertices[i][0][2] = z;
385 vertices[i][0][3] = 1.0f;
386 vertices[i][1][0] = color[0];
387 vertices[i][1][1] = color[1];
388 vertices[i][1][2] = color[2];
389 vertices[i][1][3] = color[3];
390 vertices[i][2][2] = 0.0; /*R*/
391 vertices[i][2][3] = 1.0; /*Q*/
392 }
393
394 u_upload_unmap(st->uploader);
395 }
396
397
398
399 /**
400 * Render a glBitmap by drawing a textured quad
401 */
402 static void
403 draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
404 GLsizei width, GLsizei height,
405 struct pipe_sampler_view *sv,
406 const GLfloat *color)
407 {
408 struct st_context *st = st_context(ctx);
409 struct pipe_context *pipe = st->pipe;
410 struct cso_context *cso = st->cso_context;
411 struct st_fp_variant *fpv;
412 struct st_fp_variant_key key;
413 GLuint maxSize;
414 GLuint offset;
415 struct pipe_resource *vbuf = NULL;
416
417 memset(&key, 0, sizeof(key));
418 key.st = st;
419 key.bitmap = GL_TRUE;
420 key.clamp_color = st->clamp_frag_color_in_shader &&
421 st->ctx->Color._ClampFragmentColor &&
422 !st->ctx->DrawBuffer->_IntegerColor;
423
424 fpv = st_get_fp_variant(st, st->fp, &key);
425
426 /* As an optimization, Mesa's fragment programs will sometimes get the
427 * primary color from a statevar/constant rather than a varying variable.
428 * when that's the case, we need to ensure that we use the 'color'
429 * parameter and not the current attribute color (which may have changed
430 * through glRasterPos and state validation.
431 * So, we force the proper color here. Not elegant, but it works.
432 */
433 {
434 GLfloat colorSave[4];
435 COPY_4V(colorSave, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
436 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], color);
437 st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
438 COPY_4V(ctx->Current.Attrib[VERT_ATTRIB_COLOR0], colorSave);
439 }
440
441
442 /* limit checks */
443 /* XXX if the bitmap is larger than the max texture size, break
444 * it up into chunks.
445 */
446 maxSize = 1 << (pipe->screen->get_param(pipe->screen,
447 PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
448 assert(width <= (GLsizei)maxSize);
449 assert(height <= (GLsizei)maxSize);
450
451 cso_save_rasterizer(cso);
452 cso_save_samplers(cso, PIPE_SHADER_FRAGMENT);
453 cso_save_sampler_views(cso, PIPE_SHADER_FRAGMENT);
454 cso_save_viewport(cso);
455 cso_save_fragment_shader(cso);
456 cso_save_stream_outputs(cso);
457 cso_save_vertex_shader(cso);
458 cso_save_geometry_shader(cso);
459 cso_save_vertex_elements(cso);
460 cso_save_aux_vertex_buffer_slot(cso);
461
462 /* rasterizer state: just scissor */
463 st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
464 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
465
466 /* fragment shader state: TEX lookup program */
467 cso_set_fragment_shader_handle(cso, fpv->driver_shader);
468
469 /* vertex shader state: position + texcoord pass-through */
470 cso_set_vertex_shader_handle(cso, st->bitmap.vs);
471
472 /* geometry shader state: disabled */
473 cso_set_geometry_shader_handle(cso, NULL);
474
475 /* user samplers, plus our bitmap sampler */
476 {
477 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
478 uint num = MAX2(fpv->bitmap_sampler + 1,
479 st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
480 uint i;
481 for (i = 0; i < st->state.num_samplers[PIPE_SHADER_FRAGMENT]; i++) {
482 samplers[i] = &st->state.samplers[PIPE_SHADER_FRAGMENT][i];
483 }
484 samplers[fpv->bitmap_sampler] =
485 &st->bitmap.samplers[sv->texture->target != PIPE_TEXTURE_RECT];
486 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, num,
487 (const struct pipe_sampler_state **) samplers);
488 }
489
490 /* user textures, plus the bitmap texture */
491 {
492 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
493 uint num = MAX2(fpv->bitmap_sampler + 1,
494 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
495 memcpy(sampler_views, st->state.sampler_views[PIPE_SHADER_FRAGMENT],
496 sizeof(sampler_views));
497 sampler_views[fpv->bitmap_sampler] = sv;
498 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, num, sampler_views);
499 }
500
501 /* viewport state: viewport matching window dims */
502 {
503 const GLboolean invert = st->state.fb_orientation == Y_0_TOP;
504 const GLfloat width = (GLfloat)st->state.framebuffer.width;
505 const GLfloat height = (GLfloat)st->state.framebuffer.height;
506 struct pipe_viewport_state vp;
507 vp.scale[0] = 0.5f * width;
508 vp.scale[1] = height * (invert ? -0.5f : 0.5f);
509 vp.scale[2] = 0.5f;
510 vp.scale[3] = 1.0f;
511 vp.translate[0] = 0.5f * width;
512 vp.translate[1] = 0.5f * height;
513 vp.translate[2] = 0.5f;
514 vp.translate[3] = 0.0f;
515 cso_set_viewport(cso, &vp);
516 }
517
518 cso_set_vertex_elements(cso, 3, st->velems_util_draw);
519 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
520
521 /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
522 z = z * 2.0f - 1.0f;
523
524 /* draw textured quad */
525 setup_bitmap_vertex_data(st, sv->texture->target != PIPE_TEXTURE_RECT,
526 x, y, width, height, z, color, &vbuf, &offset);
527
528 if (vbuf) {
529 util_draw_vertex_buffer(pipe, st->cso_context, vbuf,
530 cso_get_aux_vertex_buffer_slot(st->cso_context),
531 offset,
532 PIPE_PRIM_TRIANGLE_FAN,
533 4, /* verts */
534 3); /* attribs/vert */
535 }
536
537 /* restore state */
538 cso_restore_rasterizer(cso);
539 cso_restore_samplers(cso, PIPE_SHADER_FRAGMENT);
540 cso_restore_sampler_views(cso, PIPE_SHADER_FRAGMENT);
541 cso_restore_viewport(cso);
542 cso_restore_fragment_shader(cso);
543 cso_restore_vertex_shader(cso);
544 cso_restore_geometry_shader(cso);
545 cso_restore_vertex_elements(cso);
546 cso_restore_aux_vertex_buffer_slot(cso);
547 cso_restore_stream_outputs(cso);
548
549 pipe_resource_reference(&vbuf, NULL);
550 }
551
552
553 static void
554 reset_cache(struct st_context *st)
555 {
556 struct bitmap_cache *cache = st->bitmap.cache;
557
558 /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
559 cache->empty = GL_TRUE;
560
561 cache->xmin = 1000000;
562 cache->xmax = -1000000;
563 cache->ymin = 1000000;
564 cache->ymax = -1000000;
565
566 assert(!cache->texture);
567
568 /* allocate a new texture */
569 cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
570 st->bitmap.tex_format, 0,
571 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
572 1, 1,
573 PIPE_BIND_SAMPLER_VIEW);
574 }
575
576
577 /** Print bitmap image to stdout (debug) */
578 static void
579 print_cache(const struct bitmap_cache *cache)
580 {
581 int i, j, k;
582
583 for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
584 k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
585 for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
586 if (cache->buffer[k])
587 printf("X");
588 else
589 printf(" ");
590 k++;
591 }
592 printf("\n");
593 }
594 }
595
596
597 /**
598 * Create gallium pipe_transfer object for the bitmap cache.
599 */
600 static void
601 create_cache_trans(struct st_context *st)
602 {
603 struct pipe_context *pipe = st->pipe;
604 struct bitmap_cache *cache = st->bitmap.cache;
605
606 if (cache->trans)
607 return;
608
609 /* Map the texture transfer.
610 * Subsequent glBitmap calls will write into the texture image.
611 */
612 cache->buffer = pipe_transfer_map(pipe, cache->texture, 0, 0,
613 PIPE_TRANSFER_WRITE, 0, 0,
614 BITMAP_CACHE_WIDTH,
615 BITMAP_CACHE_HEIGHT, &cache->trans);
616
617 /* init image to all 0xff */
618 memset(cache->buffer, 0xff, cache->trans->stride * BITMAP_CACHE_HEIGHT);
619 }
620
621
622 /**
623 * If there's anything in the bitmap cache, draw/flush it now.
624 */
625 void
626 st_flush_bitmap_cache(struct st_context *st)
627 {
628 if (!st->bitmap.cache->empty) {
629 struct bitmap_cache *cache = st->bitmap.cache;
630
631 struct pipe_context *pipe = st->pipe;
632 struct pipe_sampler_view *sv;
633
634 assert(cache->xmin <= cache->xmax);
635
636 /* printf("flush size %d x %d at %d, %d\n",
637 cache->xmax - cache->xmin,
638 cache->ymax - cache->ymin,
639 cache->xpos, cache->ypos);
640 */
641
642 /* The texture transfer has been mapped until now.
643 * So unmap and release the texture transfer before drawing.
644 */
645 if (cache->trans && cache->buffer) {
646 if (0)
647 print_cache(cache);
648 pipe_transfer_unmap(pipe, cache->trans);
649 cache->buffer = NULL;
650 cache->trans = NULL;
651 }
652
653 sv = st_create_texture_sampler_view(st->pipe, cache->texture);
654 if (sv) {
655 draw_bitmap_quad(st->ctx,
656 cache->xpos,
657 cache->ypos,
658 cache->zpos,
659 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
660 sv,
661 cache->color);
662
663 pipe_sampler_view_reference(&sv, NULL);
664 }
665
666 /* release/free the texture */
667 pipe_resource_reference(&cache->texture, NULL);
668
669 reset_cache(st);
670 }
671 }
672
673
674 /**
675 * Try to accumulate this glBitmap call in the bitmap cache.
676 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
677 */
678 static GLboolean
679 accum_bitmap(struct st_context *st,
680 GLint x, GLint y, GLsizei width, GLsizei height,
681 const struct gl_pixelstore_attrib *unpack,
682 const GLubyte *bitmap )
683 {
684 struct bitmap_cache *cache = st->bitmap.cache;
685 int px = -999, py = -999;
686 const GLfloat z = st->ctx->Current.RasterPos[2];
687
688 if (width > BITMAP_CACHE_WIDTH ||
689 height > BITMAP_CACHE_HEIGHT)
690 return GL_FALSE; /* too big to cache */
691
692 if (!cache->empty) {
693 px = x - cache->xpos; /* pos in buffer */
694 py = y - cache->ypos;
695 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
696 py < 0 || py + height > BITMAP_CACHE_HEIGHT ||
697 !TEST_EQ_4V(st->ctx->Current.RasterColor, cache->color) ||
698 ((fabs(z - cache->zpos) > Z_EPSILON))) {
699 /* This bitmap would extend beyond cache bounds, or the bitmap
700 * color is changing
701 * so flush and continue.
702 */
703 st_flush_bitmap_cache(st);
704 }
705 }
706
707 if (cache->empty) {
708 /* Initialize. Center bitmap vertically in the buffer. */
709 px = 0;
710 py = (BITMAP_CACHE_HEIGHT - height) / 2;
711 cache->xpos = x;
712 cache->ypos = y - py;
713 cache->zpos = z;
714 cache->empty = GL_FALSE;
715 COPY_4FV(cache->color, st->ctx->Current.RasterColor);
716 }
717
718 assert(px != -999);
719 assert(py != -999);
720
721 if (x < cache->xmin)
722 cache->xmin = x;
723 if (y < cache->ymin)
724 cache->ymin = y;
725 if (x + width > cache->xmax)
726 cache->xmax = x + width;
727 if (y + height > cache->ymax)
728 cache->ymax = y + height;
729
730 /* create the transfer if needed */
731 create_cache_trans(st);
732
733 unpack_bitmap(st, px, py, width, height, unpack, bitmap,
734 cache->buffer, BITMAP_CACHE_WIDTH);
735
736 return GL_TRUE; /* accumulated */
737 }
738
739
740
741 /**
742 * Called via ctx->Driver.Bitmap()
743 */
744 static void
745 st_Bitmap(struct gl_context *ctx, GLint x, GLint y,
746 GLsizei width, GLsizei height,
747 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
748 {
749 struct st_context *st = st_context(ctx);
750 struct pipe_resource *pt;
751
752 if (width == 0 || height == 0)
753 return;
754
755 st_validate_state(st);
756
757 if (!st->bitmap.vs) {
758 /* create pass-through vertex shader now */
759 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
760 TGSI_SEMANTIC_COLOR,
761 TGSI_SEMANTIC_GENERIC };
762 const uint semantic_indexes[] = { 0, 0, 0 };
763 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
764 semantic_names,
765 semantic_indexes);
766 }
767
768 if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
769 return;
770
771 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
772 if (pt) {
773 struct pipe_sampler_view *sv =
774 st_create_texture_sampler_view(st->pipe, pt);
775
776 assert(pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT);
777
778 if (sv) {
779 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
780 width, height, sv,
781 st->ctx->Current.RasterColor);
782
783 pipe_sampler_view_reference(&sv, NULL);
784 }
785
786 /* release/free the texture */
787 pipe_resource_reference(&pt, NULL);
788 }
789 }
790
791
792 /** Per-context init */
793 void
794 st_init_bitmap_functions(struct dd_function_table *functions)
795 {
796 functions->Bitmap = st_Bitmap;
797 }
798
799
800 /** Per-context init */
801 void
802 st_init_bitmap(struct st_context *st)
803 {
804 struct pipe_sampler_state *sampler = &st->bitmap.samplers[0];
805 struct pipe_context *pipe = st->pipe;
806 struct pipe_screen *screen = pipe->screen;
807
808 /* init sampler state once */
809 memset(sampler, 0, sizeof(*sampler));
810 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
811 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
812 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
813 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
814 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
815 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
816 st->bitmap.samplers[1] = *sampler;
817 st->bitmap.samplers[1].normalized_coords = 1;
818
819 /* init baseline rasterizer state once */
820 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
821 st->bitmap.rasterizer.gl_rasterization_rules = 1;
822 st->bitmap.rasterizer.depth_clip = 1;
823
824 /* find a usable texture format */
825 if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM,
826 PIPE_TEXTURE_2D, 0,
827 PIPE_BIND_SAMPLER_VIEW)) {
828 st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
829 }
830 else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM,
831 PIPE_TEXTURE_2D, 0,
832 PIPE_BIND_SAMPLER_VIEW)) {
833 st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
834 }
835 else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM,
836 PIPE_TEXTURE_2D, 0,
837 PIPE_BIND_SAMPLER_VIEW)) {
838 st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
839 }
840 else {
841 /* XXX support more formats */
842 assert(0);
843 }
844
845 /* alloc bitmap cache object */
846 st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
847
848 reset_cache(st);
849 }
850
851
852 /** Per-context tear-down */
853 void
854 st_destroy_bitmap(struct st_context *st)
855 {
856 struct pipe_context *pipe = st->pipe;
857 struct bitmap_cache *cache = st->bitmap.cache;
858
859 if (st->bitmap.vs) {
860 cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
861 st->bitmap.vs = NULL;
862 }
863
864 if (cache) {
865 if (cache->trans && cache->buffer) {
866 pipe_transfer_unmap(pipe, cache->trans);
867 }
868 pipe_resource_reference(&st->bitmap.cache->texture, NULL);
869 free(st->bitmap.cache);
870 st->bitmap.cache = NULL;
871 }
872 }