[MSACM32]
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / state_tracker / st_atom_texture.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 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/samplerobj.h"
38 #include "program/prog_instruction.h"
39
40 #include "st_context.h"
41 #include "st_atom.h"
42 #include "st_texture.h"
43 #include "st_format.h"
44 #include "st_cb_texture.h"
45 #include "pipe/p_context.h"
46 #include "util/u_format.h"
47 #include "util/u_inlines.h"
48 #include "cso_cache/cso_context.h"
49
50
51 /**
52 * Combine depth texture mode with "swizzle" so that depth mode swizzling
53 * takes place before texture swizzling, and return the resulting swizzle.
54 * If the format is not a depth format, return "swizzle" unchanged.
55 *
56 * \param format PIPE_FORMAT_*.
57 * \param swizzle Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
58 * \param depthmode One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA, GL_RED.
59 */
60 static GLuint
61 apply_depthmode(enum pipe_format format, GLuint swizzle, GLenum depthmode)
62 {
63 const struct util_format_description *desc =
64 util_format_description(format);
65 unsigned char swiz[4];
66 unsigned i;
67
68 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS ||
69 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
70 /* Not a depth format. */
71 return swizzle;
72 }
73
74 for (i = 0; i < 4; i++)
75 swiz[i] = GET_SWZ(swizzle, i);
76
77 switch (depthmode) {
78 case GL_LUMINANCE:
79 /* Rewrite reads from W to ONE, and reads from XYZ to XXX. */
80 for (i = 0; i < 4; i++)
81 if (swiz[i] == SWIZZLE_W)
82 swiz[i] = SWIZZLE_ONE;
83 else if (swiz[i] < SWIZZLE_W)
84 swiz[i] = SWIZZLE_X;
85 break;
86
87 case GL_INTENSITY:
88 /* Rewrite reads from XYZW to XXXX. */
89 for (i = 0; i < 4; i++)
90 if (swiz[i] <= SWIZZLE_W)
91 swiz[i] = SWIZZLE_X;
92 break;
93
94 case GL_ALPHA:
95 /* Rewrite reads from W to X, and reads from XYZ to 000. */
96 for (i = 0; i < 4; i++)
97 if (swiz[i] == SWIZZLE_W)
98 swiz[i] = SWIZZLE_X;
99 else if (swiz[i] < SWIZZLE_W)
100 swiz[i] = SWIZZLE_ZERO;
101 break;
102 case GL_RED:
103 /* Rewrite reads W to 1, XYZ to X00 */
104 for (i = 0; i < 4; i++)
105 if (swiz[i] == SWIZZLE_W)
106 swiz[i] = SWIZZLE_ONE;
107 else if (swiz[i] == SWIZZLE_Y || swiz[i] == SWIZZLE_Z)
108 swiz[i] = SWIZZLE_ZERO;
109 break;
110 }
111
112 return MAKE_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
113 }
114
115
116 /**
117 * Return TRUE if the swizzling described by "swizzle" and
118 * "depthmode" (for depth textures only) is different from the swizzling
119 * set in the given sampler view.
120 *
121 * \param sv A sampler view.
122 * \param swizzle Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
123 * \param depthmode One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA.
124 */
125 static boolean
126 check_sampler_swizzle(struct pipe_sampler_view *sv,
127 GLuint swizzle, GLenum depthmode)
128 {
129 swizzle = apply_depthmode(sv->texture->format, swizzle, depthmode);
130
131 if ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
132 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
133 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
134 (sv->swizzle_a != GET_SWZ(swizzle, 3)))
135 return TRUE;
136 return FALSE;
137 }
138
139
140 static INLINE struct pipe_sampler_view *
141 st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
142 struct st_texture_object *stObj,
143 const struct gl_sampler_object *samp,
144 enum pipe_format format)
145 {
146 struct pipe_sampler_view templ;
147 GLuint swizzle = apply_depthmode(stObj->pt->format,
148 stObj->base._Swizzle,
149 samp->DepthMode);
150
151 u_sampler_view_default_template(&templ,
152 stObj->pt,
153 format);
154 templ.u.tex.first_level = stObj->base.BaseLevel;
155
156 if (swizzle != SWIZZLE_NOOP) {
157 templ.swizzle_r = GET_SWZ(swizzle, 0);
158 templ.swizzle_g = GET_SWZ(swizzle, 1);
159 templ.swizzle_b = GET_SWZ(swizzle, 2);
160 templ.swizzle_a = GET_SWZ(swizzle, 3);
161 }
162
163 return pipe->create_sampler_view(pipe, stObj->pt, &templ);
164 }
165
166
167 static INLINE struct pipe_sampler_view *
168 st_get_texture_sampler_view_from_stobj(struct st_texture_object *stObj,
169 struct pipe_context *pipe,
170 const struct gl_sampler_object *samp,
171 enum pipe_format format)
172 {
173 if (!stObj || !stObj->pt) {
174 return NULL;
175 }
176
177 if (!stObj->sampler_view) {
178 stObj->sampler_view =
179 st_create_texture_sampler_view_from_stobj(pipe, stObj, samp, format);
180 }
181
182 return stObj->sampler_view;
183 }
184
185 static GLboolean
186 update_single_texture(struct st_context *st, struct pipe_sampler_view **sampler_view,
187 GLuint texUnit)
188 {
189 struct pipe_context *pipe = st->pipe;
190 struct gl_context *ctx = st->ctx;
191 const struct gl_sampler_object *samp;
192 struct gl_texture_object *texObj;
193 struct st_texture_object *stObj;
194 enum pipe_format st_view_format;
195 GLboolean retval;
196
197 samp = _mesa_get_samplerobj(ctx, texUnit);
198
199 texObj = ctx->Texture.Unit[texUnit]._Current;
200
201 if (!texObj) {
202 texObj = st_get_default_texture(st);
203 samp = &texObj->Sampler;
204 }
205 stObj = st_texture_object(texObj);
206
207 retval = st_finalize_texture(ctx, st->pipe, texObj);
208 if (!retval) {
209 /* out of mem */
210 return GL_FALSE;
211 }
212
213 /* Determine the format of the texture sampler view */
214 st_view_format = stObj->pt->format;
215 {
216 const struct st_texture_image *firstImage =
217 st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
218 const gl_format texFormat = firstImage->base.TexFormat;
219 enum pipe_format firstImageFormat =
220 st_mesa_format_to_pipe_format(texFormat);
221
222 if ((samp->sRGBDecode == GL_SKIP_DECODE_EXT) &&
223 (_mesa_get_format_color_encoding(texFormat) == GL_SRGB)) {
224 /* Don't do sRGB->RGB conversion. Interpret the texture data as
225 * linear values.
226 */
227 const gl_format linearFormat =
228 _mesa_get_srgb_format_linear(texFormat);
229 firstImageFormat = st_mesa_format_to_pipe_format(linearFormat);
230 }
231
232 if (firstImageFormat != stObj->pt->format)
233 st_view_format = firstImageFormat;
234 }
235
236
237 /* if sampler view has changed dereference it */
238 if (stObj->sampler_view) {
239 if (check_sampler_swizzle(stObj->sampler_view,
240 stObj->base._Swizzle,
241 samp->DepthMode) ||
242 (st_view_format != stObj->sampler_view->format) ||
243 stObj->base.BaseLevel != stObj->sampler_view->u.tex.first_level) {
244 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
245 }
246 }
247
248 *sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe,
249 samp,
250 st_view_format);
251 return GL_TRUE;
252 }
253
254 static void
255 update_vertex_textures(struct st_context *st)
256 {
257 const struct gl_context *ctx = st->ctx;
258 struct gl_vertex_program *vprog = ctx->VertexProgram._Current;
259 GLuint su;
260
261 st->state.num_vertex_textures = 0;
262
263 /* loop over sampler units (aka tex image units) */
264 for (su = 0; su < ctx->Const.MaxTextureImageUnits; su++) {
265 struct pipe_sampler_view *sampler_view = NULL;
266 if (vprog->Base.SamplersUsed & (1 << su)) {
267 GLboolean retval;
268 GLuint texUnit;
269
270 texUnit = vprog->Base.SamplerUnits[su];
271
272 retval = update_single_texture(st, &sampler_view, texUnit);
273 if (retval == GL_FALSE)
274 continue;
275
276 st->state.num_vertex_textures = su + 1;
277 }
278 pipe_sampler_view_reference(&st->state.sampler_vertex_views[su], sampler_view);
279 }
280
281 if (ctx->Const.MaxVertexTextureImageUnits > 0) {
282 GLuint numUnits = MIN2(st->state.num_vertex_textures,
283 ctx->Const.MaxVertexTextureImageUnits);
284 cso_set_vertex_sampler_views(st->cso_context,
285 numUnits,
286 st->state.sampler_vertex_views);
287 }
288 }
289
290 static void
291 update_fragment_textures(struct st_context *st)
292 {
293 const struct gl_context *ctx = st->ctx;
294 struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
295 GLuint su;
296
297 st->state.num_textures = 0;
298
299 /* loop over sampler units (aka tex image units) */
300 for (su = 0; su < ctx->Const.MaxTextureImageUnits; su++) {
301 struct pipe_sampler_view *sampler_view = NULL;
302 if (fprog->Base.SamplersUsed & (1 << su)) {
303 GLboolean retval;
304 GLuint texUnit;
305
306 texUnit = fprog->Base.SamplerUnits[su];
307
308 retval = update_single_texture(st, &sampler_view, texUnit);
309 if (retval == GL_FALSE)
310 continue;
311
312 st->state.num_textures = su + 1;
313 }
314 pipe_sampler_view_reference(&st->state.sampler_views[su], sampler_view);
315 }
316
317 cso_set_fragment_sampler_views(st->cso_context,
318 st->state.num_textures,
319 st->state.sampler_views);
320 }
321
322 const struct st_tracked_state st_update_texture = {
323 "st_update_texture", /* name */
324 { /* dirty */
325 _NEW_TEXTURE, /* mesa */
326 ST_NEW_FRAGMENT_PROGRAM, /* st */
327 },
328 update_fragment_textures /* update */
329 };
330
331 const struct st_tracked_state st_update_vertex_texture = {
332 "st_update_vertex_texture", /* name */
333 { /* dirty */
334 _NEW_TEXTURE, /* mesa */
335 ST_NEW_VERTEX_PROGRAM, /* st */
336 },
337 update_vertex_textures /* update */
338 };
339
340 static void
341 finalize_textures(struct st_context *st)
342 {
343 struct gl_context *ctx = st->ctx;
344 struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
345 const GLboolean prev_missing_textures = st->missing_textures;
346 GLuint su;
347
348 st->missing_textures = GL_FALSE;
349
350 for (su = 0; su < ctx->Const.MaxTextureCoordUnits; su++) {
351 if (fprog->Base.SamplersUsed & (1 << su)) {
352 const GLuint texUnit = fprog->Base.SamplerUnits[su];
353 struct gl_texture_object *texObj
354 = ctx->Texture.Unit[texUnit]._Current;
355
356 if (texObj) {
357 GLboolean retval;
358
359 retval = st_finalize_texture(ctx, st->pipe, texObj);
360 if (!retval) {
361 /* out of mem */
362 st->missing_textures = GL_TRUE;
363 continue;
364 }
365 }
366 }
367 }
368
369 if (prev_missing_textures != st->missing_textures)
370 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
371 }
372
373
374
375 const struct st_tracked_state st_finalize_textures = {
376 "st_finalize_textures", /* name */
377 { /* dirty */
378 _NEW_TEXTURE, /* mesa */
379 0, /* st */
380 },
381 finalize_textures /* update */
382 };