mesa: rename src/mesa/shader/ to src/mesa/program/
[mesa.git] / 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 "program/prog_instruction.h"
37
38 #include "st_context.h"
39 #include "st_atom.h"
40 #include "st_texture.h"
41 #include "st_format.h"
42 #include "st_cb_texture.h"
43 #include "pipe/p_context.h"
44 #include "util/u_inlines.h"
45 #include "cso_cache/cso_context.h"
46
47 static boolean check_sampler_swizzle(struct pipe_sampler_view *sv,
48 uint32_t _swizzle)
49 {
50 if ((sv->swizzle_r != GET_SWZ(_swizzle, 0)) ||
51 (sv->swizzle_g != GET_SWZ(_swizzle, 1)) ||
52 (sv->swizzle_b != GET_SWZ(_swizzle, 2)) ||
53 (sv->swizzle_a != GET_SWZ(_swizzle, 3)))
54 return true;
55 return false;
56 }
57
58 static INLINE struct pipe_sampler_view *
59 st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
60 struct st_texture_object *stObj,
61 enum pipe_format format)
62
63 {
64 struct pipe_sampler_view templ;
65
66 u_sampler_view_default_template(&templ,
67 stObj->pt,
68 format);
69
70 if (stObj->base._Swizzle != SWIZZLE_NOOP) {
71 templ.swizzle_r = GET_SWZ(stObj->base._Swizzle, 0);
72 templ.swizzle_g = GET_SWZ(stObj->base._Swizzle, 1);
73 templ.swizzle_b = GET_SWZ(stObj->base._Swizzle, 2);
74 templ.swizzle_a = GET_SWZ(stObj->base._Swizzle, 3);
75 }
76
77 return pipe->create_sampler_view(pipe, stObj->pt, &templ);
78 }
79
80
81 static INLINE struct pipe_sampler_view *
82 st_get_texture_sampler_view_from_stobj(struct st_texture_object *stObj,
83 struct pipe_context *pipe,
84 enum pipe_format format)
85
86 {
87 if (!stObj || !stObj->pt) {
88 return NULL;
89 }
90
91 if (!stObj->sampler_view) {
92 stObj->sampler_view = st_create_texture_sampler_view_from_stobj(pipe, stObj, format);
93 }
94
95 return stObj->sampler_view;
96 }
97
98 static void
99 update_textures(struct st_context *st)
100 {
101 struct pipe_context *pipe = st->pipe;
102 struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current;
103 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
104 const GLbitfield samplersUsed = (vprog->Base.SamplersUsed |
105 fprog->Base.SamplersUsed);
106 GLuint su;
107
108 st->state.num_textures = 0;
109
110 /* loop over sampler units (aka tex image units) */
111 for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) {
112 struct pipe_sampler_view *sampler_view = NULL;
113 enum pipe_format st_view_format;
114 if (samplersUsed & (1 << su)) {
115 struct gl_texture_object *texObj;
116 struct st_texture_object *stObj;
117 GLboolean retval;
118 GLuint texUnit;
119
120 if (fprog->Base.SamplersUsed & (1 << su))
121 texUnit = fprog->Base.SamplerUnits[su];
122 else
123 texUnit = vprog->Base.SamplerUnits[su];
124
125 texObj = st->ctx->Texture.Unit[texUnit]._Current;
126
127 if (!texObj) {
128 texObj = st_get_default_texture(st);
129 }
130 stObj = st_texture_object(texObj);
131
132 retval = st_finalize_texture(st->ctx, st->pipe, texObj);
133 if (!retval) {
134 /* out of mem */
135 continue;
136 }
137
138 st_view_format = stObj->pt->format;
139 {
140 struct st_texture_image *firstImage;
141 enum pipe_format firstImageFormat;
142 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
143
144 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
145 if (firstImageFormat != stObj->pt->format)
146 st_view_format = firstImageFormat;
147
148 }
149 st->state.num_textures = su + 1;
150
151 /* if sampler view has changed dereference it */
152 if (stObj->sampler_view)
153 if (check_sampler_swizzle(stObj->sampler_view, stObj->base._Swizzle) || (st_view_format != stObj->sampler_view->format))
154 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
155
156 sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe, st_view_format);
157 }
158 pipe_sampler_view_reference(&st->state.sampler_views[su], sampler_view);
159 }
160
161 cso_set_fragment_sampler_views(st->cso_context,
162 st->state.num_textures,
163 st->state.sampler_views);
164 if (st->ctx->Const.MaxVertexTextureImageUnits > 0) {
165 cso_set_vertex_sampler_views(st->cso_context,
166 MIN2(st->state.num_textures,
167 st->ctx->Const.MaxVertexTextureImageUnits),
168 st->state.sampler_views);
169 }
170 }
171
172
173 const struct st_tracked_state st_update_texture = {
174 "st_update_texture", /* name */
175 { /* dirty */
176 _NEW_TEXTURE, /* mesa */
177 ST_NEW_FRAGMENT_PROGRAM, /* st */
178 },
179 update_textures /* update */
180 };
181
182
183
184
185 static void
186 finalize_textures(struct st_context *st)
187 {
188 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
189 const GLboolean prev_missing_textures = st->missing_textures;
190 GLuint su;
191
192 st->missing_textures = GL_FALSE;
193
194 for (su = 0; su < st->ctx->Const.MaxTextureCoordUnits; su++) {
195 if (fprog->Base.SamplersUsed & (1 << su)) {
196 const GLuint texUnit = fprog->Base.SamplerUnits[su];
197 struct gl_texture_object *texObj
198 = st->ctx->Texture.Unit[texUnit]._Current;
199
200 if (texObj) {
201 GLboolean retval;
202
203 retval = st_finalize_texture(st->ctx, st->pipe, texObj);
204 if (!retval) {
205 /* out of mem */
206 st->missing_textures = GL_TRUE;
207 continue;
208 }
209 }
210 }
211 }
212
213 if (prev_missing_textures != st->missing_textures)
214 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
215 }
216
217
218
219 const struct st_tracked_state st_finalize_textures = {
220 "st_finalize_textures", /* name */
221 { /* dirty */
222 _NEW_TEXTURE, /* mesa */
223 0, /* st */
224 },
225 finalize_textures /* update */
226 };