2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
23 #include "basetexture9.h"
28 #include "cubetexture9.h"
29 #include "volumetexture9.h"
32 #include "nine_pipe.h"
33 #include "nine_dump.h"
36 #include "util/u_format.h"
37 #include "util/u_gen_mipmap.h"
39 #define DBG_CHANNEL DBG_BASETEXTURE
42 NineBaseTexture9_ctor( struct NineBaseTexture9
*This
,
43 struct NineUnknownParams
*pParams
,
44 struct pipe_resource
*initResource
,
50 BOOL alloc
= (Pool
== D3DPOOL_DEFAULT
) && !initResource
&&
51 (format
!= D3DFMT_NULL
);
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
);
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
);
62 hr
= NineResource9_ctor(&This
->base
, pParams
, initResource
, alloc
, Type
, Pool
, Usage
);
66 This
->format
= format
;
67 This
->pipe
= pParams
->device
->pipe
;
68 This
->mipfilter
= (Usage
& D3DUSAGE_AUTOGENMIPMAP
) ?
69 D3DTEXF_LINEAR
: D3DTEXF_NONE
;
71 This
->lod_resident
= -1;
72 /* When a depth buffer is sampled, it is for shadow mapping, except for
73 * D3DFMT_INTZ, D3DFMT_DF16 and D3DFMT_DF24.
74 * In addition D3DFMT_INTZ can be used for both texturing and depth buffering
75 * if z write is disabled. This particular feature may not work for us in
76 * practice because OGL doesn't have that. However apparently it is known
77 * some cards have performance issues with this feature, so real apps
78 * shouldn't use it. */
79 This
->shadow
= (This
->format
!= D3DFMT_INTZ
&& This
->format
!= D3DFMT_DF16
&&
80 This
->format
!= D3DFMT_DF24
) &&
81 util_format_has_depth(util_format_description(This
->base
.info
.format
));
83 list_inithead(&This
->list
);
89 NineBaseTexture9_dtor( struct NineBaseTexture9
*This
)
91 DBG("This=%p\n", This
);
93 pipe_sampler_view_reference(&This
->view
[0], NULL
);
94 pipe_sampler_view_reference(&This
->view
[1], NULL
);
96 if (This
->list
.prev
!= NULL
&& This
->list
.next
!= NULL
)
97 list_del(&This
->list
),
99 NineResource9_dtor(&This
->base
);
103 NineBaseTexture9_SetLOD( struct NineBaseTexture9
*This
,
106 DWORD old
= This
->lod
;
108 DBG("This=%p LODNew=%d\n", This
, LODNew
);
110 user_assert(This
->base
.pool
== D3DPOOL_MANAGED
, 0);
112 This
->lod
= MIN2(LODNew
, This
->base
.info
.last_level
);
114 if (This
->lod
!= old
&& This
->bind_count
&& LIST_IS_EMPTY(&This
->list
))
115 list_add(&This
->list
, &This
->base
.base
.device
->update_textures
);
121 NineBaseTexture9_GetLOD( struct NineBaseTexture9
*This
)
123 DBG("This=%p\n", This
);
129 NineBaseTexture9_GetLevelCount( struct NineBaseTexture9
*This
)
131 DBG("This=%p\n", This
);
133 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
135 return This
->base
.info
.last_level
+ 1;
139 NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9
*This
,
140 D3DTEXTUREFILTERTYPE FilterType
)
142 DBG("This=%p FilterType=%d\n", This
, FilterType
);
144 if (!(This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
))
146 user_assert(FilterType
!= D3DTEXF_NONE
, D3DERR_INVALIDCALL
);
148 This
->mipfilter
= FilterType
;
153 D3DTEXTUREFILTERTYPE WINAPI
154 NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9
*This
)
156 DBG("This=%p\n", This
);
158 return This
->mipfilter
;
162 NineBaseTexture9_UploadSelf( struct NineBaseTexture9
*This
)
165 unsigned last_level
= This
->base
.info
.last_level
;
168 DBG("This=%p dirty=%i type=%s\n", This
, This
->dirty
,
169 nine_D3DRTYPE_to_str(This
->base
.type
));
171 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
173 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
174 last_level
= 0; /* TODO: What if level 0 is not resident ? */
176 if (This
->lod_resident
!= This
->lod
) {
177 struct pipe_resource
*res
;
179 DBG("updating LOD from %u to %u ...\n", This
->lod_resident
, This
->lod
);
181 pipe_sampler_view_reference(&This
->view
[0], NULL
);
182 pipe_sampler_view_reference(&This
->view
[1], NULL
);
184 if (This
->bind_count
) {
185 /* mark state dirty */
186 struct nine_state
*state
= &This
->base
.base
.device
->state
;
188 for (s
= 0; s
< NINE_MAX_SAMPLERS
; ++s
)
189 if (state
->texture
[s
] == This
)
190 state
->changed
.texture
|= 1 << s
;
191 if (state
->changed
.texture
)
192 state
->changed
.group
|= NINE_STATE_TEXTURE
;
195 hr
= NineBaseTexture9_CreatePipeResource(This
, This
->lod_resident
!= -1);
198 res
= This
->base
.resource
;
200 if (This
->lod_resident
== -1) /* no levels were resident */
201 This
->lod_resident
= This
->base
.info
.last_level
+ 1;
203 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
204 struct NineTexture9
*tex
= NineTexture9(This
);
207 /* Mark uninitialized levels as dirty. */
208 box
.x
= box
.y
= box
.z
= 0;
210 for (l
= This
->lod
; l
< This
->lod_resident
; ++l
) {
211 box
.width
= u_minify(This
->base
.info
.width0
, l
);
212 box
.height
= u_minify(This
->base
.info
.height0
, l
);
213 NineSurface9_AddDirtyRect(tex
->surfaces
[l
], &box
);
215 for (l
= 0; l
< This
->lod
; ++l
)
216 NineSurface9_SetResource(tex
->surfaces
[l
], NULL
, -1);
217 for (; l
<= This
->base
.info
.last_level
; ++l
)
218 NineSurface9_SetResource(tex
->surfaces
[l
], res
, l
- This
->lod
);
220 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
221 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
225 /* Mark uninitialized levels as dirty. */
226 box
.x
= box
.y
= box
.z
= 0;
228 for (l
= This
->lod
; l
< This
->lod_resident
; ++l
) {
229 box
.width
= u_minify(This
->base
.info
.width0
, l
);
230 box
.height
= u_minify(This
->base
.info
.height0
, l
);
231 for (z
= 0; z
< 6; ++z
)
232 NineSurface9_AddDirtyRect(tex
->surfaces
[l
* 6 + z
], &box
);
234 for (l
= 0; l
< This
->lod
; ++l
) {
235 for (z
= 0; z
< 6; ++z
)
236 NineSurface9_SetResource(tex
->surfaces
[l
* 6 + z
],
239 for (; l
<= This
->base
.info
.last_level
; ++l
) {
240 for (z
= 0; z
< 6; ++z
)
241 NineSurface9_SetResource(tex
->surfaces
[l
* 6 + z
],
245 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
246 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
249 /* Mark uninitialized levels as dirty. */
250 box
.x
= box
.y
= box
.z
= 0;
251 for (l
= This
->lod
; l
< This
->lod_resident
; ++l
) {
252 box
.width
= u_minify(This
->base
.info
.width0
, l
);
253 box
.height
= u_minify(This
->base
.info
.height0
, l
);
254 box
.depth
= u_minify(This
->base
.info
.depth0
, l
);
255 NineVolume9_AddDirtyRegion(tex
->volumes
[l
], &box
);
257 for (l
= 0; l
< This
->lod
; ++l
)
258 NineVolume9_SetResource(tex
->volumes
[l
], NULL
, -1);
259 for (; l
<= This
->base
.info
.last_level
; ++l
)
260 NineVolume9_SetResource(tex
->volumes
[l
], res
, l
- This
->lod
);
262 assert(!"invalid texture type");
265 if (This
->lod
< This
->lod_resident
)
267 This
->lod_resident
= This
->lod
;
272 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
273 struct NineTexture9
*tex
= NineTexture9(This
);
278 DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
279 tex
->dirty_rect
.x
, tex
->dirty_rect
.y
,
280 tex
->dirty_rect
.width
, tex
->dirty_rect
.height
);
282 if (tex
->dirty_rect
.width
) {
283 for (l
= 0; l
<= last_level
; ++l
) {
284 u_box_minify_2d(&box
, &tex
->dirty_rect
, l
);
285 NineSurface9_AddDirtyRect(tex
->surfaces
[l
], &box
);
287 memset(&tex
->dirty_rect
, 0, sizeof(tex
->dirty_rect
));
288 tex
->dirty_rect
.depth
= 1;
290 for (l
= This
->lod
; l
<= last_level
; ++l
)
291 NineSurface9_UploadSelf(tex
->surfaces
[l
]);
293 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
294 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
300 for (z
= 0; z
< 6; ++z
) {
301 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z
,
302 tex
->dirty_rect
[z
].x
, tex
->dirty_rect
[z
].y
,
303 tex
->dirty_rect
[z
].width
, tex
->dirty_rect
[z
].height
);
305 if (tex
->dirty_rect
[z
].width
) {
306 for (l
= 0; l
<= last_level
; ++l
) {
307 u_box_minify_2d(&box
, &tex
->dirty_rect
[z
], l
);
308 NineSurface9_AddDirtyRect(tex
->surfaces
[l
* 6 + z
], &box
);
310 memset(&tex
->dirty_rect
[z
], 0, sizeof(tex
->dirty_rect
[z
]));
311 tex
->dirty_rect
[z
].depth
= 1;
313 for (l
= This
->lod
; l
<= last_level
; ++l
)
314 NineSurface9_UploadSelf(tex
->surfaces
[l
* 6 + z
]);
317 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
318 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
321 DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
322 tex
->dirty_box
.x
, tex
->dirty_box
.y
, tex
->dirty_box
.y
,
323 tex
->dirty_box
.width
, tex
->dirty_box
.height
, tex
->dirty_box
.depth
);
325 if (tex
->dirty_box
.width
) {
326 for (l
= 0; l
<= last_level
; ++l
) {
327 u_box_minify_2d(&box
, &tex
->dirty_box
, l
);
328 NineVolume9_AddDirtyRegion(tex
->volumes
[l
], &tex
->dirty_box
);
330 memset(&tex
->dirty_box
, 0, sizeof(tex
->dirty_box
));
332 for (l
= This
->lod
; l
<= last_level
; ++l
)
333 NineVolume9_UploadSelf(tex
->volumes
[l
]);
335 assert(!"invalid texture type");
339 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
340 This
->dirty_mip
= TRUE
;
341 /* TODO: if dirty only because of lod change, only generate added levels */
343 DBG("DONE, generate mip maps = %i\n", This
->dirty_mip
);
348 NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9
*This
)
350 struct pipe_resource
*resource
= This
->base
.resource
;
352 unsigned base_level
= 0;
353 unsigned last_level
= This
->base
.info
.last_level
- This
->lod
;
354 unsigned first_layer
= 0;
356 unsigned filter
= This
->mipfilter
== D3DTEXF_POINT
? PIPE_TEX_FILTER_NEAREST
357 : PIPE_TEX_FILTER_LINEAR
;
358 DBG("This=%p\n", This
);
360 if (This
->base
.pool
== D3DPOOL_MANAGED
)
361 NineBaseTexture9_UploadSelf(This
);
362 if (!This
->dirty_mip
)
365 ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
370 NineBaseTexture9_UpdateSamplerView(This
, 0);
372 last_layer
= util_max_layer(This
->view
[0]->texture
, base_level
);
374 util_gen_mipmap(This
->pipe
, resource
,
375 resource
->format
, base_level
, last_level
,
376 first_layer
, last_layer
, filter
);
378 This
->dirty_mip
= FALSE
;
380 NineDevice9_RestoreNonCSOState(This
->base
.base
.device
, ~0x3);
384 NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9
*This
,
387 struct pipe_context
*pipe
= This
->pipe
;
388 struct pipe_screen
*screen
= This
->base
.info
.screen
;
389 struct pipe_resource templ
;
391 struct pipe_resource
*res
;
392 struct pipe_resource
*old
= This
->base
.resource
;
394 DBG("This=%p lod=%u last_level=%u\n", This
,
395 This
->lod
, This
->base
.info
.last_level
);
397 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
399 templ
= This
->base
.info
;
402 templ
.width0
= u_minify(templ
.width0
, This
->lod
);
403 templ
.height0
= u_minify(templ
.height0
, This
->lod
);
404 templ
.depth0
= u_minify(templ
.depth0
, This
->lod
);
406 templ
.last_level
= This
->base
.info
.last_level
- This
->lod
;
409 /* LOD might have changed. */
410 if (old
->width0
== templ
.width0
&&
411 old
->height0
== templ
.height0
&&
412 old
->depth0
== templ
.depth0
)
416 res
= screen
->resource_create(screen
, &templ
);
418 return D3DERR_OUTOFVIDEOMEMORY
;
419 This
->base
.resource
= res
;
421 if (old
&& CopyData
) { /* Don't return without releasing old ! */
427 l
= (This
->lod
< This
->lod_resident
) ? This
->lod_resident
- This
->lod
: 0;
428 m
= (This
->lod
< This
->lod_resident
) ? 0 : This
->lod
- This
->lod_resident
;
430 box
.width
= u_minify(templ
.width0
, l
);
431 box
.height
= u_minify(templ
.height0
, l
);
432 box
.depth
= u_minify(templ
.depth0
, l
);
434 for (; l
<= templ
.last_level
; ++l
, ++m
) {
435 pipe
->resource_copy_region(pipe
,
438 box
.width
= u_minify(box
.width
, 1);
439 box
.height
= u_minify(box
.height
, 1);
440 box
.depth
= u_minify(box
.depth
, 1);
443 pipe_resource_reference(&old
, NULL
);
448 #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
449 s == UTIL_FORMAT_SWIZZLE_1 || \
450 s == UTIL_FORMAT_SWIZZLE_NONE)
453 NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9
*This
,
456 const struct util_format_description
*desc
;
457 struct pipe_context
*pipe
= This
->pipe
;
458 struct pipe_screen
*screen
= pipe
->screen
;
459 struct pipe_resource
*resource
= This
->base
.resource
;
460 struct pipe_sampler_view templ
;
461 enum pipe_format srgb_format
;
465 DBG("This=%p sRGB=%d\n", This
, sRGB
);
467 if (unlikely(!resource
)) {
468 if (unlikely(This
->format
== D3DFMT_NULL
))
470 NineBaseTexture9_Dump(This
);
471 /* hack due to incorrect POOL_MANAGED handling */
472 NineBaseTexture9_GenerateMipSubLevels(This
);
473 resource
= This
->base
.resource
;
477 pipe_sampler_view_reference(&This
->view
[sRGB
], NULL
);
479 swizzle
[0] = PIPE_SWIZZLE_RED
;
480 swizzle
[1] = PIPE_SWIZZLE_GREEN
;
481 swizzle
[2] = PIPE_SWIZZLE_BLUE
;
482 swizzle
[3] = PIPE_SWIZZLE_ALPHA
;
483 desc
= util_format_description(resource
->format
);
484 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
485 /* msdn doc is incomplete here and wrong.
486 * The only formats that can be read directly here
487 * are DF16, DF24 and INTZ.
488 * Tested on win the swizzle is
489 * R = depth, G = B = 0, A = 1 for DF16 and DF24
490 * R = G = B = A = depth for INTZ
491 * For the other ZS formats that can't be read directly
492 * but can be used as shadow map, the result is duplicated on
494 if (This
->format
== D3DFMT_DF16
||
495 This
->format
== D3DFMT_DF24
) {
496 swizzle
[1] = PIPE_SWIZZLE_ZERO
;
497 swizzle
[2] = PIPE_SWIZZLE_ZERO
;
498 swizzle
[3] = PIPE_SWIZZLE_ONE
;
500 swizzle
[1] = PIPE_SWIZZLE_RED
;
501 swizzle
[2] = PIPE_SWIZZLE_RED
;
502 swizzle
[3] = PIPE_SWIZZLE_RED
;
504 } else if (resource
->format
!= PIPE_FORMAT_A8_UNORM
&&
505 resource
->format
!= PIPE_FORMAT_RGTC1_UNORM
) {
507 * A8 should have 0.0 as default values for RGB.
508 * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
509 * It is already what gallium does. All the other ones
510 * should have 1.0 for non-defined values */
511 for (i
= 0; i
< 4; i
++) {
512 if (SWIZZLE_TO_REPLACE(desc
->swizzle
[i
]))
513 swizzle
[i
] = PIPE_SWIZZLE_ONE
;
517 /* if requested and supported, convert to the sRGB format */
518 srgb_format
= util_format_srgb(resource
->format
);
519 if (sRGB
&& srgb_format
!= PIPE_FORMAT_NONE
&&
520 screen
->is_format_supported(screen
, srgb_format
,
521 resource
->target
, 0, resource
->bind
))
522 templ
.format
= srgb_format
;
524 templ
.format
= resource
->format
;
525 templ
.u
.tex
.first_layer
= 0;
526 templ
.u
.tex
.last_layer
= resource
->target
== PIPE_TEXTURE_3D
?
527 resource
->depth0
- 1 : resource
->array_size
- 1;
528 templ
.u
.tex
.first_level
= 0;
529 templ
.u
.tex
.last_level
= resource
->last_level
;
530 templ
.swizzle_r
= swizzle
[0];
531 templ
.swizzle_g
= swizzle
[1];
532 templ
.swizzle_b
= swizzle
[2];
533 templ
.swizzle_a
= swizzle
[3];
534 templ
.target
= resource
->target
;
536 This
->view
[sRGB
] = pipe
->create_sampler_view(pipe
, resource
, &templ
);
538 DBG("sampler view = %p(resource = %p)\n", This
->view
[sRGB
], resource
);
540 return This
->view
? D3D_OK
: D3DERR_DRIVERINTERNALERROR
;
544 NineBaseTexture9_PreLoad( struct NineBaseTexture9
*This
)
546 DBG("This=%p\n", This
);
548 if (This
->dirty
&& This
->base
.pool
== D3DPOOL_MANAGED
)
549 NineBaseTexture9_UploadSelf(This
);
554 NineBaseTexture9_Dump( struct NineBaseTexture9
*This
)
556 DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
557 "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This
,
559 nine_D3DPOOL_to_str(This
->base
.pool
),
560 nine_D3DRTYPE_to_str(This
->base
.type
),
561 nine_D3DUSAGE_to_str(This
->base
.usage
),
562 d3dformat_to_string(This
->format
),
563 This
->base
.info
.width0
, This
->base
.info
.height0
, This
->base
.info
.depth0
,
564 This
->base
.info
.array_size
, This
->base
.info
.last_level
,
565 This
->lod
, This
->lod_resident
);