#endif
#include "util/u_format.h"
-#include "util/u_gen_mipmap.h"
#define DBG_CHANNEL DBG_BASETEXTURE
user_assert(!(Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) ||
Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
user_assert(!(Usage & D3DUSAGE_DYNAMIC) ||
- Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
+ !(Pool == D3DPOOL_MANAGED ||
+ Pool == D3DPOOL_SCRATCH), D3DERR_INVALIDCALL);
hr = NineResource9_ctor(&This->base, pParams, initResource, alloc, Type, Pool, Usage);
if (FAILED(hr))
return hr;
This->format = format;
- This->pipe = pParams->device->pipe;
This->mipfilter = (Usage & D3DUSAGE_AUTOGENMIPMAP) ?
D3DTEXF_LINEAR : D3DTEXF_NONE;
This->managed.lod = 0;
This->managed.lod_resident = -1;
+ /* Mark the texture as dirty to trigger first upload when we need the texture,
+ * even if it wasn't set by the application */
+ if (Pool == D3DPOOL_MANAGED)
+ This->managed.dirty = TRUE;
/* When a depth buffer is sampled, it is for shadow mapping, except for
* D3DFMT_INTZ, D3DFMT_DF16 and D3DFMT_DF24.
* In addition D3DFMT_INTZ can be used for both texturing and depth buffering
util_format_has_depth(util_format_description(This->base.info.format));
list_inithead(&This->list);
+ list_inithead(&This->list2);
+ if (Pool == D3DPOOL_MANAGED)
+ list_add(&This->list2, &This->base.base.device->managed_textures);
return D3D_OK;
}
pipe_sampler_view_reference(&This->view[1], NULL);
if (This->list.prev != NULL && This->list.next != NULL)
- list_del(&This->list),
+ list_del(&This->list);
+ if (This->list2.prev != NULL && This->list2.next != NULL)
+ list_del(&This->list2);
NineResource9_dtor(&This->base);
}
-DWORD WINAPI
+DWORD NINE_WINAPI
NineBaseTexture9_SetLOD( struct NineBaseTexture9 *This,
DWORD LODNew )
{
DWORD old = This->managed.lod;
+ DWORD max_level;
DBG("This=%p LODNew=%d\n", This, LODNew);
user_assert(This->base.pool == D3DPOOL_MANAGED, 0);
- This->managed.lod = MIN2(LODNew, This->base.info.last_level);
+ max_level = (This->base.usage & D3DUSAGE_AUTOGENMIPMAP) ?
+ 0 : This->base.info.last_level;
+ This->managed.lod = MIN2(LODNew, max_level);
if (This->managed.lod != old && This->bind_count && LIST_IS_EMPTY(&This->list))
list_add(&This->list, &This->base.base.device->update_textures);
return old;
}
-DWORD WINAPI
+DWORD NINE_WINAPI
NineBaseTexture9_GetLOD( struct NineBaseTexture9 *This )
{
DBG("This=%p\n", This);
return This->managed.lod;
}
-DWORD WINAPI
+DWORD NINE_WINAPI
NineBaseTexture9_GetLevelCount( struct NineBaseTexture9 *This )
{
DBG("This=%p\n", This);
return This->base.info.last_level + 1;
}
-HRESULT WINAPI
+HRESULT NINE_WINAPI
NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9 *This,
D3DTEXTUREFILTERTYPE FilterType )
{
user_assert(FilterType != D3DTEXF_NONE, D3DERR_INVALIDCALL);
This->mipfilter = FilterType;
+ This->dirty_mip = TRUE;
+ NineBaseTexture9_GenerateMipSubLevels(This);
return D3D_OK;
}
-D3DTEXTUREFILTERTYPE WINAPI
+D3DTEXTUREFILTERTYPE NINE_WINAPI
NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9 *This )
{
DBG("This=%p\n", This);
{
HRESULT hr;
unsigned last_level = This->base.info.last_level;
- unsigned l;
+ unsigned l, min_level_dirty = This->managed.lod;
+ BOOL update_lod;
DBG("This=%p dirty=%i type=%s\n", This, This->managed.dirty,
nine_D3DRTYPE_to_str(This->base.type));
assert(This->base.pool == D3DPOOL_MANAGED);
if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
- last_level = 0; /* TODO: What if level 0 is not resident ? */
+ last_level = 0;
- if (This->managed.lod_resident != This->managed.lod) {
+ update_lod = This->managed.lod_resident != This->managed.lod;
+ if (!update_lod && !This->managed.dirty)
+ return D3D_OK;
+
+ /* Allocate a new resource with the correct number of levels,
+ * Mark states for update, and tell the nine surfaces/volumes
+ * their new resource. */
+ if (update_lod) {
struct pipe_resource *res;
DBG("updating LOD from %u to %u ...\n", This->managed.lod_resident, This->managed.lod);
pipe_sampler_view_reference(&This->view[0], NULL);
pipe_sampler_view_reference(&This->view[1], NULL);
- if (This->bind_count) {
- /* mark state dirty */
- struct nine_state *state = &This->base.base.device->state;
- unsigned s;
- for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
- if (state->texture[s] == This)
- state->changed.texture |= 1 << s;
- if (state->changed.texture)
- state->changed.group |= NINE_STATE_TEXTURE;
- }
-
+ /* Allocate a new resource */
hr = NineBaseTexture9_CreatePipeResource(This, This->managed.lod_resident != -1);
if (FAILED(hr))
return hr;
res = This->base.resource;
- if (This->managed.lod_resident == -1) /* no levels were resident */
+ if (This->managed.lod_resident == -1) {/* no levels were resident */
+ This->managed.dirty = FALSE; /* We are going to upload everything. */
This->managed.lod_resident = This->base.info.last_level + 1;
+ }
+
+ if (This->base.type == D3DRTYPE_TEXTURE) {
+ struct NineTexture9 *tex = NineTexture9(This);
+
+ /* last content (if apply) has been copied to the new resource.
+ * Note: We cannot render to surfaces of managed textures.
+ * Note2: the level argument passed is to get the level offset
+ * right when the texture is uploaded (the texture first level
+ * corresponds to This->managed.lod).
+ * Note3: We don't care about the value passed for the surfaces
+ * before This->managed.lod, negative with this implementation. */
+ for (l = 0; l <= This->base.info.last_level; ++l)
+ NineSurface9_SetResource(tex->surfaces[l], res, l - This->managed.lod);
+ } else
+ if (This->base.type == D3DRTYPE_CUBETEXTURE) {
+ struct NineCubeTexture9 *tex = NineCubeTexture9(This);
+ unsigned z;
+
+ for (l = 0; l <= This->base.info.last_level; ++l) {
+ for (z = 0; z < 6; ++z)
+ NineSurface9_SetResource(tex->surfaces[l * 6 + z],
+ res, l - This->managed.lod);
+ }
+ } else
+ if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
+ struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
+
+ for (l = 0; l <= This->base.info.last_level; ++l)
+ NineVolume9_SetResource(tex->volumes[l], res, l - This->managed.lod);
+ } else {
+ assert(!"invalid texture type");
+ }
+
+ /* We are going to fully upload the new levels,
+ * no need to update dirty parts of the texture for these */
+ min_level_dirty = MAX2(This->managed.lod, This->managed.lod_resident);
+ }
+
+ /* Update dirty parts of the texture */
+ if (This->managed.dirty) {
+ if (This->base.type == D3DRTYPE_TEXTURE) {
+ struct NineTexture9 *tex = NineTexture9(This);
+ struct pipe_box box;
+ box.z = 0;
+ box.depth = 1;
+
+ DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
+ tex->dirty_rect.x, tex->dirty_rect.y,
+ tex->dirty_rect.width, tex->dirty_rect.height);
+
+ /* Note: for l < min_level_dirty, the resource is
+ * either non-existing (and thus will be entirely re-uploaded
+ * if the lod changes) or going to have a full upload */
+ if (tex->dirty_rect.width) {
+ for (l = min_level_dirty; l <= last_level; ++l) {
+ u_box_minify_2d(&box, &tex->dirty_rect, l);
+ NineSurface9_UploadSelf(tex->surfaces[l], &box);
+ }
+ memset(&tex->dirty_rect, 0, sizeof(tex->dirty_rect));
+ tex->dirty_rect.depth = 1;
+ }
+ } else
+ if (This->base.type == D3DRTYPE_CUBETEXTURE) {
+ struct NineCubeTexture9 *tex = NineCubeTexture9(This);
+ unsigned z;
+ struct pipe_box box;
+ box.z = 0;
+ box.depth = 1;
+
+ for (z = 0; z < 6; ++z) {
+ DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z,
+ tex->dirty_rect[z].x, tex->dirty_rect[z].y,
+ tex->dirty_rect[z].width, tex->dirty_rect[z].height);
+
+ if (tex->dirty_rect[z].width) {
+ for (l = min_level_dirty; l <= last_level; ++l) {
+ u_box_minify_2d(&box, &tex->dirty_rect[z], l);
+ NineSurface9_UploadSelf(tex->surfaces[l * 6 + z], &box);
+ }
+ memset(&tex->dirty_rect[z], 0, sizeof(tex->dirty_rect[z]));
+ tex->dirty_rect[z].depth = 1;
+ }
+ }
+ } else
+ if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
+ struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
+ struct pipe_box box;
+
+ DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
+ tex->dirty_box.x, tex->dirty_box.y, tex->dirty_box.y,
+ tex->dirty_box.width, tex->dirty_box.height, tex->dirty_box.depth);
+
+ if (tex->dirty_box.width) {
+ for (l = min_level_dirty; l <= last_level; ++l) {
+ u_box_minify_3d(&box, &tex->dirty_box, l);
+ NineVolume9_UploadSelf(tex->volumes[l], &box);
+ }
+ memset(&tex->dirty_box, 0, sizeof(tex->dirty_box));
+ }
+ } else {
+ assert(!"invalid texture type");
+ }
+ This->managed.dirty = FALSE;
+ }
+ /* Upload the new levels */
+ if (update_lod) {
if (This->base.type == D3DRTYPE_TEXTURE) {
struct NineTexture9 *tex = NineTexture9(This);
struct pipe_box box;
- /* Mark uninitialized levels as dirty. */
box.x = box.y = box.z = 0;
box.depth = 1;
for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
box.width = u_minify(This->base.info.width0, l);
box.height = u_minify(This->base.info.height0, l);
- NineSurface9_AddDirtyRect(tex->surfaces[l], &box);
+ NineSurface9_UploadSelf(tex->surfaces[l], &box);
}
- for (l = 0; l < This->managed.lod; ++l)
- NineSurface9_SetResource(tex->surfaces[l], NULL, -1);
- for (; l <= This->base.info.last_level; ++l)
- NineSurface9_SetResource(tex->surfaces[l], res, l - This->managed.lod);
} else
if (This->base.type == D3DRTYPE_CUBETEXTURE) {
struct NineCubeTexture9 *tex = NineCubeTexture9(This);
struct pipe_box box;
unsigned z;
- /* Mark uninitialized levels as dirty. */
box.x = box.y = box.z = 0;
box.depth = 1;
for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
box.width = u_minify(This->base.info.width0, l);
box.height = u_minify(This->base.info.height0, l);
for (z = 0; z < 6; ++z)
- NineSurface9_AddDirtyRect(tex->surfaces[l * 6 + z], &box);
- }
- for (l = 0; l < This->managed.lod; ++l) {
- for (z = 0; z < 6; ++z)
- NineSurface9_SetResource(tex->surfaces[l * 6 + z],
- NULL, -1);
- }
- for (; l <= This->base.info.last_level; ++l) {
- for (z = 0; z < 6; ++z)
- NineSurface9_SetResource(tex->surfaces[l * 6 + z],
- res, l - This->managed.lod);
+ NineSurface9_UploadSelf(tex->surfaces[l * 6 + z], &box);
}
} else
if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
struct pipe_box box;
- /* Mark uninitialized levels as dirty. */
box.x = box.y = box.z = 0;
for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
box.width = u_minify(This->base.info.width0, l);
box.height = u_minify(This->base.info.height0, l);
box.depth = u_minify(This->base.info.depth0, l);
- NineVolume9_AddDirtyRegion(tex->volumes[l], &box);
+ NineVolume9_UploadSelf(tex->volumes[l], &box);
}
- for (l = 0; l < This->managed.lod; ++l)
- NineVolume9_SetResource(tex->volumes[l], NULL, -1);
- for (; l <= This->base.info.last_level; ++l)
- NineVolume9_SetResource(tex->volumes[l], res, l - This->managed.lod);
} else {
assert(!"invalid texture type");
}
- if (This->managed.lod < This->managed.lod_resident)
- This->managed.dirty = TRUE;
This->managed.lod_resident = This->managed.lod;
}
- if (!This->managed.dirty)
- return D3D_OK;
-
- if (This->base.type == D3DRTYPE_TEXTURE) {
- struct NineTexture9 *tex = NineTexture9(This);
- struct pipe_box box;
- box.z = 0;
- box.depth = 1;
-
- DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
- tex->dirty_rect.x, tex->dirty_rect.y,
- tex->dirty_rect.width, tex->dirty_rect.height);
-
- /* Note: for l < This->managed.lod, the resource is
- * non-existing, and thus will be entirely re-uploaded
- * if This->managed.lod changes */
- if (tex->dirty_rect.width) {
- for (l = This->managed.lod; l <= last_level; ++l) {
- u_box_minify_2d(&box, &tex->dirty_rect, l);
- NineSurface9_UploadSelf(tex->surfaces[l], &box);
- }
- memset(&tex->dirty_rect, 0, sizeof(tex->dirty_rect));
- tex->dirty_rect.depth = 1;
- }
- } else
- if (This->base.type == D3DRTYPE_CUBETEXTURE) {
- struct NineCubeTexture9 *tex = NineCubeTexture9(This);
- unsigned z;
- struct pipe_box box;
- box.z = 0;
- box.depth = 1;
-
- for (z = 0; z < 6; ++z) {
- DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z,
- tex->dirty_rect[z].x, tex->dirty_rect[z].y,
- tex->dirty_rect[z].width, tex->dirty_rect[z].height);
-
- if (tex->dirty_rect[z].width) {
- for (l = This->managed.lod; l <= last_level; ++l) {
- u_box_minify_2d(&box, &tex->dirty_rect[z], l);
- NineSurface9_UploadSelf(tex->surfaces[l * 6 + z], &box);
- }
- memset(&tex->dirty_rect[z], 0, sizeof(tex->dirty_rect[z]));
- tex->dirty_rect[z].depth = 1;
- }
- }
- } else
- if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
- struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
- struct pipe_box box;
-
- DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
- tex->dirty_box.x, tex->dirty_box.y, tex->dirty_box.y,
- tex->dirty_box.width, tex->dirty_box.height, tex->dirty_box.depth);
-
- if (tex->dirty_box.width) {
- for (l = 0; l <= last_level; ++l) {
- u_box_minify_2d(&box, &tex->dirty_box, l);
- NineVolume9_AddDirtyRegion(tex->volumes[l], &tex->dirty_box);
- }
- memset(&tex->dirty_box, 0, sizeof(tex->dirty_box));
- }
- for (l = This->managed.lod; l <= last_level; ++l)
- NineVolume9_UploadSelf(tex->volumes[l]);
- } else {
- assert(!"invalid texture type");
- }
- This->managed.dirty = FALSE;
if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
This->dirty_mip = TRUE;
- /* TODO: if dirty only because of lod change, only generate added levels */
+
+ /* Set again the textures currently bound to update the texture data */
+ if (This->bind_count) {
+ struct nine_state *state = &This->base.base.device->state;
+ unsigned s;
+ for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
+ /* Dirty tracking is done in device9 state, not nine_context. */
+ if (state->texture[s] == This)
+ nine_context_set_texture(This->base.base.device, s, This);
+ }
DBG("DONE, generate mip maps = %i\n", This->dirty_mip);
return D3D_OK;
}
-void WINAPI
+void NINE_WINAPI
NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This )
{
- struct pipe_resource *resource = This->base.resource;
-
unsigned base_level = 0;
unsigned last_level = This->base.info.last_level - This->managed.lod;
unsigned first_layer = 0;
last_layer = util_max_layer(This->view[0]->texture, base_level);
- util_gen_mipmap(This->pipe, resource,
- resource->format, base_level, last_level,
- first_layer, last_layer, filter);
+ nine_context_gen_mipmap(This->base.base.device, (struct NineUnknown *)This,
+ This->base.resource,
+ base_level, last_level,
+ first_layer, last_layer, filter);
This->dirty_mip = FALSE;
-
- NineDevice9_RestoreNonCSOState(This->base.base.device, ~0x3);
}
HRESULT
NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
BOOL CopyData )
{
- struct pipe_context *pipe = This->pipe;
+ struct pipe_context *pipe;
struct pipe_screen *screen = This->base.info.screen;
struct pipe_resource templ;
unsigned l, m;
box.height = u_minify(templ.height0, l);
box.depth = u_minify(templ.depth0, l);
+ pipe = nine_context_get_pipe_acquire(This->base.base.device);
+
for (; l <= templ.last_level; ++l, ++m) {
pipe->resource_copy_region(pipe,
res, l, 0, 0, 0,
box.height = u_minify(box.height, 1);
box.depth = u_minify(box.depth, 1);
}
+
+ nine_context_get_pipe_release(This->base.base.device);
}
pipe_resource_reference(&old, NULL);
return D3D_OK;
}
-#define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
- s == UTIL_FORMAT_SWIZZLE_1 || \
- s == UTIL_FORMAT_SWIZZLE_NONE)
+#define SWIZZLE_TO_REPLACE(s) (s == PIPE_SWIZZLE_0 || \
+ s == PIPE_SWIZZLE_1 || \
+ s == PIPE_SWIZZLE_NONE)
HRESULT
NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
const int sRGB )
{
const struct util_format_description *desc;
- struct pipe_context *pipe = This->pipe;
- struct pipe_screen *screen = pipe->screen;
+ struct pipe_context *pipe;
+ struct pipe_screen *screen = NineDevice9_GetScreen(This->base.base.device);
struct pipe_resource *resource = This->base.resource;
struct pipe_sampler_view templ;
enum pipe_format srgb_format;
if (unlikely(This->format == D3DFMT_NULL))
return D3D_OK;
NineBaseTexture9_Dump(This);
- /* hack due to incorrect POOL_MANAGED handling */
- NineBaseTexture9_GenerateMipSubLevels(This);
- resource = This->base.resource;
}
assert(resource);
pipe_sampler_view_reference(&This->view[sRGB], NULL);
- swizzle[0] = PIPE_SWIZZLE_RED;
- swizzle[1] = PIPE_SWIZZLE_GREEN;
- swizzle[2] = PIPE_SWIZZLE_BLUE;
- swizzle[3] = PIPE_SWIZZLE_ALPHA;
+ swizzle[0] = PIPE_SWIZZLE_X;
+ swizzle[1] = PIPE_SWIZZLE_Y;
+ swizzle[2] = PIPE_SWIZZLE_Z;
+ swizzle[3] = PIPE_SWIZZLE_W;
desc = util_format_description(resource->format);
if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
/* msdn doc is incomplete here and wrong.
* all channel */
if (This->format == D3DFMT_DF16 ||
This->format == D3DFMT_DF24) {
- swizzle[1] = PIPE_SWIZZLE_ZERO;
- swizzle[2] = PIPE_SWIZZLE_ZERO;
- swizzle[3] = PIPE_SWIZZLE_ONE;
+ swizzle[1] = PIPE_SWIZZLE_0;
+ swizzle[2] = PIPE_SWIZZLE_0;
+ swizzle[3] = PIPE_SWIZZLE_1;
} else {
- swizzle[1] = PIPE_SWIZZLE_RED;
- swizzle[2] = PIPE_SWIZZLE_RED;
- swizzle[3] = PIPE_SWIZZLE_RED;
+ swizzle[1] = PIPE_SWIZZLE_X;
+ swizzle[2] = PIPE_SWIZZLE_X;
+ swizzle[3] = PIPE_SWIZZLE_X;
}
+ } else if (resource->format == PIPE_FORMAT_RGTC2_UNORM) {
+ swizzle[0] = PIPE_SWIZZLE_Y;
+ swizzle[1] = PIPE_SWIZZLE_X;
+ swizzle[2] = PIPE_SWIZZLE_1;
+ swizzle[3] = PIPE_SWIZZLE_1;
} else if (resource->format != PIPE_FORMAT_A8_UNORM &&
resource->format != PIPE_FORMAT_RGTC1_UNORM) {
/* exceptions:
* should have 1.0 for non-defined values */
for (i = 0; i < 4; i++) {
if (SWIZZLE_TO_REPLACE(desc->swizzle[i]))
- swizzle[i] = PIPE_SWIZZLE_ONE;
+ swizzle[i] = PIPE_SWIZZLE_1;
}
}
srgb_format = util_format_srgb(resource->format);
if (sRGB && srgb_format != PIPE_FORMAT_NONE &&
screen->is_format_supported(screen, srgb_format,
- resource->target, 0, resource->bind))
+ resource->target, 0, 0, resource->bind))
templ.format = srgb_format;
else
templ.format = resource->format;
templ.swizzle_a = swizzle[3];
templ.target = resource->target;
+ pipe = nine_context_get_pipe_acquire(This->base.base.device);
This->view[sRGB] = pipe->create_sampler_view(pipe, resource, &templ);
+ nine_context_get_pipe_release(This->base.base.device);
DBG("sampler view = %p(resource = %p)\n", This->view[sRGB], resource);
return This->view ? D3D_OK : D3DERR_DRIVERINTERNALERROR;
}
-void WINAPI
+void NINE_WINAPI
NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This )
{
DBG("This=%p\n", This);
- if (This->managed.dirty && This->base.pool == D3DPOOL_MANAGED)
+ if (This->base.pool == D3DPOOL_MANAGED)
NineBaseTexture9_UploadSelf(This);
}
+void
+NineBaseTexture9_UnLoad( struct NineBaseTexture9 *This )
+{
+ if (This->base.pool != D3DPOOL_MANAGED ||
+ This->managed.lod_resident == -1)
+ return;
+
+ pipe_resource_reference(&This->base.resource, NULL);
+ This->managed.lod_resident = -1;
+ This->managed.dirty = TRUE;
+
+ /* If the texture is bound, we have to re-upload it */
+ BASETEX_REGISTER_UPDATE(This);
+}
+
#ifdef DEBUG
void
NineBaseTexture9_Dump( struct NineBaseTexture9 *This )