st/mesa: store state that affects sampler views per context
[mesa.git] / src / mesa / state_tracker / st_atom_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
31 * Brian Paul
32 */
33
34
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "main/samplerobj.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41 #include "program/prog_instruction.h"
42
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_sampler_view.h"
46 #include "st_texture.h"
47 #include "st_format.h"
48 #include "st_cb_texture.h"
49 #include "pipe/p_context.h"
50 #include "util/u_format.h"
51 #include "util/u_inlines.h"
52 #include "cso_cache/cso_context.h"
53
54
55 /**
56 * Get a pipe_sampler_view object from a texture unit.
57 */
58 void
59 st_update_single_texture(struct st_context *st,
60 struct pipe_sampler_view **sampler_view,
61 GLuint texUnit, bool glsl130_or_later)
62 {
63 struct gl_context *ctx = st->ctx;
64 const struct gl_sampler_object *samp;
65 struct gl_texture_object *texObj;
66 struct st_texture_object *stObj;
67
68 samp = _mesa_get_samplerobj(ctx, texUnit);
69
70 texObj = ctx->Texture.Unit[texUnit]._Current;
71 assert(texObj);
72
73 stObj = st_texture_object(texObj);
74
75 if (unlikely(texObj->Target == GL_TEXTURE_BUFFER)) {
76 *sampler_view = st_get_buffer_sampler_view_from_stobj(st, stObj);
77 return;
78 }
79
80 if (!st_finalize_texture(ctx, st->pipe, texObj, 0) ||
81 !stObj->pt) {
82 /* out of mem */
83 *sampler_view = NULL;
84 return;
85 }
86
87 if (texObj->TargetIndex == TEXTURE_EXTERNAL_INDEX &&
88 stObj->pt->screen->resource_changed)
89 stObj->pt->screen->resource_changed(stObj->pt->screen, stObj->pt);
90
91 *sampler_view =
92 st_get_texture_sampler_view_from_stobj(st, stObj, samp,
93 glsl130_or_later);
94 }
95
96
97
98 static void
99 update_textures(struct st_context *st,
100 enum pipe_shader_type shader_stage,
101 const struct gl_program *prog,
102 struct pipe_sampler_view **sampler_views,
103 unsigned *out_num_textures)
104 {
105 const GLuint old_max = *out_num_textures;
106 GLbitfield samplers_used = prog->SamplersUsed;
107 GLbitfield free_slots = ~prog->SamplersUsed;
108 GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
109 GLuint unit;
110
111 if (samplers_used == 0x0 && old_max == 0)
112 return;
113
114 unsigned num_textures = 0;
115
116 /* prog->sh.data is NULL if it's ARB_fragment_program */
117 bool glsl130 = (prog->sh.data ? prog->sh.data->Version : 0) >= 130;
118
119 /* loop over sampler units (aka tex image units) */
120 for (unit = 0; samplers_used || unit < old_max;
121 unit++, samplers_used >>= 1) {
122 struct pipe_sampler_view *sampler_view = NULL;
123
124 if (samplers_used & 1) {
125 const GLuint texUnit = prog->SamplerUnits[unit];
126
127 st_update_single_texture(st, &sampler_view, texUnit, glsl130);
128 num_textures = unit + 1;
129 }
130
131 pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
132 }
133
134 /* For any external samplers with multiplaner YUV, stuff the additional
135 * sampler views we need at the end.
136 *
137 * Trying to cache the sampler view in the stObj looks painful, so just
138 * re-create the sampler view for the extra planes each time. Main use
139 * case is video playback (ie. fps games wouldn't be using this) so I
140 * guess no point to try to optimize this feature.
141 */
142 while (unlikely(external_samplers_used)) {
143 GLuint unit = u_bit_scan(&external_samplers_used);
144 GLuint extra = 0;
145 struct st_texture_object *stObj =
146 st_get_texture_object(st->ctx, prog, unit);
147 struct pipe_sampler_view tmpl;
148
149 if (!stObj)
150 continue;
151
152 /* use original view as template: */
153 tmpl = *sampler_views[unit];
154
155 switch (st_get_view_format(stObj)) {
156 case PIPE_FORMAT_NV12:
157 /* we need one additional R8G8 view: */
158 tmpl.format = PIPE_FORMAT_RG88_UNORM;
159 tmpl.swizzle_g = PIPE_SWIZZLE_Y; /* tmpl from Y plane is R8 */
160 extra = u_bit_scan(&free_slots);
161 sampler_views[extra] =
162 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
163 break;
164 case PIPE_FORMAT_IYUV:
165 /* we need two additional R8 views: */
166 tmpl.format = PIPE_FORMAT_R8_UNORM;
167 extra = u_bit_scan(&free_slots);
168 sampler_views[extra] =
169 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
170 extra = u_bit_scan(&free_slots);
171 sampler_views[extra] =
172 st->pipe->create_sampler_view(st->pipe, stObj->pt->next->next, &tmpl);
173 break;
174 default:
175 break;
176 }
177
178 num_textures = MAX2(num_textures, extra + 1);
179 }
180
181 cso_set_sampler_views(st->cso_context,
182 shader_stage,
183 num_textures,
184 sampler_views);
185 *out_num_textures = num_textures;
186 }
187
188
189
190 void
191 st_update_vertex_textures(struct st_context *st)
192 {
193 const struct gl_context *ctx = st->ctx;
194
195 if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
196 update_textures(st,
197 PIPE_SHADER_VERTEX,
198 ctx->VertexProgram._Current,
199 st->state.sampler_views[PIPE_SHADER_VERTEX],
200 &st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
201 }
202 }
203
204
205 void
206 st_update_fragment_textures(struct st_context *st)
207 {
208 const struct gl_context *ctx = st->ctx;
209
210 update_textures(st,
211 PIPE_SHADER_FRAGMENT,
212 ctx->FragmentProgram._Current,
213 st->state.sampler_views[PIPE_SHADER_FRAGMENT],
214 &st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
215 }
216
217
218 void
219 st_update_geometry_textures(struct st_context *st)
220 {
221 const struct gl_context *ctx = st->ctx;
222
223 if (ctx->GeometryProgram._Current) {
224 update_textures(st,
225 PIPE_SHADER_GEOMETRY,
226 ctx->GeometryProgram._Current,
227 st->state.sampler_views[PIPE_SHADER_GEOMETRY],
228 &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
229 }
230 }
231
232
233 void
234 st_update_tessctrl_textures(struct st_context *st)
235 {
236 const struct gl_context *ctx = st->ctx;
237
238 if (ctx->TessCtrlProgram._Current) {
239 update_textures(st,
240 PIPE_SHADER_TESS_CTRL,
241 ctx->TessCtrlProgram._Current,
242 st->state.sampler_views[PIPE_SHADER_TESS_CTRL],
243 &st->state.num_sampler_views[PIPE_SHADER_TESS_CTRL]);
244 }
245 }
246
247
248 void
249 st_update_tesseval_textures(struct st_context *st)
250 {
251 const struct gl_context *ctx = st->ctx;
252
253 if (ctx->TessEvalProgram._Current) {
254 update_textures(st,
255 PIPE_SHADER_TESS_EVAL,
256 ctx->TessEvalProgram._Current,
257 st->state.sampler_views[PIPE_SHADER_TESS_EVAL],
258 &st->state.num_sampler_views[PIPE_SHADER_TESS_EVAL]);
259 }
260 }
261
262
263 void
264 st_update_compute_textures(struct st_context *st)
265 {
266 const struct gl_context *ctx = st->ctx;
267
268 if (ctx->ComputeProgram._Current) {
269 update_textures(st,
270 PIPE_SHADER_COMPUTE,
271 ctx->ComputeProgram._Current,
272 st->state.sampler_views[PIPE_SHADER_COMPUTE],
273 &st->state.num_sampler_views[PIPE_SHADER_COMPUTE]);
274 }
275 }