gallium: add missing cso_save_samplers() call
[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_mesa_to_tgsi.h"
48 #include "st_texture.h"
49 #include "pipe/p_context.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_inlines.h"
52 #include "pipe/p_winsys.h"
53 #include "util/p_tile.h"
54 #include "util/u_draw_quad.h"
55 #include "util/u_simple_shaders.h"
56 #include "shader/prog_instruction.h"
57 #include "cso_cache/cso_context.h"
58
59
60
61 /**
62 * Make fragment program for glBitmap:
63 * Sample the texture and kill the fragment if the bit is 0.
64 * This program will be combined with the user's fragment program.
65 */
66 static struct st_fragment_program *
67 make_bitmap_fragment_program(GLcontext *ctx)
68 {
69 struct st_fragment_program *stfp;
70 struct gl_program *p;
71 GLuint ic = 0;
72
73 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
74 if (!p)
75 return NULL;
76
77 p->NumInstructions = 5;
78
79 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
80 if (!p->Instructions) {
81 ctx->Driver.DeleteProgram(ctx, p);
82 return NULL;
83 }
84 _mesa_init_instructions(p->Instructions, p->NumInstructions);
85
86 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
87 p->Instructions[ic].Opcode = OPCODE_TEX;
88 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
89 p->Instructions[ic].DstReg.Index = 0;
90 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
91 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
92 p->Instructions[ic].TexSrcUnit = 0;
93 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
94 ic++;
95
96 /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
97 p->Instructions[ic].Opcode = OPCODE_SWZ;
98 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
99 p->Instructions[ic].DstReg.Index = 0;
100 p->Instructions[ic].DstReg.WriteMask = WRITEMASK_X;
101 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
102 p->Instructions[ic].SrcReg[0].Index = 0;
103 p->Instructions[ic].SrcReg[0].Swizzle
104 = MAKE_SWIZZLE4(SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE );
105 ic++;
106
107 /* SUB tmp0, tmp0.wwww, tmp0.xxxx; # tmp0.w -= 1 */
108 p->Instructions[ic].Opcode = OPCODE_SUB;
109 p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
110 p->Instructions[ic].DstReg.Index = 0;
111 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
112 p->Instructions[ic].SrcReg[0].Index = 0;
113 p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_WWWW;
114 p->Instructions[ic].SrcReg[1].File = PROGRAM_TEMPORARY;
115 p->Instructions[ic].SrcReg[1].Index = 0;
116 p->Instructions[ic].SrcReg[1].Swizzle = SWIZZLE_XXXX; /* 1.0 */
117 ic++;
118
119 /* KIL if tmp0 < 0 */
120 p->Instructions[ic].Opcode = OPCODE_KIL;
121 p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
122 p->Instructions[ic].SrcReg[0].Index = 0;
123 ic++;
124
125 /* END; */
126 p->Instructions[ic++].Opcode = OPCODE_END;
127
128 assert(ic == p->NumInstructions);
129
130 p->InputsRead = FRAG_BIT_TEX0;
131 p->OutputsWritten = 0x0;
132
133 stfp = (struct st_fragment_program *) p;
134 stfp->Base.UsesKill = GL_TRUE;
135 st_translate_fragment_program(ctx->st, stfp, NULL);
136
137 return stfp;
138 }
139
140
141 /**
142 * Combine basic bitmap fragment program with the user-defined program.
143 */
144 static struct st_fragment_program *
145 combined_bitmap_fragment_program(GLcontext *ctx)
146 {
147 struct st_context *st = ctx->st;
148 struct st_fragment_program *stfp;
149
150 if (!st->bitmap.program) {
151 /* create the basic bitmap fragment program */
152 st->bitmap.program = make_bitmap_fragment_program(ctx);
153 }
154
155 if (st->bitmap.user_prog_sn == st->fp->serialNo) {
156 /* re-use */
157 stfp = st->bitmap.combined_prog;
158 }
159 else {
160 /* Concatenate the bitmap program with the current user-defined program.
161 */
162 stfp = (struct st_fragment_program *)
163 _mesa_combine_programs(ctx,
164 &st->bitmap.program->Base.Base,
165 &st->fp->Base.Base);
166
167 #if 0
168 {
169 struct gl_program *p = &stfp->Base.Base;
170 printf("Combined bitmap program:\n");
171 _mesa_print_program(p);
172 printf("InputsRead: 0x%x\n", p->InputsRead);
173 printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
174 _mesa_print_parameter_list(p->Parameters);
175 }
176 #endif
177
178 /* translate to TGSI tokens */
179 st_translate_fragment_program(st, stfp, NULL);
180
181 /* save new program, update serial numbers */
182 st->bitmap.user_prog_sn = st->fp->serialNo;
183 st->bitmap.combined_prog = stfp;
184 }
185
186 /* Ideally we'd have updated the pipe constants during the normal
187 * st/atom mechanism. But we can't since this is specific to glBitmap.
188 */
189 st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
190
191 return stfp;
192 }
193
194
195 /**
196 * Create a texture which represents a bitmap image.
197 */
198 static struct pipe_texture *
199 make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
200 const struct gl_pixelstore_attrib *unpack,
201 const GLubyte *bitmap)
202 {
203 struct pipe_context *pipe = ctx->st->pipe;
204 struct pipe_screen *screen = pipe->screen;
205 struct pipe_surface *surface;
206 uint format = 0, cpp, comp;
207 ubyte *dest;
208 struct pipe_texture *pt;
209 int row, col;
210
211 /* find a texture format we know */
212 if (screen->is_format_supported( screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE )) {
213 format = PIPE_FORMAT_U_I8;
214 cpp = 1;
215 comp = 0;
216 }
217 else if (screen->is_format_supported( screen, PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_TEXTURE )) {
218 format = PIPE_FORMAT_A8R8G8B8_UNORM;
219 cpp = 4;
220 comp = 3; /* alpha channel */ /*XXX little-endian dependency */
221 }
222 else {
223 /* XXX support more formats */
224 assert( 0 );
225 }
226
227 /* PBO source... */
228 bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
229 if (!bitmap) {
230 return NULL;
231 }
232
233 /**
234 * Create texture to hold bitmap pattern.
235 */
236 pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0, width, height,
237 1, 0);
238 if (!pt) {
239 _mesa_unmap_bitmap_pbo(ctx, unpack);
240 return NULL;
241 }
242
243 surface = screen->get_tex_surface(screen, pt, 0, 0, 0);
244
245 /* map texture surface */
246 dest = pipe_surface_map(surface);
247
248 /* Put image into texture surface.
249 * Note that the image is actually going to be upside down in
250 * the texture. We deal with that with texcoords.
251 */
252
253 for (row = 0; row < height; row++) {
254 const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
255 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
256 ubyte *destRow = dest + row * surface->pitch * cpp;
257
258 if (unpack->LsbFirst) {
259 /* Lsb first */
260 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
261 for (col = 0; col < width; col++) {
262
263 /* set texel to 255 if bit is set */
264 destRow[comp] = (*src & mask) ? 255 : 0;
265 destRow += cpp;
266
267 if (mask == 128U) {
268 src++;
269 mask = 1U;
270 }
271 else {
272 mask = mask << 1;
273 }
274 }
275
276 /* get ready for next row */
277 if (mask != 1)
278 src++;
279 }
280 else {
281 /* Msb first */
282 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
283 for (col = 0; col < width; col++) {
284
285 /* set texel to 255 if bit is set */
286 destRow[comp] =(*src & mask) ? 255 : 0;
287 destRow += cpp;
288
289 if (mask == 1U) {
290 src++;
291 mask = 128U;
292 }
293 else {
294 mask = mask >> 1;
295 }
296 }
297
298 /* get ready for next row */
299 if (mask != 128)
300 src++;
301 }
302
303 } /* row */
304
305 _mesa_unmap_bitmap_pbo(ctx, unpack);
306
307 /* Release surface */
308 pipe_surface_unmap(surface);
309 pipe_surface_reference(&surface, NULL);
310 pipe->texture_update(pipe, pt, 0, 0x1);
311
312 pt->format = format;
313
314 return pt;
315 }
316
317
318 static void
319 setup_bitmap_vertex_data(struct st_context *st,
320 int x, int y, int width, int height,
321 float z, const float color[4])
322 {
323 struct pipe_context *pipe = st->pipe;
324 const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
325 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
326 const GLfloat x0 = x;
327 const GLfloat x1 = x + width;
328 const GLfloat y0 = invert ? (fb->Height - y - height) : y;
329 const GLfloat y1 = invert ? (y0 + height) : y + height;
330 const GLfloat bias = st->bitmap_texcoord_bias;
331 const GLfloat xBias = bias / (x1-x0);
332 const GLfloat yBias = bias / (y1-y0);
333 const GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias;
334 const GLfloat tTop = 1.0 - yBias, tBot = 1.0 - tTop - yBias;
335 GLuint i;
336 void *buf;
337
338 if (!st->bitmap.vbuf) {
339 st->bitmap.vbuf = pipe->winsys->buffer_create(pipe->winsys, 32,
340 PIPE_BUFFER_USAGE_VERTEX,
341 sizeof(st->bitmap.vertices));
342 }
343
344 /* positions, texcoords */
345 st->bitmap.vertices[0][0][0] = x0;
346 st->bitmap.vertices[0][0][1] = y0;
347 st->bitmap.vertices[0][2][0] = sLeft;
348 st->bitmap.vertices[0][2][1] = tTop;
349
350 st->bitmap.vertices[1][0][0] = x1;
351 st->bitmap.vertices[1][0][1] = y0;
352 st->bitmap.vertices[1][2][0] = sRight;
353 st->bitmap.vertices[1][2][1] = tTop;
354
355 st->bitmap.vertices[2][0][0] = x1;
356 st->bitmap.vertices[2][0][1] = y1;
357 st->bitmap.vertices[2][2][0] = sRight;
358 st->bitmap.vertices[2][2][1] = tBot;
359
360 st->bitmap.vertices[3][0][0] = x0;
361 st->bitmap.vertices[3][0][1] = y1;
362 st->bitmap.vertices[3][2][0] = sLeft;
363 st->bitmap.vertices[3][2][1] = tBot;
364
365 /* same for all verts: */
366 for (i = 0; i < 4; i++) {
367 st->bitmap.vertices[i][0][2] = z;
368 st->bitmap.vertices[i][0][3] = 1.0;
369 st->bitmap.vertices[i][1][0] = color[0];
370 st->bitmap.vertices[i][1][1] = color[1];
371 st->bitmap.vertices[i][1][2] = color[2];
372 st->bitmap.vertices[i][1][3] = color[3];
373 st->bitmap.vertices[i][2][2] = 0.0; /*R*/
374 st->bitmap.vertices[i][2][3] = 1.0; /*Q*/
375 }
376
377 /* put vertex data into vbuf */
378 buf = pipe->winsys->buffer_map(pipe->winsys, st->bitmap.vbuf,
379 PIPE_BUFFER_USAGE_CPU_WRITE);
380 memcpy(buf, st->bitmap.vertices, sizeof(st->bitmap.vertices));
381 pipe->winsys->buffer_unmap(pipe->winsys, st->bitmap.vbuf);
382 }
383
384
385
386 /**
387 * Render a glBitmap by drawing a textured quad
388 */
389 static void
390 draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
391 GLsizei width, GLsizei height,
392 struct pipe_texture *pt,
393 struct st_fragment_program *stfp)
394 {
395 struct st_context *st = ctx->st;
396 struct pipe_context *pipe = ctx->st->pipe;
397 struct cso_context *cso = ctx->st->cso_context;
398 GLuint maxSize;
399
400 /* limit checks */
401 /* XXX if DrawPixels image is larger than max texture size, break
402 * it up into chunks.
403 */
404 maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
405 assert(width <= maxSize);
406 assert(height <= maxSize);
407
408 cso_save_rasterizer(cso);
409 cso_save_samplers(cso);
410 //cso_save_viewport(cso);
411
412 /* rasterizer state: just scissor */
413 {
414 struct pipe_rasterizer_state rasterizer;
415 memset(&rasterizer, 0, sizeof(rasterizer));
416 if (ctx->Scissor.Enabled)
417 rasterizer.scissor = 1;
418 rasterizer.bypass_clipping = 1;
419
420 cso_set_rasterizer(cso, &rasterizer);
421 }
422
423 /* fragment shader state: TEX lookup program */
424 pipe->bind_fs_state(pipe, stfp->driver_shader);
425
426 /* vertex shader state: position + texcoord pass-through */
427 pipe->bind_vs_state(pipe, st->bitmap.vs);
428
429 /* sampler / texture state */
430 {
431 struct pipe_sampler_state sampler;
432 memset(&sampler, 0, sizeof(sampler));
433 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
434 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
435 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP;
436 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
437 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
438 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
439 sampler.normalized_coords = 1;
440
441 cso_single_sampler(cso, 0, &sampler);
442 cso_single_sampler_done(cso);
443
444 pipe->set_sampler_textures(pipe, 1, &pt);
445 }
446
447 /* draw textured quad */
448 setup_bitmap_vertex_data(st, x, y, width, height,
449 ctx->Current.RasterPos[2],
450 ctx->Current.RasterColor);
451
452 util_draw_vertex_buffer(pipe, st->bitmap.vbuf,
453 PIPE_PRIM_TRIANGLE_FAN,
454 4, /* verts */
455 3); /* attribs/vert */
456
457
458 /* restore state */
459 cso_restore_rasterizer(cso);
460 cso_restore_samplers(cso);
461 //cso_restore_viewport(cso);
462 /* shaders don't go through cso yet */
463 pipe->bind_fs_state(pipe, st->fp->driver_shader);
464 pipe->bind_vs_state(pipe, st->vp->driver_shader);
465
466 pipe->set_sampler_textures(pipe, ctx->st->state.num_textures,
467 ctx->st->state.sampler_texture);
468 }
469
470
471
472 static void
473 st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
474 const struct gl_pixelstore_attrib *unpack, const GLubyte *bitmap )
475 {
476 struct st_fragment_program *stfp;
477 struct st_context *st = ctx->st;
478 struct pipe_texture *pt;
479
480 stfp = combined_bitmap_fragment_program(ctx);
481
482 if (!st->bitmap.vs) {
483 /* create pass-through vertex shader now */
484 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
485 TGSI_SEMANTIC_COLOR,
486 TGSI_SEMANTIC_GENERIC };
487 const uint semantic_indexes[] = { 0, 0, 0 };
488 st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
489 semantic_names,
490 semantic_indexes);
491 }
492
493 st_validate_state(st);
494
495 pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
496 if (pt) {
497 draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
498 width, height,
499 pt, stfp);
500 pipe_texture_reference(&pt, NULL);
501 }
502 }
503
504
505
506 void st_init_bitmap_functions(struct dd_function_table *functions)
507 {
508 functions->Bitmap = st_Bitmap;
509 }
510
511
512 void
513 st_destroy_bitmap(struct st_context *st)
514 {
515 struct pipe_context *pipe = st->pipe;
516
517 /* XXX free frag shader state */
518
519 if (st->bitmap.vs) {
520 pipe->delete_vs_state(pipe, st->bitmap.vs);
521 st->bitmap.vs = NULL;
522 }
523 if (st->bitmap.vbuf) {
524 pipe->winsys->buffer_destroy(pipe->winsys, st->bitmap.vbuf);
525 st->bitmap.vbuf = NULL;
526 }
527 }
528