gallium: clean-up, simplification of mipmapped textures
[mesa.git] / src / mesa / state_tracker / st_gen_mipmap.c
1 /**************************************************************************
2 *
3 * Copyright 2008 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 #include "main/imports.h"
30 #include "main/mipmap.h"
31 #include "main/teximage.h"
32 #include "main/texformat.h"
33
34 #include "shader/prog_instruction.h"
35
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "pipe/p_inlines.h"
39 #include "pipe/p_winsys.h"
40 #include "pipe/cso_cache/cso_cache.h"
41
42 #include "st_context.h"
43 #include "st_draw.h"
44 #include "st_gen_mipmap.h"
45 #include "st_program.h"
46 #include "st_cb_drawpixels.h"
47 #include "st_cb_texture.h"
48
49
50
51 static void *blend_cso = NULL;
52 static void *depthstencil_cso = NULL;
53 static void *rasterizer_cso = NULL;
54
55 static struct st_fragment_program *stfp = NULL;
56 static struct st_vertex_program *stvp = NULL;
57
58
59
60 static struct st_fragment_program *
61 make_tex_fragment_program(GLcontext *ctx)
62 {
63 struct st_fragment_program *stfp;
64 struct gl_program *p;
65 GLuint ic = 0;
66
67 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
68 if (!p)
69 return NULL;
70
71 p->NumInstructions = 2;
72
73 p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
74 if (!p->Instructions) {
75 ctx->Driver.DeleteProgram(ctx, p);
76 return NULL;
77 }
78 _mesa_init_instructions(p->Instructions, p->NumInstructions);
79
80 /* TEX result.color, fragment.texcoord[0], texture[0], 2D; */
81 p->Instructions[ic].Opcode = OPCODE_TEX;
82 p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
83 p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLR;
84 p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
85 p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
86 p->Instructions[ic].TexSrcUnit = 0;
87 p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
88 ic++;
89
90 /* END; */
91 p->Instructions[ic++].Opcode = OPCODE_END;
92
93 assert(ic == p->NumInstructions);
94
95 p->InputsRead = FRAG_BIT_TEX0;
96 p->OutputsWritten = (1 << FRAG_RESULT_COLR);
97
98 stfp = (struct st_fragment_program *) p;
99
100 st_translate_fragment_program(ctx->st, stfp, NULL,
101 stfp->tokens, ST_MAX_SHADER_TOKENS);
102
103 return stfp;
104 }
105
106
107
108
109 /**
110 * one-time init for generate mipmap
111 * XXX Note: there may be other times we need no-op/simple state like this.
112 * In that case, some code refactoring would be good.
113 */
114 void
115 st_init_generate_mipmap(struct st_context *st)
116 {
117 struct pipe_context *pipe = st->pipe;
118 struct pipe_blend_state blend;
119 struct pipe_rasterizer_state rasterizer;
120 struct pipe_depth_stencil_alpha_state depthstencil;
121
122 assert(!blend_cso);
123
124 memset(&blend, 0, sizeof(blend));
125 blend.colormask = PIPE_MASK_RGBA;
126 blend_cso = pipe->create_blend_state(pipe, &blend);
127
128 memset(&depthstencil, 0, sizeof(depthstencil));
129 depthstencil_cso = pipe->create_depth_stencil_alpha_state(pipe, &depthstencil);
130
131 memset(&rasterizer, 0, sizeof(rasterizer));
132 rasterizer_cso = pipe->create_rasterizer_state(pipe, &rasterizer);
133
134 stfp = make_tex_fragment_program(st->ctx);
135 stvp = st_make_passthrough_vertex_shader(st, GL_FALSE);
136 }
137
138
139 void
140 st_destroy_generate_mipmpap(struct st_context *st)
141 {
142 struct pipe_context *pipe = st->pipe;
143
144 pipe->delete_blend_state(pipe, blend_cso);
145 pipe->delete_depth_stencil_alpha_state(pipe, depthstencil_cso);
146 pipe->delete_rasterizer_state(pipe, rasterizer_cso);
147
148 /* XXX free stfp, stvp */
149
150 blend_cso = NULL;
151 depthstencil_cso = NULL;
152 rasterizer_cso = NULL;
153 }
154
155
156 static void
157 simple_viewport(struct pipe_context *pipe, uint width, uint height)
158 {
159 struct pipe_viewport_state vp;
160
161 vp.scale[0] = 0.5 * width;
162 vp.scale[1] = -0.5 * height;
163 vp.scale[2] = 1.0;
164 vp.scale[3] = 1.0;
165 vp.translate[0] = 0.5 * width;
166 vp.translate[1] = 0.5 * height;
167 vp.translate[2] = 0.0;
168 vp.translate[3] = 0.0;
169
170 pipe->set_viewport_state(pipe, &vp);
171 }
172
173
174
175 /*
176 * Draw simple [-1,1]x[-1,1] quad
177 */
178 static void
179 draw_quad(GLcontext *ctx)
180 {
181 GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
182 GLuint i;
183 GLfloat sLeft = 0.0, sRight = 1.0;
184 GLfloat tTop = 1.0, tBot = 0.0;
185 GLfloat x0 = -1.0, x1 = 1.0;
186 GLfloat y0 = -1.0, y1 = 1.0;
187
188 /* upper-left */
189 verts[0][0][0] = x0; /* attr[0].x */
190 verts[0][0][1] = y0; /* attr[0].y */
191 verts[0][1][0] = sLeft; /* attr[1].s */
192 verts[0][1][1] = tTop; /* attr[1].t */
193
194 /* upper-right */
195 verts[1][0][0] = x1;
196 verts[1][0][1] = y0;
197 verts[1][1][0] = sRight;
198 verts[1][1][1] = tTop;
199
200 /* lower-right */
201 verts[2][0][0] = x1;
202 verts[2][0][1] = y1;
203 verts[2][1][0] = sRight;
204 verts[2][1][1] = tBot;
205
206 /* lower-left */
207 verts[3][0][0] = x0;
208 verts[3][0][1] = y1;
209 verts[3][1][0] = sLeft;
210 verts[3][1][1] = tBot;
211
212 /* same for all verts: */
213 for (i = 0; i < 4; i++) {
214 verts[i][0][2] = 0.0; /*Z*/
215 verts[i][0][3] = 1.0; /*W*/
216 verts[i][1][2] = 0.0; /*R*/
217 verts[i][1][3] = 1.0; /*Q*/
218 }
219
220 st_draw_vertices(ctx, PIPE_PRIM_QUADS, 4, (float *) verts, 2, GL_TRUE);
221 }
222
223
224
225 /**
226 * Generate mipmap levels using hardware rendering.
227 * \return TRUE if successful, FALSE if not possible
228 */
229 static boolean
230 st_render_mipmap(struct st_context *st,
231 GLenum target,
232 struct pipe_texture *pt,
233 uint baseLevel, uint lastLevel)
234 {
235 struct pipe_context *pipe = st->pipe;
236 struct pipe_framebuffer_state fb;
237 struct pipe_sampler_state sampler;
238 void *sampler_cso;
239 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
240 /*const uint first_level_save = pt->first_level;*/
241 uint dstLevel;
242
243 assert(target != GL_TEXTURE_3D); /* not done yet */
244
245 /* check if we can render in the texture's format */
246 if (!pipe->is_format_supported(pipe, pt->format, PIPE_SURFACE)) {
247 return FALSE;
248 }
249
250 /* init framebuffer state */
251 memset(&fb, 0, sizeof(fb));
252 fb.num_cbufs = 1;
253
254 /* sampler state */
255 memset(&sampler, 0, sizeof(sampler));
256 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
257 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
258 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
259 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
260 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
261 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
262 sampler.normalized_coords = 1;
263
264
265 /* bind CSOs */
266 pipe->bind_blend_state(pipe, blend_cso);
267 pipe->bind_depth_stencil_alpha_state(pipe, depthstencil_cso);
268 pipe->bind_rasterizer_state(pipe, rasterizer_cso);
269
270 /* bind shaders */
271 pipe->bind_fs_state(pipe, stfp->fs->data);
272 pipe->bind_vs_state(pipe, stvp->cso->data);
273
274 /*
275 * XXX for small mipmap levels, it may be faster to use the software
276 * fallback path...
277 */
278 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
279 const uint srcLevel = dstLevel - 1;
280
281 /*
282 * Setup framebuffer / dest surface
283 */
284 fb.cbufs[0] = pipe->get_tex_surface(pipe, pt, face, dstLevel, zslice);
285 pipe->set_framebuffer_state(pipe, &fb);
286
287 /*
288 * Setup sampler state
289 */
290 sampler.min_lod = sampler.max_lod = srcLevel;
291 sampler_cso = pipe->create_sampler_state(pipe, &sampler);
292 pipe->bind_sampler_state(pipe, 0, sampler_cso);
293
294 simple_viewport(pipe, pt->width[dstLevel], pt->height[dstLevel]);
295
296 /*
297 * Setup src texture, override pt->first_level so we sample from
298 * the right mipmap level.
299 */
300 /*pt->first_level = srcLevel;*/
301 pipe->set_sampler_texture(pipe, 0, pt);
302
303 draw_quad(st->ctx);
304
305 pipe->delete_sampler_state(pipe, sampler_cso);
306 }
307
308 /* restore first_level */
309 /*pt->first_level = first_level_save;*/
310
311 /* restore pipe state */
312 if (st->state.rasterizer)
313 pipe->bind_rasterizer_state(pipe, st->state.rasterizer->data);
314 if (st->state.fs)
315 pipe->bind_fs_state(pipe, st->state.fs->data);
316 if (st->state.vs)
317 pipe->bind_vs_state(pipe, st->state.vs->cso->data);
318 if (st->state.sampler[0])
319 pipe->bind_sampler_state(pipe, 0, st->state.sampler[0]->data);
320 pipe->set_sampler_texture(pipe, 0, st->state.sampler_texture[0]);
321 pipe->set_viewport_state(pipe, &st->state.viewport);
322
323 return TRUE;
324 }
325
326
327 static void
328 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
329 struct gl_texture_object *texObj)
330 {
331 struct pipe_context *pipe = ctx->st->pipe;
332 struct pipe_winsys *ws = pipe->winsys;
333 struct pipe_texture *pt = st_get_texobj_texture(texObj);
334 const uint baseLevel = texObj->BaseLevel;
335 const uint lastLevel = pt->last_level;
336 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
337 uint dstLevel;
338 GLenum datatype;
339 GLuint comps;
340
341 assert(target != GL_TEXTURE_3D); /* not done yet */
342
343 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
344 &datatype, &comps);
345
346 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
347 const uint srcLevel = dstLevel - 1;
348 struct pipe_surface *srcSurf, *dstSurf;
349 const ubyte *srcData;
350 ubyte *dstData;
351
352 srcSurf = pipe->get_tex_surface(pipe, pt, face, srcLevel, zslice);
353 dstSurf = pipe->get_tex_surface(pipe, pt, face, dstLevel, zslice);
354
355 srcData = (ubyte *) ws->buffer_map(ws, srcSurf->buffer,
356 PIPE_BUFFER_USAGE_CPU_READ)
357 + srcSurf->offset;
358 dstData = (ubyte *) ws->buffer_map(ws, dstSurf->buffer,
359 PIPE_BUFFER_USAGE_CPU_WRITE)
360 + dstSurf->offset;
361
362 _mesa_generate_mipmap_level(target, datatype, comps,
363 0 /*border*/,
364 pt->width[srcLevel], pt->height[srcLevel], pt->depth[srcLevel],
365 srcSurf->pitch * srcSurf->cpp, /* stride in bytes */
366 srcData,
367 pt->width[dstLevel], pt->height[dstLevel], pt->depth[dstLevel],
368 dstSurf->pitch * dstSurf->cpp, /* stride in bytes */
369 dstData);
370
371 ws->buffer_unmap(ws, srcSurf->buffer);
372 ws->buffer_unmap(ws, dstSurf->buffer);
373
374 pipe_surface_reference(&srcSurf, NULL);
375 pipe_surface_reference(&dstSurf, NULL);
376 }
377 }
378
379
380 void
381 st_generate_mipmap(GLcontext *ctx, GLenum target,
382 struct gl_texture_object *texObj)
383 {
384 struct st_context *st = ctx->st;
385 struct pipe_texture *pt = st_get_texobj_texture(texObj);
386 const uint baseLevel = texObj->BaseLevel;
387 const uint lastLevel = pt->last_level;
388 uint dstLevel;
389
390 if (!st_render_mipmap(st, target, pt, baseLevel, lastLevel)) {
391 fallback_generate_mipmap(ctx, target, texObj);
392 }
393
394 /* Fill in the Mesa gl_texture_image fields */
395 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
396 const uint srcLevel = dstLevel - 1;
397 const struct gl_texture_image *srcImage
398 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
399 struct gl_texture_image *dstImage;
400 struct st_texture_image *stImage;
401 uint dstWidth = pt->width[dstLevel];
402 uint dstHeight = pt->height[dstLevel];
403 uint dstDepth = pt->depth[dstLevel];
404 uint border = srcImage->Border;
405
406 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
407 if (!dstImage) {
408 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
409 return;
410 }
411
412 if (dstImage->ImageOffsets)
413 _mesa_free(dstImage->ImageOffsets);
414
415 /* Free old image data */
416 if (dstImage->Data)
417 ctx->Driver.FreeTexImageData(ctx, dstImage);
418
419 /* initialize new image */
420 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
421 dstDepth, border, srcImage->InternalFormat);
422
423 dstImage->TexFormat = srcImage->TexFormat;
424
425 stImage = (struct st_texture_image *) dstImage;
426 stImage->pt = pt;
427 }
428 }