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