gallium: Use CSO cache for shaders.
[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/texformat.h"
38 #include "shader/program.h"
39 #include "shader/prog_parameter.h"
40 #include "shader/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_cb_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_texture.h"
50 #include "pipe/p_context.h"
51 #include "pipe/p_defines.h"
52 #include "pipe/p_inlines.h"
53 #include "pipe/p_winsys.h"
54 #include "util/p_tile.h"
55 #include "util/u_draw_quad.h"
56 #include "util/u_simple_shaders.h"
57 #include "shader/prog_instruction.h"
58 #include "cso_cache/cso_context.h"
59
60
61
62 /**
63 * glBitmaps are drawn as textured quads. The user's bitmap pattern
64 * is stored in a texture image. An alpha8 texture format is used.
65 * The fragment shader samples a bit (texel) from the texture, then
66 * discards the fragment if the bit is off.
67 *
68 * Note that we actually store the inverse image of the bitmap to
69 * simplify the fragment program. An "on" bit gets stored as texel=0x0
70 * and an "off" bit is stored as texel=0xff. Then we kill the
71 * fragment if the negated texel value is less than zero.
72 */
73
74
75 /**
76 * The bitmap cache attempts to accumulate multiple glBitmap calls in a
77 * buffer which is then rendered en mass upon a flush, state change, etc.
78 * A wide, short buffer is used to target the common case of a series
79 * of glBitmap calls being used to draw text.
80 */
81 static GLboolean UseBitmapCache = GL_TRUE;
82
83
84 #define BITMAP_CACHE_WIDTH 512
85 #define BITMAP_CACHE_HEIGHT 32
86
87 struct bitmap_cache
88 {
89 /** Window pos to render the cached image */
90 GLint xpos, ypos;
91 /** Bounds of region used in window coords */
92 GLint xmin, ymin, xmax, ymax;
93 struct pipe_texture *texture;
94 GLboolean empty;
95 /** An I8 texture image: */
96 GLubyte buffer[BITMAP_CACHE_HEIGHT][BITMAP_CACHE_WIDTH];
97 };
98
99
100
101
102 /**
103 * Make fragment program for glBitmap:
104 * Sample the texture and kill the fragment if the bit is 0.
105 * This program will be combined with the user's fragment program.
106 */
107 static struct st_fragment_program *
108 make_bitmap_fragment_program(GLcontext *ctx)
109 {
110 struct st_fragment_program *stfp;
111 struct gl_program *p;
112 GLuint ic = 0;
113
114 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
115 if (!p)
116 return NULL;
117
118 p->NumInstructions = 3;
119
120 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
121 if (!p->Instructions) {
122 ctx->Driver.DeleteProgram(ctx, p);
123 return NULL;
124 }
125 _mesa_init_instructions(p->Instructions, p->NumInstructions);
126
127 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
128 p->Instructions[ic].Opcode = OPCODE_TEX;
129 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
130 p->Instructions[ic].DstReg.Index = 0;
131 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
132 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
133 p->Instructions[ic].TexSrcUnit = 0;
134 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
135 ic++;
136
137 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
138 p->Instructions[ic].Opcode = OPCODE_KIL;
139 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
140 p->Instructions[ic].SrcReg[0].Index = 0;
141 p->Instructions[ic].SrcReg[0].NegateBase = NEGATE_XYZW;
142 ic++;
143
144 /* END; */
145 p->Instructions[ic++].Opcode = OPCODE_END;
146
147 assert(ic == p->NumInstructions);
148
149 p->InputsRead = FRAG_BIT_TEX0;
150 p->OutputsWritten = 0x0;
151 p->SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
152
153 stfp = (struct st_fragment_program *) p;
154 stfp->Base.UsesKill = GL_TRUE;
155 st_translate_fragment_program(ctx->st, stfp, NULL);
156
157 return stfp;
158 }
159
160
161 /**
162 * Combine basic bitmap fragment program with the user-defined program.
163 */
164 static struct st_fragment_program *
165 combined_bitmap_fragment_program(GLcontext *ctx)
166 {
167 struct st_context *st = ctx->st;
168 struct st_fragment_program *stfp;
169
170 if (!st->bitmap.program) {
171 /* create the basic bitmap fragment program */
172 st->bitmap.program = make_bitmap_fragment_program(ctx);
173 }
174
175 if (st->bitmap.user_prog_sn == st->fp->serialNo) {
176 /* re-use */
177 stfp = st->bitmap.combined_prog;
178 }
179 else {
180 /* Concatenate the bitmap program with the current user-defined program.
181 */
182 stfp = (struct st_fragment_program *)
183 _mesa_combine_programs(ctx,
184 &st->bitmap.program->Base.Base,
185 &st->fp->Base.Base);
186
187 #if 0
188 {
189 struct gl_program *p = &stfp->Base.Base;
190 printf("Combined bitmap program:\n");
191 _mesa_print_program(p);
192 printf("InputsRead: 0x%x\n", p->InputsRead);
193 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
194 _mesa_print_parameter_list(p->Parameters);
195 }
196 #endif
197
198 /* translate to TGSI tokens */
199 st_translate_fragment_program(st, stfp, NULL);
200
201 /* save new program, update serial numbers */
202 st->bitmap.user_prog_sn = st->fp->serialNo;
203 st->bitmap.combined_prog = stfp;
204 }
205
206 /* Ideally we'd have updated the pipe constants during the normal
207 * st/atom mechanism. But we can't since this is specific to glBitmap.
208 */
209 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
210
211 return stfp;
212 }
213
214
215 /**
216 * Create a texture which represents a bitmap image.
217 */
218 static struct pipe_texture *
219 make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
220 const struct gl_pixelstore_attrib *unpack,
221 const GLubyte *bitmap)
222 {
223 struct pipe_context *pipe = ctx->st->pipe;
224 struct pipe_screen *screen = pipe->screen;
225 struct pipe_surface *surface;
226 uint format = 0, cpp, comp;
227 ubyte *dest;
228 struct pipe_texture *pt;
229 int row, col;
230
231 /* find a texture format we know */
232 if (screen->is_format_supported( screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE )) {
233 format = PIPE_FORMAT_U_I8;
234 cpp = 1;
235 comp = 0;
236 }
237 else if (screen->is_format_supported( screen, PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_TEXTURE )) {
238 format = PIPE_FORMAT_A8R8G8B8_UNORM;
239 cpp = 4;
240 comp = 3; /* alpha channel */ /*XXX little-endian dependency */
241 }
242 else {
243 /* XXX support more formats */
244 assert( 0 );
245 }
246
247 /* PBO source... */
248 bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
249 if (!bitmap) {
250 return NULL;
251 }
252
253 /**
254 * Create texture to hold bitmap pattern.
255 */
256 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0, width, height,
257 1, 0);
258 if (!pt) {
259 _mesa_unmap_bitmap_pbo(ctx, unpack);
260 return NULL;
261 }
262
263 surface = screen->get_tex_surface(screen, pt, 0, 0, 0);
264
265 /* map texture surface */
266 dest = pipe_surface_map(surface);
267
268 /* Put image into texture surface.
269 * Note that the image is actually going to be upside down in
270 * the texture. We deal with that with texcoords.
271 */
272
273 for (row = 0; row < height; row++) {
274 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
275 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
276 ubyte *destRow = dest + row * surface->pitch * cpp;
277
278 if (unpack->LsbFirst) {
279 /* Lsb first */
280 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
281 for (col = 0; col < width; col++) {
282
283 /* set texel to 255 if bit is set */
284 destRow[comp] = (*src & mask) ? 0x0 : 0xff;
285 destRow += cpp;
286
287 if (mask == 128U) {
288 src++;
289 mask = 1U;
290 }
291 else {
292 mask = mask << 1;
293 }
294 }
295
296 /* get ready for next row */
297 if (mask != 1)
298 src++;
299 }
300 else {
301 /* Msb first */
302 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
303 for (col = 0; col < width; col++) {
304
305 /* set texel to 255 if bit is set */
306 destRow[comp] =(*src & mask) ? 0x0 : 0xff;
307 destRow += cpp;
308
309 if (mask == 1U) {
310 src++;
311 mask = 128U;
312 }
313 else {
314 mask = mask >> 1;
315 }
316 }
317
318 /* get ready for next row */
319 if (mask != 128)
320 src++;
321 }
322
323 } /* row */
324
325 _mesa_unmap_bitmap_pbo(ctx, unpack);
326
327 /* Release surface */
328 pipe_surface_unmap(surface);
329 pipe_surface_reference(&surface, NULL);
330 pipe->texture_update(pipe, pt, 0, 0x1);
331
332 pt->format = format;
333
334 return pt;
335 }
336
337
338 static void
339 setup_bitmap_vertex_data(struct st_context *st,
340 int x, int y, int width, int height,
341 float z, const float color[4])
342 {
343 struct pipe_context *pipe = st->pipe;
344 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
345 const GLfloat fb_width = fb->Width;
346 const GLfloat fb_height = fb->Height;
347 const GLfloat x0 = x;
348 const GLfloat x1 = x + width;
349 const GLfloat y0 = y;
350 const GLfloat y1 = y + height;
351 const GLfloat bias = st->bitmap_texcoord_bias;
352 const GLfloat xBias = bias / (x1-x0);
353 const GLfloat yBias = bias / (y1-y0);
354 const GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias;
355 const GLfloat tTop = yBias, tBot = 1.0 - tTop - yBias;
356 const GLfloat clip_x0 = x0 / fb_width * 2.0 - 1.0;
357 const GLfloat clip_y0 = y0 / fb_height * 2.0 - 1.0;
358 const GLfloat clip_x1 = x1 / fb_width * 2.0 - 1.0;
359 const GLfloat clip_y1 = y1 / fb_height * 2.0 - 1.0;
360 GLuint i;
361 void *buf;
362
363 if (!st->bitmap.vbuf) {
364 st->bitmap.vbuf = pipe->winsys->buffer_create(pipe->winsys, 32,
365 PIPE_BUFFER_USAGE_VERTEX,
366 sizeof(st->bitmap.vertices));
367 }
368
369 /* Positions are in clip coords since we need to do clipping in case
370 * the bitmap quad goes beyond the window bounds.
371 */
372 st->bitmap.vertices[0][0][0] = clip_x0;
373 st->bitmap.vertices[0][0][1] = clip_y0;
374 st->bitmap.vertices[0][2][0] = sLeft;
375 st->bitmap.vertices[0][2][1] = tTop;
376
377 st->bitmap.vertices[1][0][0] = clip_x1;
378 st->bitmap.vertices[1][0][1] = clip_y0;
379 st->bitmap.vertices[1][2][0] = sRight;
380 st->bitmap.vertices[1][2][1] = tTop;
381
382 st->bitmap.vertices[2][0][0] = clip_x1;
383 st->bitmap.vertices[2][0][1] = clip_y1;
384 st->bitmap.vertices[2][2][0] = sRight;
385 st->bitmap.vertices[2][2][1] = tBot;
386
387 st->bitmap.vertices[3][0][0] = clip_x0;
388 st->bitmap.vertices[3][0][1] = clip_y1;
389 st->bitmap.vertices[3][2][0] = sLeft;
390 st->bitmap.vertices[3][2][1] = tBot;
391
392 /* same for all verts: */
393 for (i = 0; i < 4; i++) {
394 st->bitmap.vertices[i][0][2] = z;
395 st->bitmap.vertices[i][0][3] = 1.0;
396 st->bitmap.vertices[i][1][0] = color[0];
397 st->bitmap.vertices[i][1][1] = color[1];
398 st->bitmap.vertices[i][1][2] = color[2];
399 st->bitmap.vertices[i][1][3] = color[3];
400 st->bitmap.vertices[i][2][2] = 0.0; /*R*/
401 st->bitmap.vertices[i][2][3] = 1.0; /*Q*/
402 }
403
404 /* put vertex data into vbuf */
405 buf = pipe->winsys->buffer_map(pipe->winsys, st->bitmap.vbuf,
406 PIPE_BUFFER_USAGE_CPU_WRITE);
407 memcpy(buf, st->bitmap.vertices, sizeof(st->bitmap.vertices));
408 pipe->winsys->buffer_unmap(pipe->winsys, st->bitmap.vbuf);
409 }
410
411
412
413 /**
414 * Render a glBitmap by drawing a textured quad
415 */
416 static void
417 draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
418 GLsizei width, GLsizei height,
419 struct pipe_texture *pt)
420 {
421 struct st_context *st = ctx->st;
422 struct pipe_context *pipe = ctx->st->pipe;
423 struct cso_context *cso = ctx->st->cso_context;
424 struct st_fragment_program *stfp;
425 GLuint maxSize;
426
427 stfp = combined_bitmap_fragment_program(ctx);
428
429 /* limit checks */
430 /* XXX if the bitmap is larger than the max texture size, break
431 * it up into chunks.
432 */
433 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
434 assert(width <= maxSize);
435 assert(height <= maxSize);
436
437 cso_save_rasterizer(cso);
438 cso_save_samplers(cso);
439 cso_save_sampler_textures(cso);
440 cso_save_viewport(cso);
441 cso_save_fragment_shader(cso);
442 cso_save_vertex_shader(cso);
443
444 /* rasterizer state: just scissor */
445 st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
446 cso_set_rasterizer(cso, &st->bitmap.rasterizer);
447
448 /* fragment shader state: TEX lookup program */
449 cso_set_fragment_shader(cso, stfp->driver_shader);
450
451 /* vertex shader state: position + texcoord pass-through */
452 cso_set_vertex_shader(cso, st->bitmap.vs);
453
454 /* sampler / texture state */
455 cso_single_sampler(cso, 0, &st->bitmap.sampler);
456 cso_single_sampler_done(cso);
457 pipe->set_sampler_textures(pipe, 1, &pt);
458
459 /* viewport state: viewport matching window dims */
460 {
461 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
462 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
463 const float width = fb->Width;
464 const float height = fb->Height;
465 struct pipe_viewport_state vp;
466 vp.scale[0] = 0.5 * width;
467 vp.scale[1] = height * (invert ? -0.5 : 0.5);
468 vp.scale[2] = 1.0;
469 vp.scale[3] = 1.0;
470 vp.translate[0] = 0.5 * width;
471 vp.translate[1] = 0.5 * height;
472 vp.translate[2] = 0.0;
473 vp.translate[3] = 0.0;
474 cso_set_viewport(cso, &vp);
475 }
476
477 /* draw textured quad */
478 setup_bitmap_vertex_data(st, x, y, width, height,
479 ctx->Current.RasterPos[2],
480 ctx->Current.RasterColor);
481
482 util_draw_vertex_buffer(pipe, st->bitmap.vbuf,
483 PIPE_PRIM_TRIANGLE_FAN,
484 4, /* verts */
485 3); /* attribs/vert */
486
487
488 /* restore state */
489 cso_restore_rasterizer(cso);
490 cso_restore_samplers(cso);
491 cso_restore_sampler_textures(cso);
492 cso_restore_viewport(cso);
493 cso_save_fragment_shader(cso);
494 cso_save_vertex_shader(cso);
495 }
496
497
498 static void
499 reset_cache(struct st_context *st)
500 {
501 memset(st->bitmap.cache->buffer, 0xff, sizeof(st->bitmap.cache->buffer));
502 st->bitmap.cache->empty = GL_TRUE;
503
504 st->bitmap.cache->xmin = 1000000;
505 st->bitmap.cache->xmax = -1000000;
506 st->bitmap.cache->ymin = 1000000;
507 st->bitmap.cache->ymax = -1000000;
508 }
509
510
511 static void
512 init_bitmap_cache(struct st_context *st)
513 {
514 struct pipe_context *pipe = st->pipe;
515 struct pipe_screen *screen = pipe->screen;
516 enum pipe_format format;
517
518 st->bitmap.cache = CALLOC_STRUCT(bitmap_cache);
519 if (!st->bitmap.cache)
520 return;
521
522 /* find a usable texture format */
523 if (screen->is_format_supported(screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE)) {
524 format = PIPE_FORMAT_U_I8;
525 }
526 else {
527 /* XXX support more formats */
528 assert(0);
529 }
530
531 st->bitmap.cache->texture
532 = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
533 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT, 1, 0);
534 if (!st->bitmap.cache->texture) {
535 FREE(st->bitmap.cache);
536 st->bitmap.cache = NULL;
537 return;
538 }
539
540 reset_cache(st);
541 }
542
543
544 /**
545 * If there's anything in the bitmap cache, draw/flush it now.
546 */
547 void
548 st_flush_bitmap_cache(struct st_context *st)
549 {
550 if (!st->bitmap.cache->empty) {
551 if (st->ctx->DrawBuffer) {
552 struct bitmap_cache *cache = st->bitmap.cache;
553 struct pipe_context *pipe = st->pipe;
554 struct pipe_screen *screen = pipe->screen;
555 struct pipe_surface *surf;
556 void *dest;
557
558 assert(cache->xmin <= cache->xmax);
559 /*
560 printf("flush size %d x %d at %d, %d\n",
561 cache->xmax - cache->xmin,
562 cache->ymax - cache->ymin,
563 cache->xpos, cache->ypos);
564 */
565
566 /* update the texture map image */
567 surf = screen->get_tex_surface(screen, cache->texture, 0, 0, 0);
568 dest = pipe_surface_map(surf);
569 memcpy(dest, cache->buffer, sizeof(cache->buffer));
570 pipe_surface_unmap(surf);
571 pipe_surface_reference(&surf, NULL);
572
573 pipe->texture_update(pipe, cache->texture, 0, 0x1);
574
575 draw_bitmap_quad(st->ctx,
576 cache->xpos,
577 cache->ypos,
578 st->ctx->Current.RasterPos[2],
579 BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
580 cache->texture);
581
582 }
583 reset_cache(st);
584 }
585 }
586
587
588 /**
589 * Try to accumulate this glBitmap call in the bitmap cache.
590 * \return GL_TRUE for success, GL_FALSE if bitmap is too large, etc.
591 */
592 static GLboolean
593 accum_bitmap(struct st_context *st,
594 GLint x, GLint y, GLsizei width, GLsizei height,
595 const struct gl_pixelstore_attrib *unpack,
596 const GLubyte *bitmap )
597 {
598 struct bitmap_cache *cache = st->bitmap.cache;
599 int row, col;
600 int px = -999, py;
601
602 if (width > BITMAP_CACHE_WIDTH ||
603 height > BITMAP_CACHE_HEIGHT)
604 return GL_FALSE; /* too big to cache */
605
606 if (!cache->empty) {
607 px = x - cache->xpos; /* pos in buffer */
608 py = y - cache->ypos;
609 if (px < 0 || px + width > BITMAP_CACHE_WIDTH ||
610 py < 0 || py + height > BITMAP_CACHE_HEIGHT) {
611 /* This bitmap would extend beyond cache bounds,
612 * so flush and continue.
613 */
614 st_flush_bitmap_cache(st);
615 }
616 }
617
618 if (cache->empty) {
619 /* Initialize. Center bitmap vertically in the buffer. */
620 px = 0;
621 py = (BITMAP_CACHE_HEIGHT - height) / 2;
622 cache->xpos = x;
623 cache->ypos = y - py;
624 cache->empty = GL_FALSE;
625 }
626
627 assert(px != -999);
628
629 if (x < cache->xmin)
630 cache->xmin = x;
631 if (y < cache->ymin)
632 cache->ymin = y;
633 if (x + width > cache->xmax)
634 cache->xmax = x + width;
635 if (y + height > cache->ymax)
636 cache->ymax = y + height;
637
638 /* XXX try to combine this code with code in make_bitmap_texture() */
639 #define SET_PIXEL(COL, ROW) \
640 cache->buffer[py + (ROW)][px + (COL)] = 0x0;
641
642 for (row = 0; row < height; row++) {
643 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
644 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
645
646 if (unpack->LsbFirst) {
647 /* Lsb first */
648 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
649 for (col = 0; col < width; col++) {
650
651 if (*src & mask) {
652 SET_PIXEL(col, row);
653 }
654
655 if (mask == 128U) {
656 src++;
657 mask = 1U;
658 }
659 else {
660 mask = mask << 1;
661 }
662 }
663
664 /* get ready for next row */
665 if (mask != 1)
666 src++;
667 }
668 else {
669 /* Msb first */
670 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
671 for (col = 0; col < width; col++) {
672
673 if (*src & mask) {
674 SET_PIXEL(col, row);
675 }
676
677 if (mask == 1U) {
678 src++;
679 mask = 128U;
680 }
681 else {
682 mask = mask >> 1;
683 }
684 }
685
686 /* get ready for next row */
687 if (mask != 128)
688 src++;
689 }
690
691 } /* row */
692
693 return GL_TRUE; /* accumulated */
694 }
695
696
697
698 /**
699 * Called via ctx->Driver.Bitmap()
700 */
701 static void
702 st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
703 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
704 {
705 struct st_context *st = ctx->st;
706 struct pipe_texture *pt;
707
708 st_validate_state(st);
709
710 if (!st->bitmap.vs) {
711 /* create pass-through vertex shader now */
712 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
713 TGSI_SEMANTIC_COLOR,
714 TGSI_SEMANTIC_GENERIC };
715 const uint semantic_indexes[] = { 0, 0, 0 };
716 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
717 semantic_names,
718 semantic_indexes,
719 &st->bitmap.vert_shader);
720 }
721
722 if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
723 return;
724
725 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
726 if (pt) {
727 assert(pt->target == PIPE_TEXTURE_2D);
728 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
729 width, height, pt);
730 pipe_texture_reference(&pt, NULL);
731 }
732 }
733
734
735 /** Per-context init */
736 void
737 st_init_bitmap_functions(struct dd_function_table *functions)
738 {
739 functions->Bitmap = st_Bitmap;
740 }
741
742
743 /** Per-context init */
744 void
745 st_init_bitmap(struct st_context *st)
746 {
747 struct pipe_sampler_state *sampler = &st->bitmap.sampler;
748
749 /* init sampler state once */
750 memset(sampler, 0, sizeof(*sampler));
751 sampler->wrap_s = PIPE_TEX_WRAP_CLAMP;
752 sampler->wrap_t = PIPE_TEX_WRAP_CLAMP;
753 sampler->wrap_r = PIPE_TEX_WRAP_CLAMP;
754 sampler->min_img_filter = PIPE_TEX_FILTER_NEAREST;
755 sampler->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
756 sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
757 sampler->normalized_coords = 1;
758
759 /* init baseline rasterizer state once */
760 memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
761 st->bitmap.rasterizer.gl_rasterization_rules = 1;
762 st->bitmap.rasterizer.bypass_vs = 1;
763
764 init_bitmap_cache(st);
765 }
766
767
768 /** Per-context tear-down */
769 void
770 st_destroy_bitmap(struct st_context *st)
771 {
772 struct pipe_context *pipe = st->pipe;
773
774 if (st->bitmap.combined_prog) {
775 st_delete_program(st->ctx, &st->bitmap.combined_prog->Base.Base);
776 }
777
778 if (st->bitmap.program) {
779 st_delete_program(st->ctx, &st->bitmap.program->Base.Base);
780 }
781
782 if (st->bitmap.vs) {
783 pipe->delete_vs_state(pipe, st->bitmap.vs);
784 st->bitmap.vs = NULL;
785 }
786
787 if (st->bitmap.vbuf) {
788 pipe->winsys->buffer_destroy(pipe->winsys, st->bitmap.vbuf);
789 st->bitmap.vbuf = NULL;
790 }
791
792 if (st->bitmap.cache) {
793 pipe_texture_release(&st->bitmap.cache->texture);
794 FREE(st->bitmap.cache);
795 st->bitmap.cache = NULL;
796 }
797 }