gallium/radeon: assign the highest priority to scratch; make rings second
[mesa.git] / src / gallium / drivers / svga / svga_state_sampler.c
1 /*
2 * Copyright 2013 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25
26 /**
27 * VGPU10 sampler and sampler view functions.
28 */
29
30
31 #include "pipe/p_defines.h"
32 #include "util/u_bitmask.h"
33 #include "util/u_inlines.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36
37 #include "svga_cmd.h"
38 #include "svga_context.h"
39 #include "svga_format.h"
40 #include "svga_resource_buffer.h"
41 #include "svga_resource_texture.h"
42 #include "svga_shader.h"
43 #include "svga_state.h"
44 #include "svga_sampler_view.h"
45
46
47 /** Get resource handle for a texture or buffer */
48 static inline struct svga_winsys_surface *
49 svga_resource_handle(struct pipe_resource *res)
50 {
51 if (res->target == PIPE_BUFFER) {
52 return svga_buffer(res)->handle;
53 }
54 else {
55 return svga_texture(res)->handle;
56 }
57 }
58
59
60 /**
61 * This helper function returns TRUE if the specified resource collides with
62 * any of the resources bound to any of the currently bound sampler views.
63 */
64 boolean
65 svga_check_sampler_view_resource_collision(struct svga_context *svga,
66 struct svga_winsys_surface *res,
67 unsigned shader)
68 {
69 struct pipe_screen *screen = svga->pipe.screen;
70 unsigned i;
71
72 if (svga_screen(screen)->debug.no_surface_view) {
73 return FALSE;
74 }
75
76 for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
77 struct svga_pipe_sampler_view *sv =
78 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
79
80 if (sv && res == svga_resource_handle(sv->base.texture)) {
81 return TRUE;
82 }
83 }
84
85 return FALSE;
86 }
87
88
89 /**
90 * Create a DX ShaderResourceSamplerView for the given pipe_sampler_view,
91 * if needed.
92 */
93 enum pipe_error
94 svga_validate_pipe_sampler_view(struct svga_context *svga,
95 struct svga_pipe_sampler_view *sv)
96 {
97 enum pipe_error ret = PIPE_OK;
98
99 if (sv->id == SVGA3D_INVALID_ID) {
100 struct svga_screen *ss = svga_screen(svga->pipe.screen);
101 struct pipe_resource *texture = sv->base.texture;
102 struct svga_winsys_surface *surface = svga_resource_handle(texture);
103 SVGA3dSurfaceFormat format;
104 SVGA3dResourceType resourceDim;
105 SVGA3dShaderResourceViewDesc viewDesc;
106 enum pipe_format pformat = sv->base.format;
107
108 /* vgpu10 cannot create a BGRX view for a BGRA resource, so force it to
109 * create a BGRA view (and vice versa).
110 */
111 if (pformat == PIPE_FORMAT_B8G8R8X8_UNORM &&
112 sv->base.texture->format == PIPE_FORMAT_B8G8R8A8_UNORM) {
113 pformat = PIPE_FORMAT_B8G8R8A8_UNORM;
114 }
115 else if (pformat == PIPE_FORMAT_B8G8R8A8_UNORM &&
116 sv->base.texture->format == PIPE_FORMAT_B8G8R8X8_UNORM) {
117 pformat = PIPE_FORMAT_B8G8R8X8_UNORM;
118 }
119
120 format = svga_translate_format(ss, pformat,
121 PIPE_BIND_SAMPLER_VIEW);
122 assert(format != SVGA3D_FORMAT_INVALID);
123
124 /* Convert the format to a sampler-friendly format, if needed */
125 format = svga_sampler_format(format);
126
127 if (texture->target == PIPE_BUFFER) {
128 viewDesc.buffer.firstElement = sv->base.u.buf.first_element;
129 viewDesc.buffer.numElements = (sv->base.u.buf.last_element -
130 sv->base.u.buf.first_element + 1);
131 }
132 else {
133 viewDesc.tex.mostDetailedMip = sv->base.u.tex.first_level;
134 viewDesc.tex.firstArraySlice = sv->base.u.tex.first_layer;
135 viewDesc.tex.mipLevels = (sv->base.u.tex.last_level -
136 sv->base.u.tex.first_level + 1);
137 }
138
139 /* arraySize in viewDesc specifies the number of array slices in a
140 * texture array. For 3D texture, last_layer in
141 * pipe_sampler_view specifies the last slice of the texture
142 * which is different from the last slice in a texture array,
143 * hence we need to set arraySize to 1 explicitly.
144 */
145 viewDesc.tex.arraySize =
146 (texture->target == PIPE_TEXTURE_3D ||
147 texture->target == PIPE_BUFFER) ? 1 :
148 (sv->base.u.tex.last_layer - sv->base.u.tex.first_layer + 1);
149
150 switch (texture->target) {
151 case PIPE_BUFFER:
152 resourceDim = SVGA3D_RESOURCE_BUFFER;
153 break;
154 case PIPE_TEXTURE_1D:
155 case PIPE_TEXTURE_1D_ARRAY:
156 resourceDim = SVGA3D_RESOURCE_TEXTURE1D;
157 break;
158 case PIPE_TEXTURE_RECT:
159 case PIPE_TEXTURE_2D:
160 case PIPE_TEXTURE_2D_ARRAY:
161 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
162 break;
163 case PIPE_TEXTURE_3D:
164 resourceDim = SVGA3D_RESOURCE_TEXTURE3D;
165 break;
166 case PIPE_TEXTURE_CUBE:
167 case PIPE_TEXTURE_CUBE_ARRAY:
168 resourceDim = SVGA3D_RESOURCE_TEXTURECUBE;
169 break;
170
171 default:
172 assert(!"Unexpected texture type");
173 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
174 }
175
176 sv->id = util_bitmask_add(svga->sampler_view_id_bm);
177
178 ret = SVGA3D_vgpu10_DefineShaderResourceView(svga->swc,
179 sv->id,
180 surface,
181 format,
182 resourceDim,
183 &viewDesc);
184 if (ret != PIPE_OK) {
185 util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
186 sv->id = SVGA3D_INVALID_ID;
187 }
188 }
189
190 return ret;
191 }
192
193
194 static enum pipe_error
195 update_sampler_resources(struct svga_context *svga, unsigned dirty)
196 {
197 enum pipe_error ret = PIPE_OK;
198 unsigned shader;
199
200 if (!svga_have_vgpu10(svga))
201 return PIPE_OK;
202
203 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
204 SVGA3dShaderResourceViewId ids[PIPE_MAX_SAMPLERS];
205 struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
206 unsigned count;
207 unsigned nviews;
208 unsigned i;
209
210 count = svga->curr.num_sampler_views[shader];
211 for (i = 0; i < count; i++) {
212 struct svga_pipe_sampler_view *sv =
213 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
214 struct svga_winsys_surface *surface;
215
216 if (sv) {
217 surface = svga_resource_handle(sv->base.texture);
218
219 ret = svga_validate_pipe_sampler_view(svga, sv);
220 if (ret != PIPE_OK)
221 return ret;
222
223 assert(sv->id != SVGA3D_INVALID_ID);
224 ids[i] = sv->id;
225 }
226 else {
227 surface = NULL;
228 ids[i] = SVGA3D_INVALID_ID;
229 }
230 surfaces[i] = surface;
231 }
232
233 for (; i < ARRAY_SIZE(ids); i++) {
234 ids[i] = SVGA3D_INVALID_ID;
235 surfaces[i] = NULL;
236 }
237
238 if (shader == PIPE_SHADER_FRAGMENT) {
239 /* Handle polygon stipple sampler view */
240 if (svga->curr.rast->templ.poly_stipple_enable) {
241 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
242 struct svga_pipe_sampler_view *sv =
243 svga->polygon_stipple.sampler_view;
244
245 assert(sv);
246 if (!sv) {
247 return PIPE_OK; /* probably out of memory */
248 }
249
250 ret = svga_validate_pipe_sampler_view(svga, sv);
251 if (ret != PIPE_OK)
252 return ret;
253
254 ids[unit] = sv->id;
255 surfaces[unit] = svga_resource_handle(sv->base.texture);
256 count = MAX2(count, unit+1);
257 }
258 }
259
260 /* Number of ShaderResources that need to be modified. This includes
261 * the one that need to be unbound.
262 */
263 nviews = MAX2(svga->state.hw_draw.num_sampler_views[shader], count);
264 if (nviews > 0) {
265 ret = SVGA3D_vgpu10_SetShaderResources(svga->swc,
266 svga_shader_type(shader),
267 0, /* startView */
268 nviews,
269 ids,
270 surfaces);
271 if (ret != PIPE_OK)
272 return ret;
273 }
274
275 /* Number of sampler views enabled in the device */
276 svga->state.hw_draw.num_sampler_views[shader] = count;
277 }
278
279 return ret;
280 }
281
282
283 struct svga_tracked_state svga_hw_sampler_bindings = {
284 "shader resources emit",
285 SVGA_NEW_STIPPLE |
286 SVGA_NEW_TEXTURE_BINDING,
287 update_sampler_resources
288 };
289
290
291
292 static enum pipe_error
293 update_samplers(struct svga_context *svga, unsigned dirty )
294 {
295 enum pipe_error ret = PIPE_OK;
296 unsigned shader;
297
298 if (!svga_have_vgpu10(svga))
299 return PIPE_OK;
300
301 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
302 const unsigned count = svga->curr.num_samplers[shader];
303 SVGA3dSamplerId ids[PIPE_MAX_SAMPLERS];
304 unsigned i;
305
306 for (i = 0; i < count; i++) {
307 if (svga->curr.sampler[shader][i]) {
308 ids[i] = svga->curr.sampler[shader][i]->id;
309 assert(ids[i] != SVGA3D_INVALID_ID);
310 }
311 else {
312 ids[i] = SVGA3D_INVALID_ID;
313 }
314 }
315
316 if (count > 0) {
317 if (count != svga->state.hw_draw.num_samplers[shader] ||
318 memcmp(ids, svga->state.hw_draw.samplers[shader],
319 count * sizeof(ids[0])) != 0) {
320 /* HW state is really changing */
321 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
322 count,
323 0, /* start */
324 svga_shader_type(shader), /* type */
325 ids);
326 if (ret != PIPE_OK)
327 return ret;
328 memcpy(svga->state.hw_draw.samplers[shader], ids,
329 count * sizeof(ids[0]));
330 svga->state.hw_draw.num_samplers[shader] = count;
331 }
332 }
333 }
334
335 /* Handle polygon stipple sampler texture */
336 if (svga->curr.rast->templ.poly_stipple_enable) {
337 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
338 struct svga_sampler_state *sampler = svga->polygon_stipple.sampler;
339
340 assert(sampler);
341 if (!sampler) {
342 return PIPE_OK; /* probably out of memory */
343 }
344
345 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
346 1, /* count */
347 unit, /* start */
348 SVGA3D_SHADERTYPE_PS,
349 &sampler->id);
350 }
351
352 return ret;
353 }
354
355
356 struct svga_tracked_state svga_hw_sampler = {
357 "texture sampler emit",
358 (SVGA_NEW_SAMPLER |
359 SVGA_NEW_STIPPLE |
360 SVGA_NEW_TEXTURE_FLAGS),
361 update_samplers
362 };