953ab8f43648e22f6e5950b5ac0fb7f827594eb2
[mesa.git] / src / gallium / drivers / nouveau / nv50 / nv50_tex.c
1 /*
2 * Copyright 2008 Ben Skeggs
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "nv50/nv50_context.h"
24 #include "nv50/nv50_resource.h"
25 #include "nv50/g80_texture.xml.h"
26 #include "nv50/g80_defs.xml.h"
27
28 #include "util/u_format.h"
29
30 static inline uint32_t
31 nv50_tic_swizzle(const struct nv50_format *fmt, unsigned swz, bool tex_int)
32 {
33 switch (swz) {
34 case PIPE_SWIZZLE_X : return fmt->tic.src_x;
35 case PIPE_SWIZZLE_Y: return fmt->tic.src_y;
36 case PIPE_SWIZZLE_Z : return fmt->tic.src_z;
37 case PIPE_SWIZZLE_W: return fmt->tic.src_w;
38 case PIPE_SWIZZLE_1:
39 return tex_int ? G80_TIC_SOURCE_ONE_INT : G80_TIC_SOURCE_ONE_FLOAT;
40 case PIPE_SWIZZLE_0:
41 default:
42 return G80_TIC_SOURCE_ZERO;
43 }
44 }
45
46 struct pipe_sampler_view *
47 nv50_create_sampler_view(struct pipe_context *pipe,
48 struct pipe_resource *res,
49 const struct pipe_sampler_view *templ)
50 {
51 uint32_t flags = 0;
52
53 if (templ->target == PIPE_TEXTURE_RECT || templ->target == PIPE_BUFFER)
54 flags |= NV50_TEXVIEW_SCALED_COORDS;
55
56 return nv50_create_texture_view(pipe, res, templ, flags, templ->target);
57 }
58
59 struct pipe_sampler_view *
60 nv50_create_texture_view(struct pipe_context *pipe,
61 struct pipe_resource *texture,
62 const struct pipe_sampler_view *templ,
63 uint32_t flags,
64 enum pipe_texture_target target)
65 {
66 const uint32_t class_3d = nouveau_context(pipe)->screen->class_3d;
67 const struct util_format_description *desc;
68 const struct nv50_format *fmt;
69 uint64_t addr;
70 uint32_t *tic;
71 uint32_t swz[4];
72 uint32_t depth;
73 struct nv50_tic_entry *view;
74 struct nv50_miptree *mt = nv50_miptree(texture);
75 bool tex_int;
76
77 view = MALLOC_STRUCT(nv50_tic_entry);
78 if (!view)
79 return NULL;
80
81 view->pipe = *templ;
82 view->pipe.reference.count = 1;
83 view->pipe.texture = NULL;
84 view->pipe.context = pipe;
85
86 view->id = -1;
87
88 pipe_resource_reference(&view->pipe.texture, texture);
89
90 tic = &view->tic[0];
91
92 desc = util_format_description(view->pipe.format);
93
94 /* TIC[0] */
95
96 fmt = &nv50_format_table[view->pipe.format];
97
98 tex_int = util_format_is_pure_integer(view->pipe.format);
99
100 swz[0] = nv50_tic_swizzle(fmt, view->pipe.swizzle_r, tex_int);
101 swz[1] = nv50_tic_swizzle(fmt, view->pipe.swizzle_g, tex_int);
102 swz[2] = nv50_tic_swizzle(fmt, view->pipe.swizzle_b, tex_int);
103 swz[3] = nv50_tic_swizzle(fmt, view->pipe.swizzle_a, tex_int);
104 tic[0] = (fmt->tic.format << G80_TIC_0_COMPONENTS_SIZES__SHIFT) |
105 (fmt->tic.type_r << G80_TIC_0_R_DATA_TYPE__SHIFT) |
106 (fmt->tic.type_g << G80_TIC_0_G_DATA_TYPE__SHIFT) |
107 (fmt->tic.type_b << G80_TIC_0_B_DATA_TYPE__SHIFT) |
108 (fmt->tic.type_a << G80_TIC_0_A_DATA_TYPE__SHIFT) |
109 (swz[0] << G80_TIC_0_X_SOURCE__SHIFT) |
110 (swz[1] << G80_TIC_0_Y_SOURCE__SHIFT) |
111 (swz[2] << G80_TIC_0_Z_SOURCE__SHIFT) |
112 (swz[3] << G80_TIC_0_W_SOURCE__SHIFT);
113
114 addr = mt->base.address;
115
116 depth = MAX2(mt->base.base.array_size, mt->base.base.depth0);
117
118 if (mt->base.base.array_size > 1) {
119 /* there doesn't seem to be a base layer field in TIC */
120 addr += view->pipe.u.tex.first_layer * mt->layer_stride;
121 depth = view->pipe.u.tex.last_layer - view->pipe.u.tex.first_layer + 1;
122 }
123
124 tic[2] = 0x10001000 | G80_TIC_2_BORDER_SOURCE_COLOR;
125
126 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
127 tic[2] |= G80_TIC_2_SRGB_CONVERSION;
128
129 if (!(flags & NV50_TEXVIEW_SCALED_COORDS))
130 tic[2] |= G80_TIC_2_NORMALIZED_COORDS;
131
132 if (unlikely(!nouveau_bo_memtype(nv04_resource(texture)->bo))) {
133 if (target == PIPE_BUFFER) {
134 addr += view->pipe.u.buf.first_element * desc->block.bits / 8;
135 tic[2] |= G80_TIC_2_LAYOUT_PITCH | G80_TIC_2_TEXTURE_TYPE_ONE_D_BUFFER;
136 tic[3] = 0;
137 tic[4] = /* width */
138 view->pipe.u.buf.last_element - view->pipe.u.buf.first_element + 1;
139 tic[5] = 0;
140 } else {
141 tic[2] |= G80_TIC_2_LAYOUT_PITCH | G80_TIC_2_TEXTURE_TYPE_TWO_D_NO_MIPMAP;
142 tic[3] = mt->level[0].pitch;
143 tic[4] = mt->base.base.width0;
144 tic[5] = (1 << 16) | (mt->base.base.height0);
145 }
146 tic[6] =
147 tic[7] = 0;
148 tic[1] = addr;
149 tic[2] |= addr >> 32;
150 return &view->pipe;
151 }
152
153 tic[1] = addr;
154 tic[2] |= (addr >> 32) & 0xff;
155
156 tic[2] |=
157 ((mt->level[0].tile_mode & 0x0f0) << (22 - 4)) |
158 ((mt->level[0].tile_mode & 0xf00) << (25 - 8));
159
160 switch (target) {
161 case PIPE_TEXTURE_1D:
162 tic[2] |= G80_TIC_2_TEXTURE_TYPE_ONE_D;
163 break;
164 case PIPE_TEXTURE_2D:
165 tic[2] |= G80_TIC_2_TEXTURE_TYPE_TWO_D;
166 break;
167 case PIPE_TEXTURE_RECT:
168 tic[2] |= G80_TIC_2_TEXTURE_TYPE_TWO_D_NO_MIPMAP;
169 break;
170 case PIPE_TEXTURE_3D:
171 tic[2] |= G80_TIC_2_TEXTURE_TYPE_THREE_D;
172 break;
173 case PIPE_TEXTURE_CUBE:
174 depth /= 6;
175 tic[2] |= G80_TIC_2_TEXTURE_TYPE_CUBEMAP;
176 break;
177 case PIPE_TEXTURE_1D_ARRAY:
178 tic[2] |= G80_TIC_2_TEXTURE_TYPE_ONE_D_ARRAY;
179 break;
180 case PIPE_TEXTURE_2D_ARRAY:
181 tic[2] |= G80_TIC_2_TEXTURE_TYPE_TWO_D_ARRAY;
182 break;
183 case PIPE_TEXTURE_CUBE_ARRAY:
184 depth /= 6;
185 tic[2] |= G80_TIC_2_TEXTURE_TYPE_CUBE_ARRAY;
186 break;
187 case PIPE_BUFFER:
188 assert(0); /* should be linear and handled above ! */
189 tic[2] |= G80_TIC_2_TEXTURE_TYPE_ONE_D_BUFFER | G80_TIC_2_LAYOUT_PITCH;
190 break;
191 default:
192 unreachable("unexpected/invalid texture target");
193 }
194
195 tic[3] = (flags & NV50_TEXVIEW_FILTER_MSAA8) ? 0x20000000 : 0x00300000;
196
197 tic[4] = (1 << 31) | (mt->base.base.width0 << mt->ms_x);
198
199 tic[5] = (mt->base.base.height0 << mt->ms_y) & 0xffff;
200 tic[5] |= depth << 16;
201 if (class_3d > NV50_3D_CLASS)
202 tic[5] |= mt->base.base.last_level << G80_TIC_5_MAP_MIP_LEVEL__SHIFT;
203 else
204 tic[5] |= view->pipe.u.tex.last_level << G80_TIC_5_MAP_MIP_LEVEL__SHIFT;
205
206 tic[6] = (mt->ms_x > 1) ? 0x88000000 : 0x03000000; /* sampling points */
207
208 if (class_3d > NV50_3D_CLASS)
209 tic[7] = (view->pipe.u.tex.last_level << 4) | view->pipe.u.tex.first_level;
210 else
211 tic[7] = 0;
212
213 if (unlikely(!(tic[2] & G80_TIC_2_NORMALIZED_COORDS)))
214 if (mt->base.base.last_level)
215 tic[5] &= ~G80_TIC_5_MAP_MIP_LEVEL__MASK;
216
217 return &view->pipe;
218 }
219
220 static void
221 nv50_update_tic(struct nv50_context *nv50, struct nv50_tic_entry *tic,
222 struct nv04_resource *res)
223 {
224 uint64_t address = res->address;
225 if (res->base.target != PIPE_BUFFER)
226 return;
227 address += tic->pipe.u.buf.first_element *
228 util_format_get_blocksize(tic->pipe.format);
229 if (tic->tic[1] == (uint32_t)address &&
230 (tic->tic[2] & 0xff) == address >> 32)
231 return;
232
233 nv50_screen_tic_unlock(nv50->screen, tic);
234 tic->id = -1;
235 tic->tic[1] = address;
236 tic->tic[2] &= 0xffffff00;
237 tic->tic[2] |= address >> 32;
238 }
239
240 static bool
241 nv50_validate_tic(struct nv50_context *nv50, int s)
242 {
243 struct nouveau_pushbuf *push = nv50->base.pushbuf;
244 struct nouveau_bo *txc = nv50->screen->txc;
245 unsigned i;
246 bool need_flush = false;
247
248 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
249 for (i = 0; i < nv50->num_textures[s]; ++i) {
250 struct nv50_tic_entry *tic = nv50_tic_entry(nv50->textures[s][i]);
251 struct nv04_resource *res;
252
253 if (!tic) {
254 BEGIN_NV04(push, NV50_3D(BIND_TIC(s)), 1);
255 PUSH_DATA (push, (i << 1) | 0);
256 continue;
257 }
258 res = &nv50_miptree(tic->pipe.texture)->base;
259 nv50_update_tic(nv50, tic, res);
260
261 if (tic->id < 0) {
262 tic->id = nv50_screen_tic_alloc(nv50->screen, tic);
263
264 BEGIN_NV04(push, NV50_2D(DST_FORMAT), 2);
265 PUSH_DATA (push, G80_SURFACE_FORMAT_R8_UNORM);
266 PUSH_DATA (push, 1);
267 BEGIN_NV04(push, NV50_2D(DST_PITCH), 5);
268 PUSH_DATA (push, 262144);
269 PUSH_DATA (push, 65536);
270 PUSH_DATA (push, 1);
271 PUSH_DATAh(push, txc->offset);
272 PUSH_DATA (push, txc->offset);
273 BEGIN_NV04(push, NV50_2D(SIFC_BITMAP_ENABLE), 2);
274 PUSH_DATA (push, 0);
275 PUSH_DATA (push, G80_SURFACE_FORMAT_R8_UNORM);
276 BEGIN_NV04(push, NV50_2D(SIFC_WIDTH), 10);
277 PUSH_DATA (push, 32);
278 PUSH_DATA (push, 1);
279 PUSH_DATA (push, 0);
280 PUSH_DATA (push, 1);
281 PUSH_DATA (push, 0);
282 PUSH_DATA (push, 1);
283 PUSH_DATA (push, 0);
284 PUSH_DATA (push, tic->id * 32);
285 PUSH_DATA (push, 0);
286 PUSH_DATA (push, 0);
287 BEGIN_NI04(push, NV50_2D(SIFC_DATA), 8);
288 PUSH_DATAp(push, &tic->tic[0], 8);
289
290 need_flush = true;
291 } else
292 if (res->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
293 BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
294 PUSH_DATA (push, 0x20);
295 }
296
297 nv50->screen->tic.lock[tic->id / 32] |= 1 << (tic->id % 32);
298
299 res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING;
300 res->status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
301
302 BCTX_REFN(nv50->bufctx_3d, 3D_TEXTURES, res, RD);
303
304 BEGIN_NV04(push, NV50_3D(BIND_TIC(s)), 1);
305 PUSH_DATA (push, (tic->id << 9) | (i << 1) | 1);
306 }
307 for (; i < nv50->state.num_textures[s]; ++i) {
308 BEGIN_NV04(push, NV50_3D(BIND_TIC(s)), 1);
309 PUSH_DATA (push, (i << 1) | 0);
310 }
311 if (nv50->num_textures[s]) {
312 BEGIN_NV04(push, NV50_3D(CB_ADDR), 1);
313 PUSH_DATA (push, ((NV50_CB_AUX_TEX_MS_OFFSET + 16 * s * 2 * 4) << (8 - 2)) | NV50_CB_AUX);
314 BEGIN_NI04(push, NV50_3D(CB_DATA(0)), nv50->num_textures[s] * 2);
315 for (i = 0; i < nv50->num_textures[s]; i++) {
316 struct nv50_tic_entry *tic = nv50_tic_entry(nv50->textures[s][i]);
317 struct nv50_miptree *res;
318
319 if (!tic) {
320 PUSH_DATA (push, 0);
321 PUSH_DATA (push, 0);
322 continue;
323 }
324 res = nv50_miptree(tic->pipe.texture);
325 PUSH_DATA (push, res->ms_x);
326 PUSH_DATA (push, res->ms_y);
327 }
328 }
329 nv50->state.num_textures[s] = nv50->num_textures[s];
330
331 return need_flush;
332 }
333
334 void nv50_validate_textures(struct nv50_context *nv50)
335 {
336 bool need_flush;
337
338 need_flush = nv50_validate_tic(nv50, 0);
339 need_flush |= nv50_validate_tic(nv50, 1);
340 need_flush |= nv50_validate_tic(nv50, 2);
341
342 if (need_flush) {
343 BEGIN_NV04(nv50->base.pushbuf, NV50_3D(TIC_FLUSH), 1);
344 PUSH_DATA (nv50->base.pushbuf, 0);
345 }
346 }
347
348 static bool
349 nv50_validate_tsc(struct nv50_context *nv50, int s)
350 {
351 struct nouveau_pushbuf *push = nv50->base.pushbuf;
352 unsigned i;
353 bool need_flush = false;
354
355 assert(nv50->num_samplers[s] <= PIPE_MAX_SAMPLERS);
356 for (i = 0; i < nv50->num_samplers[s]; ++i) {
357 struct nv50_tsc_entry *tsc = nv50_tsc_entry(nv50->samplers[s][i]);
358
359 if (!tsc) {
360 BEGIN_NV04(push, NV50_3D(BIND_TSC(s)), 1);
361 PUSH_DATA (push, (i << 4) | 0);
362 continue;
363 }
364 nv50->seamless_cube_map = tsc->seamless_cube_map;
365 if (tsc->id < 0) {
366 tsc->id = nv50_screen_tsc_alloc(nv50->screen, tsc);
367
368 nv50_sifc_linear_u8(&nv50->base, nv50->screen->txc,
369 65536 + tsc->id * 32,
370 NOUVEAU_BO_VRAM, 32, tsc->tsc);
371 need_flush = true;
372 }
373 nv50->screen->tsc.lock[tsc->id / 32] |= 1 << (tsc->id % 32);
374
375 BEGIN_NV04(push, NV50_3D(BIND_TSC(s)), 1);
376 PUSH_DATA (push, (tsc->id << 12) | (i << 4) | 1);
377 }
378 for (; i < nv50->state.num_samplers[s]; ++i) {
379 BEGIN_NV04(push, NV50_3D(BIND_TSC(s)), 1);
380 PUSH_DATA (push, (i << 4) | 0);
381 }
382 nv50->state.num_samplers[s] = nv50->num_samplers[s];
383
384 return need_flush;
385 }
386
387 void nv50_validate_samplers(struct nv50_context *nv50)
388 {
389 bool need_flush;
390
391 need_flush = nv50_validate_tsc(nv50, 0);
392 need_flush |= nv50_validate_tsc(nv50, 1);
393 need_flush |= nv50_validate_tsc(nv50, 2);
394
395 if (need_flush) {
396 BEGIN_NV04(nv50->base.pushbuf, NV50_3D(TSC_FLUSH), 1);
397 PUSH_DATA (nv50->base.pushbuf, 0);
398 }
399 }
400
401 /* There can be up to 4 different MS levels (1, 2, 4, 8). To simplify the
402 * shader logic, allow each one to take up 8 offsets.
403 */
404 #define COMBINE(x, y) x, y
405 #define DUMMY 0, 0
406 static const uint32_t msaa_sample_xy_offsets[] = {
407 /* MS1 */
408 COMBINE(0, 0),
409 DUMMY,
410 DUMMY,
411 DUMMY,
412 DUMMY,
413 DUMMY,
414 DUMMY,
415 DUMMY,
416
417 /* MS2 */
418 COMBINE(0, 0),
419 COMBINE(1, 0),
420 DUMMY,
421 DUMMY,
422 DUMMY,
423 DUMMY,
424 DUMMY,
425 DUMMY,
426
427 /* MS4 */
428 COMBINE(0, 0),
429 COMBINE(1, 0),
430 COMBINE(0, 1),
431 COMBINE(1, 1),
432 DUMMY,
433 DUMMY,
434 DUMMY,
435 DUMMY,
436
437 /* MS8 */
438 COMBINE(0, 0),
439 COMBINE(1, 0),
440 COMBINE(0, 1),
441 COMBINE(1, 1),
442 COMBINE(2, 0),
443 COMBINE(3, 0),
444 COMBINE(2, 1),
445 COMBINE(3, 1),
446 };
447
448 void nv50_upload_ms_info(struct nouveau_pushbuf *push)
449 {
450 BEGIN_NV04(push, NV50_3D(CB_ADDR), 1);
451 PUSH_DATA (push, (NV50_CB_AUX_MS_OFFSET << (8 - 2)) | NV50_CB_AUX);
452 BEGIN_NI04(push, NV50_3D(CB_DATA(0)), ARRAY_SIZE(msaa_sample_xy_offsets));
453 PUSH_DATAp(push, msaa_sample_xy_offsets, ARRAY_SIZE(msaa_sample_xy_offsets));
454 }