st/nine: Refactor nine_d3d9_to_pipe_format_map
[mesa.git] / src / gallium / state_trackers / nine / basetexture9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "basetexture9.h"
24 #include "device9.h"
25
26 /* For UploadSelf: */
27 #include "texture9.h"
28 #include "cubetexture9.h"
29 #include "volumetexture9.h"
30
31 #ifdef DEBUG
32 #include "nine_pipe.h"
33 #include "nine_dump.h"
34 #endif
35
36 #include "util/u_format.h"
37 #include "util/u_gen_mipmap.h"
38
39 #define DBG_CHANNEL DBG_BASETEXTURE
40
41 HRESULT
42 NineBaseTexture9_ctor( struct NineBaseTexture9 *This,
43 struct NineUnknownParams *pParams,
44 struct pipe_resource *initResource,
45 D3DRESOURCETYPE Type,
46 D3DFORMAT format,
47 D3DPOOL Pool,
48 DWORD Usage)
49 {
50 BOOL alloc = (Pool == D3DPOOL_DEFAULT) && !initResource &&
51 (format != D3DFMT_NULL);
52 HRESULT hr;
53
54 DBG("This=%p, pParams=%p initResource=%p Type=%d format=%d Pool=%d Usage=%d\n",
55 This, pParams, initResource, Type, format, Pool, Usage);
56
57 user_assert(!(Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) ||
58 Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
59 user_assert(!(Usage & D3DUSAGE_DYNAMIC) ||
60 Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
61
62 hr = NineResource9_ctor(&This->base, pParams, initResource, alloc, Type, Pool, Usage);
63 if (FAILED(hr))
64 return hr;
65
66 This->format = format;
67 This->pipe = pParams->device->pipe;
68 This->mipfilter = (Usage & D3DUSAGE_AUTOGENMIPMAP) ?
69 D3DTEXF_LINEAR : D3DTEXF_NONE;
70 This->lod = 0;
71 This->lod_resident = -1;
72 This->shadow = This->format != D3DFMT_INTZ && util_format_has_depth(
73 util_format_description(This->base.info.format));
74
75 list_inithead(&This->list);
76
77 return D3D_OK;
78 }
79
80 void
81 NineBaseTexture9_dtor( struct NineBaseTexture9 *This )
82 {
83 DBG("This=%p\n", This);
84
85 pipe_sampler_view_reference(&This->view[0], NULL);
86 pipe_sampler_view_reference(&This->view[1], NULL);
87
88 if (This->list.prev != NULL && This->list.next != NULL)
89 list_del(&This->list),
90
91 NineResource9_dtor(&This->base);
92 }
93
94 DWORD WINAPI
95 NineBaseTexture9_SetLOD( struct NineBaseTexture9 *This,
96 DWORD LODNew )
97 {
98 DWORD old = This->lod;
99
100 DBG("This=%p LODNew=%d\n", This, LODNew);
101
102 user_assert(This->base.pool == D3DPOOL_MANAGED, 0);
103
104 This->lod = MIN2(LODNew, This->base.info.last_level);
105
106 if (This->lod != old && This->bind_count && LIST_IS_EMPTY(&This->list))
107 list_add(&This->list, &This->base.base.device->update_textures);
108
109 return old;
110 }
111
112 DWORD WINAPI
113 NineBaseTexture9_GetLOD( struct NineBaseTexture9 *This )
114 {
115 DBG("This=%p\n", This);
116
117 return This->lod;
118 }
119
120 DWORD WINAPI
121 NineBaseTexture9_GetLevelCount( struct NineBaseTexture9 *This )
122 {
123 DBG("This=%p\n", This);
124
125 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
126 return 1;
127 return This->base.info.last_level + 1;
128 }
129
130 HRESULT WINAPI
131 NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9 *This,
132 D3DTEXTUREFILTERTYPE FilterType )
133 {
134 DBG("This=%p FilterType=%d\n", This, FilterType);
135
136 if (!(This->base.usage & D3DUSAGE_AUTOGENMIPMAP))
137 return D3D_OK;
138 user_assert(FilterType != D3DTEXF_NONE, D3DERR_INVALIDCALL);
139
140 This->mipfilter = FilterType;
141
142 return D3D_OK;
143 }
144
145 D3DTEXTUREFILTERTYPE WINAPI
146 NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9 *This )
147 {
148 DBG("This=%p\n", This);
149
150 return This->mipfilter;
151 }
152
153 HRESULT
154 NineBaseTexture9_UploadSelf( struct NineBaseTexture9 *This )
155 {
156 HRESULT hr;
157 unsigned last_level = This->base.info.last_level;
158 unsigned l;
159
160 DBG("This=%p dirty=%i type=%s\n", This, This->dirty,
161 nine_D3DRTYPE_to_str(This->base.type));
162
163 assert(This->base.pool == D3DPOOL_MANAGED);
164
165 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
166 last_level = 0; /* TODO: What if level 0 is not resident ? */
167
168 if (This->lod_resident != This->lod) {
169 struct pipe_resource *res;
170
171 DBG("updating LOD from %u to %u ...\n", This->lod_resident, This->lod);
172
173 pipe_sampler_view_reference(&This->view[0], NULL);
174 pipe_sampler_view_reference(&This->view[1], NULL);
175
176 if (This->bind_count) {
177 /* mark state dirty */
178 struct nine_state *state = &This->base.base.device->state;
179 unsigned s;
180 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
181 if (state->texture[s] == This)
182 state->changed.texture |= 1 << s;
183 if (state->changed.texture)
184 state->changed.group |= NINE_STATE_TEXTURE;
185 }
186
187 hr = NineBaseTexture9_CreatePipeResource(This, This->lod_resident != -1);
188 if (FAILED(hr))
189 return hr;
190 res = This->base.resource;
191
192 if (This->lod_resident == -1) /* no levels were resident */
193 This->lod_resident = This->base.info.last_level + 1;
194
195 if (This->base.type == D3DRTYPE_TEXTURE) {
196 struct NineTexture9 *tex = NineTexture9(This);
197 struct pipe_box box;
198
199 /* Mark uninitialized levels as dirty. */
200 box.x = box.y = box.z = 0;
201 box.depth = 1;
202 for (l = This->lod; l < This->lod_resident; ++l) {
203 box.width = u_minify(This->base.info.width0, l);
204 box.height = u_minify(This->base.info.height0, l);
205 NineSurface9_AddDirtyRect(tex->surfaces[l], &box);
206 }
207 for (l = 0; l < This->lod; ++l)
208 NineSurface9_SetResource(tex->surfaces[l], NULL, -1);
209 for (; l <= This->base.info.last_level; ++l)
210 NineSurface9_SetResource(tex->surfaces[l], res, l - This->lod);
211 } else
212 if (This->base.type == D3DRTYPE_CUBETEXTURE) {
213 struct NineCubeTexture9 *tex = NineCubeTexture9(This);
214 struct pipe_box box;
215 unsigned z;
216
217 /* Mark uninitialized levels as dirty. */
218 box.x = box.y = box.z = 0;
219 box.depth = 1;
220 for (l = This->lod; l < This->lod_resident; ++l) {
221 box.width = u_minify(This->base.info.width0, l);
222 box.height = u_minify(This->base.info.height0, l);
223 for (z = 0; z < 6; ++z)
224 NineSurface9_AddDirtyRect(tex->surfaces[l * 6 + z], &box);
225 }
226 for (l = 0; l < This->lod; ++l) {
227 for (z = 0; z < 6; ++z)
228 NineSurface9_SetResource(tex->surfaces[l * 6 + z],
229 NULL, -1);
230 }
231 for (; l <= This->base.info.last_level; ++l) {
232 for (z = 0; z < 6; ++z)
233 NineSurface9_SetResource(tex->surfaces[l * 6 + z],
234 res, l - This->lod);
235 }
236 } else
237 if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
238 struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
239 struct pipe_box box;
240
241 /* Mark uninitialized levels as dirty. */
242 box.x = box.y = box.z = 0;
243 for (l = This->lod; l < This->lod_resident; ++l) {
244 box.width = u_minify(This->base.info.width0, l);
245 box.height = u_minify(This->base.info.height0, l);
246 box.depth = u_minify(This->base.info.depth0, l);
247 NineVolume9_AddDirtyRegion(tex->volumes[l], &box);
248 }
249 for (l = 0; l < This->lod; ++l)
250 NineVolume9_SetResource(tex->volumes[l], NULL, -1);
251 for (; l <= This->base.info.last_level; ++l)
252 NineVolume9_SetResource(tex->volumes[l], res, l - This->lod);
253 } else {
254 assert(!"invalid texture type");
255 }
256
257 if (This->lod < This->lod_resident)
258 This->dirty = TRUE;
259 This->lod_resident = This->lod;
260 }
261 if (!This->dirty)
262 return D3D_OK;
263
264 if (This->base.type == D3DRTYPE_TEXTURE) {
265 struct NineTexture9 *tex = NineTexture9(This);
266 struct pipe_box box;
267 box.z = 0;
268 box.depth = 1;
269
270 DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
271 tex->dirty_rect.x, tex->dirty_rect.y,
272 tex->dirty_rect.width, tex->dirty_rect.height);
273
274 if (tex->dirty_rect.width) {
275 for (l = 0; l <= last_level; ++l) {
276 u_box_minify_2d(&box, &tex->dirty_rect, l);
277 NineSurface9_AddDirtyRect(tex->surfaces[l], &box);
278 }
279 memset(&tex->dirty_rect, 0, sizeof(tex->dirty_rect));
280 tex->dirty_rect.depth = 1;
281 }
282 for (l = This->lod; l <= last_level; ++l)
283 NineSurface9_UploadSelf(tex->surfaces[l]);
284 } else
285 if (This->base.type == D3DRTYPE_CUBETEXTURE) {
286 struct NineCubeTexture9 *tex = NineCubeTexture9(This);
287 unsigned z;
288 struct pipe_box box;
289 box.z = 0;
290 box.depth = 1;
291
292 for (z = 0; z < 6; ++z) {
293 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z,
294 tex->dirty_rect[z].x, tex->dirty_rect[z].y,
295 tex->dirty_rect[z].width, tex->dirty_rect[z].height);
296
297 if (tex->dirty_rect[z].width) {
298 for (l = 0; l <= last_level; ++l) {
299 u_box_minify_2d(&box, &tex->dirty_rect[z], l);
300 NineSurface9_AddDirtyRect(tex->surfaces[l * 6 + z], &box);
301 }
302 memset(&tex->dirty_rect[z], 0, sizeof(tex->dirty_rect[z]));
303 tex->dirty_rect[z].depth = 1;
304 }
305 for (l = This->lod; l <= last_level; ++l)
306 NineSurface9_UploadSelf(tex->surfaces[l * 6 + z]);
307 }
308 } else
309 if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
310 struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
311 struct pipe_box box;
312
313 DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
314 tex->dirty_box.x, tex->dirty_box.y, tex->dirty_box.y,
315 tex->dirty_box.width, tex->dirty_box.height, tex->dirty_box.depth);
316
317 if (tex->dirty_box.width) {
318 for (l = 0; l <= last_level; ++l) {
319 u_box_minify_2d(&box, &tex->dirty_box, l);
320 NineVolume9_AddDirtyRegion(tex->volumes[l], &tex->dirty_box);
321 }
322 memset(&tex->dirty_box, 0, sizeof(tex->dirty_box));
323 }
324 for (l = This->lod; l <= last_level; ++l)
325 NineVolume9_UploadSelf(tex->volumes[l]);
326 } else {
327 assert(!"invalid texture type");
328 }
329 This->dirty = FALSE;
330
331 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
332 This->dirty_mip = TRUE;
333 /* TODO: if dirty only because of lod change, only generate added levels */
334
335 DBG("DONE, generate mip maps = %i\n", This->dirty_mip);
336 return D3D_OK;
337 }
338
339 void WINAPI
340 NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This )
341 {
342 struct pipe_resource *resource = This->base.resource;
343
344 unsigned base_level = 0;
345 unsigned last_level = This->base.info.last_level - This->lod;
346 unsigned first_layer = 0;
347 unsigned last_layer;
348 unsigned filter = This->mipfilter == D3DTEXF_POINT ? PIPE_TEX_FILTER_NEAREST
349 : PIPE_TEX_FILTER_LINEAR;
350 DBG("This=%p\n", This);
351
352 if (This->base.pool == D3DPOOL_MANAGED)
353 NineBaseTexture9_UploadSelf(This);
354 if (!This->dirty_mip)
355 return;
356 if (This->lod) {
357 ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
358 return;
359 }
360
361 if (!This->view[0])
362 NineBaseTexture9_UpdateSamplerView(This, 0);
363
364 last_layer = util_max_layer(This->view[0]->texture, base_level);
365
366 util_gen_mipmap(This->pipe, resource,
367 resource->format, base_level, last_level,
368 first_layer, last_layer, filter);
369
370 This->dirty_mip = FALSE;
371
372 NineDevice9_RestoreNonCSOState(This->base.base.device, ~0x3);
373 }
374
375 HRESULT
376 NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
377 BOOL CopyData )
378 {
379 struct pipe_context *pipe = This->pipe;
380 struct pipe_screen *screen = This->base.info.screen;
381 struct pipe_resource templ;
382 unsigned l, m;
383 struct pipe_resource *res;
384 struct pipe_resource *old = This->base.resource;
385
386 DBG("This=%p lod=%u last_level=%u\n", This,
387 This->lod, This->base.info.last_level);
388
389 assert(This->base.pool == D3DPOOL_MANAGED);
390
391 templ = This->base.info;
392
393 if (This->lod) {
394 templ.width0 = u_minify(templ.width0, This->lod);
395 templ.height0 = u_minify(templ.height0, This->lod);
396 templ.depth0 = u_minify(templ.depth0, This->lod);
397 }
398 templ.last_level = This->base.info.last_level - This->lod;
399
400 if (old) {
401 /* LOD might have changed. */
402 if (old->width0 == templ.width0 &&
403 old->height0 == templ.height0 &&
404 old->depth0 == templ.depth0)
405 return D3D_OK;
406 }
407
408 res = screen->resource_create(screen, &templ);
409 if (!res)
410 return D3DERR_OUTOFVIDEOMEMORY;
411 This->base.resource = res;
412
413 if (old && CopyData) { /* Don't return without releasing old ! */
414 struct pipe_box box;
415 box.x = 0;
416 box.y = 0;
417 box.z = 0;
418
419 l = (This->lod < This->lod_resident) ? This->lod_resident - This->lod : 0;
420 m = (This->lod < This->lod_resident) ? 0 : This->lod - This->lod_resident;
421
422 box.width = u_minify(templ.width0, l);
423 box.height = u_minify(templ.height0, l);
424 box.depth = u_minify(templ.depth0, l);
425
426 for (; l <= templ.last_level; ++l, ++m) {
427 pipe->resource_copy_region(pipe,
428 res, l, 0, 0, 0,
429 old, m, &box);
430 box.width = u_minify(box.width, 1);
431 box.height = u_minify(box.height, 1);
432 box.depth = u_minify(box.depth, 1);
433 }
434 }
435 pipe_resource_reference(&old, NULL);
436
437 return D3D_OK;
438 }
439
440 #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
441 s == UTIL_FORMAT_SWIZZLE_1 || \
442 s == UTIL_FORMAT_SWIZZLE_NONE)
443
444 HRESULT
445 NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
446 const int sRGB )
447 {
448 const struct util_format_description *desc;
449 struct pipe_context *pipe = This->pipe;
450 struct pipe_screen *screen = pipe->screen;
451 struct pipe_resource *resource = This->base.resource;
452 struct pipe_sampler_view templ;
453 enum pipe_format srgb_format;
454 unsigned i;
455 uint8_t swizzle[4];
456
457 DBG("This=%p sRGB=%d\n", This, sRGB);
458
459 if (unlikely(!resource)) {
460 if (unlikely(This->format == D3DFMT_NULL))
461 return D3D_OK;
462 NineBaseTexture9_Dump(This);
463 /* hack due to incorrect POOL_MANAGED handling */
464 NineBaseTexture9_GenerateMipSubLevels(This);
465 resource = This->base.resource;
466 }
467 assert(resource);
468
469 pipe_sampler_view_reference(&This->view[sRGB], NULL);
470
471 swizzle[0] = PIPE_SWIZZLE_RED;
472 swizzle[1] = PIPE_SWIZZLE_GREEN;
473 swizzle[2] = PIPE_SWIZZLE_BLUE;
474 swizzle[3] = PIPE_SWIZZLE_ALPHA;
475 desc = util_format_description(resource->format);
476 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
477 /* msdn doc is incomplete here and wrong.
478 * The only formats that can be read directly here
479 * are DF16, DF24 and INTZ.
480 * Tested on win the swizzle is
481 * R = depth, G = B = 0, A = 1 for DF16 and DF24
482 * R = G = B = A = depth for INTZ
483 * For the other ZS formats that can't be read directly
484 * but can be used as shadow map, the result is duplicated on
485 * all channel */
486 if (This->format == D3DFMT_DF16 ||
487 This->format == D3DFMT_DF24) {
488 swizzle[1] = PIPE_SWIZZLE_ZERO;
489 swizzle[2] = PIPE_SWIZZLE_ZERO;
490 swizzle[3] = PIPE_SWIZZLE_ONE;
491 } else {
492 swizzle[1] = PIPE_SWIZZLE_RED;
493 swizzle[2] = PIPE_SWIZZLE_RED;
494 swizzle[3] = PIPE_SWIZZLE_RED;
495 }
496 } else if (resource->format != PIPE_FORMAT_A8_UNORM &&
497 resource->format != PIPE_FORMAT_RGTC1_UNORM) {
498 /* exceptions:
499 * A8 should have 0.0 as default values for RGB.
500 * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
501 * It is already what gallium does. All the other ones
502 * should have 1.0 for non-defined values */
503 for (i = 0; i < 4; i++) {
504 if (SWIZZLE_TO_REPLACE(desc->swizzle[i]))
505 swizzle[i] = PIPE_SWIZZLE_ONE;
506 }
507 }
508
509 /* if requested and supported, convert to the sRGB format */
510 srgb_format = util_format_srgb(resource->format);
511 if (sRGB && srgb_format != PIPE_FORMAT_NONE &&
512 screen->is_format_supported(screen, srgb_format,
513 resource->target, 0, resource->bind))
514 templ.format = srgb_format;
515 else
516 templ.format = resource->format;
517 templ.u.tex.first_layer = 0;
518 templ.u.tex.last_layer = resource->target == PIPE_TEXTURE_3D ?
519 resource->depth0 - 1 : resource->array_size - 1;
520 templ.u.tex.first_level = 0;
521 templ.u.tex.last_level = resource->last_level;
522 templ.swizzle_r = swizzle[0];
523 templ.swizzle_g = swizzle[1];
524 templ.swizzle_b = swizzle[2];
525 templ.swizzle_a = swizzle[3];
526 templ.target = resource->target;
527
528 This->view[sRGB] = pipe->create_sampler_view(pipe, resource, &templ);
529
530 DBG("sampler view = %p(resource = %p)\n", This->view[sRGB], resource);
531
532 return This->view ? D3D_OK : D3DERR_DRIVERINTERNALERROR;
533 }
534
535 void WINAPI
536 NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This )
537 {
538 DBG("This=%p\n", This);
539
540 if (This->dirty && This->base.pool == D3DPOOL_MANAGED)
541 NineBaseTexture9_UploadSelf(This);
542 }
543
544 #ifdef DEBUG
545 void
546 NineBaseTexture9_Dump( struct NineBaseTexture9 *This )
547 {
548 DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
549 "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This,
550 This->base.resource,
551 nine_D3DPOOL_to_str(This->base.pool),
552 nine_D3DRTYPE_to_str(This->base.type),
553 nine_D3DUSAGE_to_str(This->base.usage),
554 d3dformat_to_string(This->format),
555 This->base.info.width0, This->base.info.height0, This->base.info.depth0,
556 This->base.info.array_size, This->base.info.last_level,
557 This->lod, This->lod_resident);
558 }
559 #endif /* DEBUG */