2012-05-01 20:00:24 +00:00
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// DirectXTexImage.cpp
|
|
|
|
//
|
|
|
|
// DirectX Texture Library - Image container
|
|
|
|
//
|
|
|
|
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
|
|
|
|
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
|
|
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
|
|
|
// PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// http://go.microsoft.com/fwlink/?LinkId=248926
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "directxtexp.h"
|
|
|
|
|
|
|
|
namespace DirectX
|
|
|
|
{
|
|
|
|
|
|
|
|
extern bool _CalculateMipLevels( _In_ size_t width, _In_ size_t height, _Inout_ size_t& mipLevels );
|
|
|
|
extern bool _CalculateMipLevels3D( _In_ size_t width, _In_ size_t height, _In_ size_t depth, _Inout_ size_t& mipLevels );
|
2013-03-23 00:52:43 +00:00
|
|
|
extern bool _IsAlphaAllOpaqueBC( _In_ const Image& cImage );
|
2012-05-01 20:00:24 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// Determines number of image array entries and pixel size
|
|
|
|
//-------------------------------------------------------------------------------------
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2012-05-01 20:00:24 +00:00
|
|
|
void _DetermineImageArray( const TexMetadata& metadata, DWORD cpFlags,
|
|
|
|
size_t& nImages, size_t& pixelSize )
|
|
|
|
{
|
|
|
|
assert( metadata.width > 0 && metadata.height > 0 && metadata.depth > 0 );
|
|
|
|
assert( metadata.arraySize > 0 );
|
|
|
|
assert( metadata.mipLevels > 0 );
|
|
|
|
|
|
|
|
size_t _pixelSize = 0;
|
|
|
|
size_t _nimages = 0;
|
|
|
|
|
|
|
|
switch( metadata.dimension )
|
|
|
|
{
|
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
|
|
|
for( size_t item = 0; item < metadata.arraySize; ++item )
|
|
|
|
{
|
|
|
|
size_t w = metadata.width;
|
|
|
|
size_t h = metadata.height;
|
|
|
|
|
|
|
|
for( size_t level=0; level < metadata.mipLevels; ++level )
|
|
|
|
{
|
|
|
|
size_t rowPitch, slicePitch;
|
|
|
|
ComputePitch( metadata.format, w, h, rowPitch, slicePitch, cpFlags );
|
|
|
|
|
|
|
|
_pixelSize += slicePitch;
|
|
|
|
++_nimages;
|
|
|
|
|
|
|
|
if ( h > 1 )
|
|
|
|
h >>= 1;
|
|
|
|
|
|
|
|
if ( w > 1 )
|
|
|
|
w >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
|
|
|
{
|
|
|
|
size_t w = metadata.width;
|
|
|
|
size_t h = metadata.height;
|
|
|
|
size_t d = metadata.depth;
|
|
|
|
|
|
|
|
for( size_t level=0; level < metadata.mipLevels; ++level )
|
|
|
|
{
|
|
|
|
size_t rowPitch, slicePitch;
|
|
|
|
ComputePitch( metadata.format, w, h, rowPitch, slicePitch, cpFlags );
|
|
|
|
|
|
|
|
for( size_t slice=0; slice < d; ++slice )
|
|
|
|
{
|
|
|
|
_pixelSize += slicePitch;
|
|
|
|
++_nimages;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( h > 1 )
|
|
|
|
h >>= 1;
|
|
|
|
|
|
|
|
if ( w > 1 )
|
|
|
|
w >>= 1;
|
|
|
|
|
|
|
|
if ( d > 1 )
|
|
|
|
d >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nImages = _nimages;
|
|
|
|
pixelSize = _pixelSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// Fills in the image array entries
|
|
|
|
//-------------------------------------------------------------------------------------
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2012-05-01 20:00:24 +00:00
|
|
|
bool _SetupImageArray( uint8_t *pMemory, size_t pixelSize,
|
|
|
|
const TexMetadata& metadata, DWORD cpFlags,
|
|
|
|
Image* images, size_t nImages )
|
|
|
|
{
|
|
|
|
assert( pMemory );
|
|
|
|
assert( pixelSize > 0 );
|
|
|
|
assert( nImages > 0 );
|
|
|
|
|
|
|
|
if ( !images )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
uint8_t* pixels = pMemory;
|
|
|
|
const uint8_t* pEndBits = pMemory + pixelSize;
|
|
|
|
|
|
|
|
switch( metadata.dimension )
|
|
|
|
{
|
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
2012-05-24 00:57:47 +00:00
|
|
|
if (metadata.arraySize == 0 || metadata.mipLevels == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
for( size_t item = 0; item < metadata.arraySize; ++item )
|
|
|
|
{
|
|
|
|
size_t w = metadata.width;
|
|
|
|
size_t h = metadata.height;
|
|
|
|
|
|
|
|
for( size_t level=0; level < metadata.mipLevels; ++level )
|
|
|
|
{
|
|
|
|
if ( index >= nImages )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t rowPitch, slicePitch;
|
|
|
|
ComputePitch( metadata.format, w, h, rowPitch, slicePitch, cpFlags );
|
|
|
|
|
|
|
|
images[index].width = w;
|
|
|
|
images[index].height = h;
|
|
|
|
images[index].format = metadata.format;
|
|
|
|
images[index].rowPitch = rowPitch;
|
|
|
|
images[index].slicePitch = slicePitch;
|
|
|
|
images[index].pixels = pixels;
|
|
|
|
++index;
|
|
|
|
|
|
|
|
pixels += slicePitch;
|
|
|
|
if ( pixels > pEndBits )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( h > 1 )
|
|
|
|
h >>= 1;
|
|
|
|
|
|
|
|
if ( w > 1 )
|
|
|
|
w >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
|
|
|
{
|
2012-05-24 00:57:47 +00:00
|
|
|
if (metadata.mipLevels == 0 || metadata.depth == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
size_t w = metadata.width;
|
|
|
|
size_t h = metadata.height;
|
|
|
|
size_t d = metadata.depth;
|
|
|
|
|
|
|
|
for( size_t level=0; level < metadata.mipLevels; ++level )
|
|
|
|
{
|
|
|
|
size_t rowPitch, slicePitch;
|
|
|
|
ComputePitch( metadata.format, w, h, rowPitch, slicePitch, cpFlags );
|
|
|
|
|
|
|
|
for( size_t slice=0; slice < d; ++slice )
|
|
|
|
{
|
|
|
|
if ( index >= nImages )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use the same memory organization that Direct3D 11 needs for D3D11_SUBRESOURCE_DATA
|
|
|
|
// with all slices of a given miplevel being continuous in memory
|
|
|
|
images[index].width = w;
|
|
|
|
images[index].height = h;
|
|
|
|
images[index].format = metadata.format;
|
|
|
|
images[index].rowPitch = rowPitch;
|
|
|
|
images[index].slicePitch = slicePitch;
|
|
|
|
images[index].pixels = pixels;
|
|
|
|
++index;
|
|
|
|
|
|
|
|
pixels += slicePitch;
|
|
|
|
if ( pixels > pEndBits )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( h > 1 )
|
|
|
|
h >>= 1;
|
|
|
|
|
|
|
|
if ( w > 1 )
|
|
|
|
w >>= 1;
|
|
|
|
|
|
|
|
if ( d > 1 )
|
|
|
|
d >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=====================================================================================
|
|
|
|
// ScratchImage - Bitmap image container
|
|
|
|
//=====================================================================================
|
|
|
|
|
2013-12-24 20:57:42 +00:00
|
|
|
ScratchImage& ScratchImage::operator= (ScratchImage&& moveFrom)
|
|
|
|
{
|
|
|
|
if ( this != &moveFrom )
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
|
|
|
|
_nimages = moveFrom._nimages;
|
|
|
|
_size = moveFrom._size;
|
|
|
|
_metadata = moveFrom._metadata;
|
|
|
|
_image = moveFrom._image;
|
|
|
|
_memory = moveFrom._memory;
|
|
|
|
|
|
|
|
moveFrom._nimages = 0;
|
|
|
|
moveFrom._size = 0;
|
|
|
|
moveFrom._image = nullptr;
|
|
|
|
moveFrom._memory = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// Methods
|
|
|
|
//-------------------------------------------------------------------------------------
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::Initialize( const TexMetadata& mdata, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( !IsValid(mdata.format) )
|
2012-05-01 20:00:24 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( IsPalettized(mdata.format) )
|
|
|
|
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
size_t mipLevels = mdata.mipLevels;
|
|
|
|
|
|
|
|
switch( mdata.dimension )
|
|
|
|
{
|
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
|
|
|
if ( !mdata.width || mdata.height != 1 || mdata.depth != 1 || !mdata.arraySize )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
if ( !_CalculateMipLevels(mdata.width,1,mipLevels) )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
|
|
|
if ( !mdata.width || !mdata.height || mdata.depth != 1 || !mdata.arraySize )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2013-03-23 00:52:43 +00:00
|
|
|
if ( mdata.IsCubemap() )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
if ( (mdata.arraySize % 6) != 0 )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !_CalculateMipLevels(mdata.width,mdata.height,mipLevels) )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
|
|
|
if ( !mdata.width || !mdata.height || !mdata.depth || mdata.arraySize != 1 )
|
|
|
|
return E_INVALIDARG;
|
2014-02-14 21:33:01 +00:00
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !_CalculateMipLevels3D(mdata.width,mdata.height,mdata.depth,mipLevels) )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
|
|
|
|
Release();
|
|
|
|
|
|
|
|
_metadata.width = mdata.width;
|
|
|
|
_metadata.height = mdata.height;
|
|
|
|
_metadata.depth = mdata.depth;
|
|
|
|
_metadata.arraySize = mdata.arraySize;
|
|
|
|
_metadata.mipLevels = mipLevels;
|
2013-03-23 00:52:43 +00:00
|
|
|
_metadata.miscFlags = mdata.miscFlags;
|
|
|
|
_metadata.miscFlags2 = mdata.miscFlags2;
|
2012-05-01 20:00:24 +00:00
|
|
|
_metadata.format = mdata.format;
|
|
|
|
_metadata.dimension = mdata.dimension;
|
|
|
|
|
|
|
|
size_t pixelSize, nimages;
|
2014-02-20 23:24:34 +00:00
|
|
|
_DetermineImageArray( _metadata, flags, nimages, pixelSize );
|
2012-05-01 20:00:24 +00:00
|
|
|
|
2013-03-20 21:12:40 +00:00
|
|
|
_image = new (std::nothrow) Image[ nimages ];
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !_image )
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
_nimages = nimages;
|
|
|
|
memset( _image, 0, sizeof(Image) * nimages );
|
|
|
|
|
|
|
|
_memory = reinterpret_cast<uint8_t*>( _aligned_malloc( pixelSize, 16 ) );
|
|
|
|
if ( !_memory )
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
_size = pixelSize;
|
2014-02-20 23:24:34 +00:00
|
|
|
if ( !_SetupImageArray( _memory, pixelSize, _metadata, flags, _image, nimages ) )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::Initialize1D( DXGI_FORMAT fmt, size_t length, size_t arraySize, size_t mipLevels, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( !length || !arraySize )
|
2012-05-01 20:00:24 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
// 1D is a special case of the 2D case
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT hr = Initialize2D( fmt, length, 1, arraySize, mipLevels, flags );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
_metadata.dimension = TEX_DIMENSION_TEXTURE1D;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::Initialize2D( DXGI_FORMAT fmt, size_t width, size_t height, size_t arraySize, size_t mipLevels, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( !IsValid(fmt) || !width || !height || !arraySize )
|
2012-05-01 20:00:24 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( IsPalettized(fmt) )
|
|
|
|
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !_CalculateMipLevels(width,height,mipLevels) )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
Release();
|
|
|
|
|
|
|
|
_metadata.width = width;
|
|
|
|
_metadata.height = height;
|
|
|
|
_metadata.depth = 1;
|
|
|
|
_metadata.arraySize = arraySize;
|
|
|
|
_metadata.mipLevels = mipLevels;
|
|
|
|
_metadata.miscFlags = 0;
|
2013-03-23 00:52:43 +00:00
|
|
|
_metadata.miscFlags2 = 0;
|
2012-05-01 20:00:24 +00:00
|
|
|
_metadata.format = fmt;
|
|
|
|
_metadata.dimension = TEX_DIMENSION_TEXTURE2D;
|
|
|
|
|
|
|
|
size_t pixelSize, nimages;
|
2014-02-20 23:24:34 +00:00
|
|
|
_DetermineImageArray( _metadata, flags, nimages, pixelSize );
|
2012-05-01 20:00:24 +00:00
|
|
|
|
2013-03-20 21:12:40 +00:00
|
|
|
_image = new (std::nothrow) Image[ nimages ];
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !_image )
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
_nimages = nimages;
|
|
|
|
memset( _image, 0, sizeof(Image) * nimages );
|
|
|
|
|
|
|
|
_memory = reinterpret_cast<uint8_t*>( _aligned_malloc( pixelSize, 16 ) );
|
|
|
|
if ( !_memory )
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
_size = pixelSize;
|
2014-02-20 23:24:34 +00:00
|
|
|
if ( !_SetupImageArray( _memory, pixelSize, _metadata, flags, _image, nimages ) )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::Initialize3D( DXGI_FORMAT fmt, size_t width, size_t height, size_t depth, size_t mipLevels, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( !IsValid(fmt) || !width || !height || !depth )
|
2012-05-01 20:00:24 +00:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2015-03-16 23:34:27 +00:00
|
|
|
if ( IsPalettized(fmt) )
|
2014-02-14 21:33:01 +00:00
|
|
|
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !_CalculateMipLevels3D(width,height,depth,mipLevels) )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
Release();
|
|
|
|
|
|
|
|
_metadata.width = width;
|
|
|
|
_metadata.height = height;
|
|
|
|
_metadata.depth = depth;
|
|
|
|
_metadata.arraySize = 1; // Direct3D 10.x/11 does not support arrays of 3D textures
|
|
|
|
_metadata.mipLevels = mipLevels;
|
|
|
|
_metadata.miscFlags = 0;
|
2013-03-23 00:52:43 +00:00
|
|
|
_metadata.miscFlags2 = 0;
|
2012-05-01 20:00:24 +00:00
|
|
|
_metadata.format = fmt;
|
|
|
|
_metadata.dimension = TEX_DIMENSION_TEXTURE3D;
|
|
|
|
|
|
|
|
size_t pixelSize, nimages;
|
2014-02-20 23:24:34 +00:00
|
|
|
_DetermineImageArray( _metadata, flags, nimages, pixelSize );
|
2012-05-01 20:00:24 +00:00
|
|
|
|
2013-03-20 21:12:40 +00:00
|
|
|
_image = new (std::nothrow) Image[ nimages ];
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !_image )
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
_nimages = nimages;
|
|
|
|
memset( _image, 0, sizeof(Image) * nimages );
|
|
|
|
|
|
|
|
_memory = reinterpret_cast<uint8_t*>( _aligned_malloc( pixelSize, 16 ) );
|
|
|
|
if ( !_memory )
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
_size = pixelSize;
|
|
|
|
|
2014-02-20 23:24:34 +00:00
|
|
|
if ( !_SetupImageArray( _memory, pixelSize, _metadata, flags, _image, nimages ) )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::InitializeCube( DXGI_FORMAT fmt, size_t width, size_t height, size_t nCubes, size_t mipLevels, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( !width || !height || !nCubes )
|
2012-05-01 20:00:24 +00:00
|
|
|
return E_INVALIDARG;
|
2014-02-14 21:33:01 +00:00
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
// A DirectX11 cubemap is just a 2D texture array that is a multiple of 6 for each cube
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT hr = Initialize2D( fmt, width, height, nCubes * 6, mipLevels, flags );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
_metadata.miscFlags |= TEX_MISC_TEXTURECUBE;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::InitializeFromImage( const Image& srcImage, bool allow1D, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
HRESULT hr = ( srcImage.height > 1 || !allow1D )
|
2014-02-20 23:24:34 +00:00
|
|
|
? Initialize2D( srcImage.format, srcImage.width, srcImage.height, 1, 1, flags )
|
|
|
|
: Initialize1D( srcImage.format, srcImage.width, 1, 1, flags );
|
2012-05-01 20:00:24 +00:00
|
|
|
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
return hr;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
size_t rowCount = ComputeScanlines( srcImage.format, srcImage.height );
|
|
|
|
if ( !rowCount )
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
const uint8_t* sptr = reinterpret_cast<const uint8_t*>( srcImage.pixels );
|
|
|
|
if ( !sptr )
|
|
|
|
return E_POINTER;
|
|
|
|
|
2013-06-21 21:12:39 +00:00
|
|
|
auto dptr = reinterpret_cast<uint8_t*>( _image[0].pixels );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !dptr )
|
|
|
|
return E_POINTER;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
size_t spitch = srcImage.rowPitch;
|
|
|
|
size_t dpitch = _image[0].rowPitch;
|
|
|
|
|
|
|
|
size_t size = std::min<size_t>( dpitch, spitch );
|
|
|
|
|
|
|
|
for( size_t y = 0; y < rowCount; ++y )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
memcpy_s( dptr, dpitch, sptr, size );
|
|
|
|
sptr += spitch;
|
|
|
|
dptr += dpitch;
|
2012-05-01 20:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::InitializeArrayFromImages( const Image* images, size_t nImages, bool allow1D, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
if ( !images || !nImages )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
DXGI_FORMAT format = images[0].format;
|
|
|
|
size_t width = images[0].width;
|
|
|
|
size_t height = images[0].height;
|
|
|
|
|
|
|
|
for( size_t index=0; index < nImages; ++index )
|
|
|
|
{
|
|
|
|
if ( !images[index].pixels )
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
if ( images[index].format != format || images[index].width != width || images[index].height != height )
|
|
|
|
{
|
|
|
|
// All images must be the same format, width, and height
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = ( height > 1 || !allow1D )
|
2014-02-20 23:24:34 +00:00
|
|
|
? Initialize2D( format, width, height, nImages, 1, flags )
|
|
|
|
: Initialize1D( format, width, nImages, 1, flags );
|
2012-05-01 20:00:24 +00:00
|
|
|
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
return hr;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
size_t rowCount = ComputeScanlines( format, height );
|
|
|
|
if ( !rowCount )
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
for( size_t index=0; index < nImages; ++index )
|
|
|
|
{
|
2013-06-21 21:12:39 +00:00
|
|
|
auto sptr = reinterpret_cast<const uint8_t*>( images[index].pixels );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !sptr )
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
assert( index < _nimages );
|
2013-06-21 21:12:39 +00:00
|
|
|
auto dptr = reinterpret_cast<uint8_t*>( _image[index].pixels );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !dptr )
|
|
|
|
return E_POINTER;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
size_t spitch = images[index].rowPitch;
|
|
|
|
size_t dpitch = _image[index].rowPitch;
|
|
|
|
|
|
|
|
size_t size = std::min<size_t>( dpitch, spitch );
|
|
|
|
|
|
|
|
for( size_t y = 0; y < rowCount; ++y )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
memcpy_s( dptr, dpitch, sptr, size );
|
|
|
|
sptr += spitch;
|
|
|
|
dptr += dpitch;
|
2012-05-01 20:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::InitializeCubeFromImages( const Image* images, size_t nImages, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
if ( !images || !nImages )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
// A DirectX11 cubemap is just a 2D texture array that is a multiple of 6 for each cube
|
|
|
|
if ( ( nImages % 6 ) != 0 )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT hr = InitializeArrayFromImages( images, nImages, false, flags );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
_metadata.miscFlags |= TEX_MISC_TEXTURECUBE;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT ScratchImage::Initialize3DFromImages( const Image* images, size_t depth, DWORD flags )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
|
|
|
if ( !images || !depth )
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
DXGI_FORMAT format = images[0].format;
|
|
|
|
size_t width = images[0].width;
|
|
|
|
size_t height = images[0].height;
|
|
|
|
|
|
|
|
for( size_t slice=0; slice < depth; ++slice )
|
|
|
|
{
|
|
|
|
if ( !images[slice].pixels )
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
if ( images[slice].format != format || images[slice].width != width || images[slice].height != height )
|
|
|
|
{
|
|
|
|
// All images must be the same format, width, and height
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 23:24:34 +00:00
|
|
|
HRESULT hr = Initialize3D( format, width, height, depth, 1, flags );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
return hr;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
size_t rowCount = ComputeScanlines( format, height );
|
|
|
|
if ( !rowCount )
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
for( size_t slice=0; slice < depth; ++slice )
|
|
|
|
{
|
2013-06-21 21:12:39 +00:00
|
|
|
auto sptr = reinterpret_cast<const uint8_t*>( images[slice].pixels );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !sptr )
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
assert( slice < _nimages );
|
2013-06-21 21:12:39 +00:00
|
|
|
auto dptr = reinterpret_cast<uint8_t*>( _image[slice].pixels );
|
2012-05-01 20:00:24 +00:00
|
|
|
if ( !dptr )
|
|
|
|
return E_POINTER;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
size_t spitch = images[slice].rowPitch;
|
|
|
|
size_t dpitch = _image[slice].rowPitch;
|
|
|
|
|
|
|
|
size_t size = std::min<size_t>( dpitch, spitch );
|
|
|
|
|
|
|
|
for( size_t y = 0; y < rowCount; ++y )
|
2012-05-01 20:00:24 +00:00
|
|
|
{
|
2014-02-14 21:33:01 +00:00
|
|
|
memcpy_s( dptr, dpitch, sptr, size );
|
|
|
|
sptr += spitch;
|
|
|
|
dptr += dpitch;
|
2012-05-01 20:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScratchImage::Release()
|
|
|
|
{
|
|
|
|
_nimages = 0;
|
|
|
|
_size = 0;
|
|
|
|
|
|
|
|
if ( _image )
|
|
|
|
{
|
|
|
|
delete [] _image;
|
|
|
|
_image = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _memory )
|
|
|
|
{
|
|
|
|
_aligned_free( _memory );
|
|
|
|
_memory = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&_metadata, 0, sizeof(_metadata));
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2012-07-02 20:31:12 +00:00
|
|
|
bool ScratchImage::OverrideFormat( DXGI_FORMAT f )
|
|
|
|
{
|
|
|
|
if ( !_image )
|
|
|
|
return false;
|
|
|
|
|
2014-02-14 21:33:01 +00:00
|
|
|
if ( !IsValid( f ) || IsPlanar( f ) || IsPalettized( f ) )
|
2012-07-02 20:31:12 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for( size_t index = 0; index < _nimages; ++index )
|
|
|
|
{
|
|
|
|
_image[ index ].format = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
_metadata.format = f;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-08 01:30:49 +00:00
|
|
|
_Use_decl_annotations_
|
2012-05-01 20:00:24 +00:00
|
|
|
const Image* ScratchImage::GetImage(size_t mip, size_t item, size_t slice) const
|
|
|
|
{
|
|
|
|
if ( mip >= _metadata.mipLevels )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
|
|
|
switch( _metadata.dimension )
|
|
|
|
{
|
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
|
|
|
if ( slice > 0 )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if ( item >= _metadata.arraySize )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
index = item*( _metadata.mipLevels ) + mip;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
|
|
|
if ( item > 0 )
|
|
|
|
{
|
|
|
|
// No support for arrays of volumes
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t d = _metadata.depth;
|
|
|
|
|
|
|
|
for( size_t level = 0; level < mip; ++level )
|
|
|
|
{
|
|
|
|
index += d;
|
|
|
|
if ( d > 1 )
|
|
|
|
d >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( slice >= d )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
index += slice;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &_image[index];
|
|
|
|
}
|
|
|
|
|
2013-03-23 00:52:43 +00:00
|
|
|
bool ScratchImage::IsAlphaAllOpaque() const
|
|
|
|
{
|
2013-09-09 22:10:35 +00:00
|
|
|
if ( !_image )
|
|
|
|
return false;
|
|
|
|
|
2013-03-23 00:52:43 +00:00
|
|
|
if ( !HasAlpha( _metadata.format ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( IsCompressed( _metadata.format ) )
|
|
|
|
{
|
|
|
|
for( size_t index = 0; index < _nimages; ++index )
|
|
|
|
{
|
|
|
|
if ( !_IsAlphaAllOpaqueBC( _image[ index ] ) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ScopedAlignedArrayXMVECTOR scanline( reinterpret_cast<XMVECTOR*>( _aligned_malloc( (sizeof(XMVECTOR)*_metadata.width), 16 ) ) );
|
|
|
|
if ( !scanline )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
static const XMVECTORF32 threshold = { 0.99f, 0.99f, 0.99f, 0.99f };
|
|
|
|
|
|
|
|
for( size_t index = 0; index < _nimages; ++index )
|
|
|
|
{
|
2013-10-08 01:21:09 +00:00
|
|
|
#pragma warning( suppress : 6011 )
|
2013-03-23 00:52:43 +00:00
|
|
|
const Image& img = _image[ index ];
|
|
|
|
|
|
|
|
const uint8_t *pPixels = img.pixels;
|
|
|
|
assert( pPixels );
|
|
|
|
|
|
|
|
for( size_t h = 0; h < img.height; ++h )
|
|
|
|
{
|
|
|
|
if ( !_LoadScanline( scanline.get(), img.width, pPixels, img.rowPitch, img.format ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
XMVECTOR* ptr = scanline.get();
|
|
|
|
for( size_t w = 0; w < img.width; ++w )
|
|
|
|
{
|
|
|
|
XMVECTOR alpha = XMVectorSplatW( *ptr );
|
|
|
|
if ( XMVector4Less( alpha, threshold ) )
|
|
|
|
return false;
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
pPixels += img.rowPitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-01 20:00:24 +00:00
|
|
|
}; // namespace
|