1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/mipmap.h"
31 #include "main/teximage.h"
32 #include "main/texformat.h"
34 #include "shader/prog_instruction.h"
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"
42 #include "st_context.h"
44 #include "st_gen_mipmap.h"
45 #include "st_program.h"
46 #include "st_cb_drawpixels.h"
47 #include "st_cb_texture.h"
51 static void *blend_cso
= NULL
;
52 static void *depthstencil_cso
= NULL
;
53 static void *rasterizer_cso
= NULL
;
55 static struct st_fragment_program
*stfp
= NULL
;
56 static struct st_vertex_program
*stvp
= NULL
;
60 static struct st_fragment_program
*
61 make_tex_fragment_program(GLcontext
*ctx
)
63 struct st_fragment_program
*stfp
;
67 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
71 p
->NumInstructions
= 2;
73 p
->Instructions
= _mesa_alloc_instructions(p
->NumInstructions
);
74 if (!p
->Instructions
) {
75 ctx
->Driver
.DeleteProgram(ctx
, p
);
78 _mesa_init_instructions(p
->Instructions
, p
->NumInstructions
);
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
;
91 p
->Instructions
[ic
++].Opcode
= OPCODE_END
;
93 assert(ic
== p
->NumInstructions
);
95 p
->InputsRead
= FRAG_BIT_TEX0
;
96 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
98 stfp
= (struct st_fragment_program
*) p
;
100 st_translate_fragment_program(ctx
->st
, stfp
, NULL
,
101 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
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.
115 st_init_generate_mipmap(struct st_context
*st
)
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
;
124 memset(&blend
, 0, sizeof(blend
));
125 blend
.colormask
= PIPE_MASK_RGBA
;
126 blend_cso
= pipe
->create_blend_state(pipe
, &blend
);
128 memset(&depthstencil
, 0, sizeof(depthstencil
));
129 depthstencil_cso
= pipe
->create_depth_stencil_alpha_state(pipe
, &depthstencil
);
131 memset(&rasterizer
, 0, sizeof(rasterizer
));
132 rasterizer_cso
= pipe
->create_rasterizer_state(pipe
, &rasterizer
);
134 stfp
= make_tex_fragment_program(st
->ctx
);
135 stvp
= st_make_passthrough_vertex_shader(st
, GL_FALSE
);
140 st_destroy_generate_mipmpap(struct st_context
*st
)
142 struct pipe_context
*pipe
= st
->pipe
;
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
);
148 /* XXX free stfp, stvp */
151 depthstencil_cso
= NULL
;
152 rasterizer_cso
= NULL
;
157 simple_viewport(struct pipe_context
*pipe
, uint width
, uint height
)
159 struct pipe_viewport_state vp
;
161 vp
.scale
[0] = 0.5 * width
;
162 vp
.scale
[1] = -0.5 * height
;
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;
170 pipe
->set_viewport_state(pipe
, &vp
);
176 * Draw simple [-1,1]x[-1,1] quad
179 draw_quad(GLcontext
*ctx
)
181 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
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;
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 */
197 verts
[1][1][0] = sRight
;
198 verts
[1][1][1] = tTop
;
203 verts
[2][1][0] = sRight
;
204 verts
[2][1][1] = tBot
;
209 verts
[3][1][0] = sLeft
;
210 verts
[3][1][1] = tBot
;
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*/
220 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2, GL_TRUE
);
226 * Generate mipmap levels using hardware rendering.
227 * \return TRUE if successful, FALSE if not possible
230 st_render_mipmap(struct st_context
*st
,
232 struct pipe_texture
*pt
,
233 uint baseLevel
, uint lastLevel
)
235 struct pipe_context
*pipe
= st
->pipe
;
236 struct pipe_framebuffer_state fb
;
237 struct pipe_sampler_state sampler
;
239 const uint face
= _mesa_tex_target_to_face(target
), zslice
= 0;
240 /*const uint first_level_save = pt->first_level;*/
243 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
245 /* check if we can render in the texture's format */
246 if (!pipe
->is_format_supported(pipe
, pt
->format
, PIPE_SURFACE
)) {
250 /* init framebuffer state */
251 memset(&fb
, 0, sizeof(fb
));
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;
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
);
271 pipe
->bind_fs_state(pipe
, stfp
->fs
->data
);
272 pipe
->bind_vs_state(pipe
, stvp
->cso
->data
);
275 * XXX for small mipmap levels, it may be faster to use the software
278 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
279 const uint srcLevel
= dstLevel
- 1;
282 * Setup framebuffer / dest surface
284 fb
.cbufs
[0] = pipe
->get_tex_surface(pipe
, pt
, face
, dstLevel
, zslice
);
285 pipe
->set_framebuffer_state(pipe
, &fb
);
288 * Setup sampler state
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
);
294 simple_viewport(pipe
, pt
->width
[dstLevel
], pt
->height
[dstLevel
]);
297 * Setup src texture, override pt->first_level so we sample from
298 * the right mipmap level.
300 /*pt->first_level = srcLevel;*/
301 pipe
->set_sampler_texture(pipe
, 0, pt
);
305 pipe
->delete_sampler_state(pipe
, sampler_cso
);
308 /* restore first_level */
309 /*pt->first_level = first_level_save;*/
311 /* restore pipe state */
312 if (st
->state
.rasterizer
)
313 pipe
->bind_rasterizer_state(pipe
, st
->state
.rasterizer
->data
);
315 pipe
->bind_fs_state(pipe
, st
->state
.fs
->data
);
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
);
328 fallback_generate_mipmap(GLcontext
*ctx
, GLenum target
,
329 struct gl_texture_object
*texObj
)
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;
341 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
343 _mesa_format_to_type_and_comps(texObj
->Image
[face
][baseLevel
]->TexFormat
,
346 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
347 const uint srcLevel
= dstLevel
- 1;
348 struct pipe_surface
*srcSurf
, *dstSurf
;
349 const ubyte
*srcData
;
352 srcSurf
= pipe
->get_tex_surface(pipe
, pt
, face
, srcLevel
, zslice
);
353 dstSurf
= pipe
->get_tex_surface(pipe
, pt
, face
, dstLevel
, zslice
);
355 srcData
= (ubyte
*) ws
->buffer_map(ws
, srcSurf
->buffer
,
356 PIPE_BUFFER_USAGE_CPU_READ
)
358 dstData
= (ubyte
*) ws
->buffer_map(ws
, dstSurf
->buffer
,
359 PIPE_BUFFER_USAGE_CPU_WRITE
)
362 _mesa_generate_mipmap_level(target
, datatype
, comps
,
364 pt
->width
[srcLevel
], pt
->height
[srcLevel
], pt
->depth
[srcLevel
],
365 srcSurf
->pitch
* srcSurf
->cpp
, /* stride in bytes */
367 pt
->width
[dstLevel
], pt
->height
[dstLevel
], pt
->depth
[dstLevel
],
368 dstSurf
->pitch
* dstSurf
->cpp
, /* stride in bytes */
371 ws
->buffer_unmap(ws
, srcSurf
->buffer
);
372 ws
->buffer_unmap(ws
, dstSurf
->buffer
);
374 pipe_surface_reference(&srcSurf
, NULL
);
375 pipe_surface_reference(&dstSurf
, NULL
);
381 st_generate_mipmap(GLcontext
*ctx
, GLenum target
,
382 struct gl_texture_object
*texObj
)
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
;
390 if (!st_render_mipmap(st
, target
, pt
, baseLevel
, lastLevel
)) {
391 fallback_generate_mipmap(ctx
, target
, texObj
);
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
;
406 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
408 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
412 if (dstImage
->ImageOffsets
)
413 _mesa_free(dstImage
->ImageOffsets
);
415 /* Free old image data */
417 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
419 /* initialize new image */
420 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
421 dstDepth
, border
, srcImage
->InternalFormat
);
423 dstImage
->TexFormat
= srcImage
->TexFormat
;
425 stImage
= (struct st_texture_image
*) dstImage
;