1 #include "..\include\raMain.h" 9 case DXGI_FORMAT_R8G8B8A8_TYPELESS:
10 case DXGI_FORMAT_R8G8B8A8_UNORM:
11 case DXGI_FORMAT_R8G8B8A8_UINT:
12 case DXGI_FORMAT_R8G8B8A8_SNORM:
13 case DXGI_FORMAT_R8G8B8A8_SINT:
14 return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
16 case DXGI_FORMAT_BC1_TYPELESS:
17 case DXGI_FORMAT_BC1_UNORM:
18 return DXGI_FORMAT_BC1_UNORM_SRGB;
19 case DXGI_FORMAT_BC2_TYPELESS:
20 case DXGI_FORMAT_BC2_UNORM:
21 return DXGI_FORMAT_BC2_UNORM_SRGB;
22 case DXGI_FORMAT_BC3_TYPELESS:
23 case DXGI_FORMAT_BC3_UNORM:
24 return DXGI_FORMAT_BC3_UNORM_SRGB;
36 m_TextureCache.RemoveAll();
44 D3DX11_IMAGE_LOAD_INFO* pLoadInfo, ID3DX11ThreadPump* pPump,
45 ID3D11ShaderResourceView** ppOutputRV,
bool bSRGB )
48 D3DX11_IMAGE_LOAD_INFO ZeroInfo;
49 D3DX11_IMAGE_INFO SrcInfo;
53 pLoadInfo = &ZeroInfo;
56 if( !pLoadInfo->pSrcInfo )
58 D3DX11GetImageInfoFromFile( pSrcFile.c_str(), NULL, &SrcInfo, NULL );
59 pLoadInfo->pSrcInfo = &SrcInfo;
61 pLoadInfo->Format = pLoadInfo->pSrcInfo->Format;
63 for(
int i=0; i < m_TextureCache.GetSize(); i++)
65 if( !strcmp( m_TextureCache[i].cSource, pSrcFile.c_str() ) &&
66 m_TextureCache[i].Width == pLoadInfo->Width &&
67 m_TextureCache[i].Height == pLoadInfo->Height &&
68 m_TextureCache[i].MipLevels == pLoadInfo->MipLevels &&
69 m_TextureCache[i].Usage11 == pLoadInfo->Usage &&
70 m_TextureCache[i].Format == pLoadInfo->Format &&
71 m_TextureCache[i].CpuAccessFlags == pLoadInfo->CpuAccessFlags &&
72 m_TextureCache[i].BindFlags == pLoadInfo->BindFlags &&
73 m_TextureCache[i].MiscFlags == pLoadInfo->MiscFlags )
75 return m_TextureCache[i].pSRV11->QueryInterface( __uuidof( ID3D11ShaderResourceView ), ( LPVOID* )ppOutputRV );
80 strcpy_s( NewEntry.
cSource, MAX_PATH, pSrcFile.c_str() );
81 NewEntry.
Width = pLoadInfo->Width;
82 NewEntry.
Height = pLoadInfo->Height;
83 NewEntry.
MipLevels = pLoadInfo->MipLevels;
84 NewEntry.
Usage11 = pLoadInfo->Usage;
85 NewEntry.
Format = pLoadInfo->Format;
88 NewEntry.
BindFlags = pLoadInfo->BindFlags;
89 NewEntry.
MiscFlags = pLoadInfo->MiscFlags;
92 ID3D11Texture2D* pRes = NULL;
93 hr = D3DX11CreateTextureFromFile( dx->
GetDevice(), pSrcFile.c_str(), pLoadInfo, pPump, ( ID3D11Resource** )&pRes, NULL );
97 D3D11_TEXTURE2D_DESC tex_dsc;
98 pRes->GetDesc(&tex_dsc);
102 ID3D11Texture2D* unormStaging = NULL;
103 ID3D11Texture2D* srgbStaging = NULL;
105 D3D11_TEXTURE2D_DESC CopyDesc;
106 pRes->GetDesc( &CopyDesc );
108 pLoadInfo->BindFlags = 0;
109 pLoadInfo->CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
110 pLoadInfo->Depth = 0;
111 pLoadInfo->Filter = D3DX11_FILTER_LINEAR;
112 pLoadInfo->FirstMipLevel = 0;
113 pLoadInfo->Format = CopyDesc.Format;
114 pLoadInfo->Height = CopyDesc.Height;
115 pLoadInfo->MipFilter = D3DX11_FILTER_LINEAR;
116 pLoadInfo->MiscFlags = CopyDesc.MiscFlags;
117 pLoadInfo->Usage = D3D11_USAGE_STAGING;
118 pLoadInfo->Width = CopyDesc.Width;
120 CopyDesc.BindFlags = 0;
121 CopyDesc.Usage = D3D11_USAGE_STAGING;
122 CopyDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
123 CopyDesc.Format =
MAKE_SRGB(CopyDesc.Format);
125 hr = D3DX11CreateTextureFromFile( dx->
GetDevice(), pSrcFile.c_str(), pLoadInfo, pPump, ( ID3D11Resource** )&unormStaging, NULL );
127 hr = dx->
GetDevice()->CreateTexture2D(&CopyDesc, NULL, &srgbStaging);
129 ID3D11Texture2D* srgbGPU;
131 pRes->GetDesc( &CopyDesc );
132 CopyDesc.Format =
MAKE_SRGB(CopyDesc.Format);
133 hr = dx->
GetDevice()->CreateTexture2D(&CopyDesc, NULL, &srgbGPU);
142 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
144 SRVDesc.Format =
MAKE_SRGB( ZeroInfo.Format );
146 SRVDesc.Format = ZeroInfo.Format;
147 if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE1D )
149 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
150 SRVDesc.Texture1D.MostDetailedMip = 0;
151 SRVDesc.Texture1D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
153 else if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE2D )
155 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
156 SRVDesc.Texture2D.MostDetailedMip = 0;
157 SRVDesc.Texture2D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
159 if( pLoadInfo->pSrcInfo->MiscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE )
161 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
162 SRVDesc.TextureCube.MostDetailedMip = 0;
163 SRVDesc.TextureCube.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
166 else if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE3D )
168 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
169 SRVDesc.Texture3D.MostDetailedMip = 0;
170 SRVDesc.Texture3D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
173 SRVDesc.Format =
MAKE_SRGB(tex_dsc.Format);
175 SRVDesc.Format = tex_dsc.Format;
177 SRVDesc.Texture2D.MipLevels = tex_dsc.MipLevels;
178 SRVDesc.Texture2D.MostDetailedMip = 0;
179 hr = dx->
GetDevice()->CreateShaderResourceView( pRes, &SRVDesc, ppOutputRV );
184 ( *ppOutputRV )->QueryInterface( __uuidof( ID3D11ShaderResourceView ), ( LPVOID* )&NewEntry.
pSRV11 );
186 m_TextureCache.Add(NewEntry);
191 D3DX11_IMAGE_LOAD_INFO* pLoadInfo, ID3DX11ThreadPump* pPump,
192 ID3D11ShaderResourceView** ppOutputRV,
bool bSRGB)
195 D3DX11_IMAGE_LOAD_INFO ZeroInfo;
196 D3DX11_IMAGE_INFO SrcInfo;
200 pLoadInfo = &ZeroInfo;
203 if( !pLoadInfo->pSrcInfo )
205 D3DX11GetImageInfoFromMemory( Data, Size, NULL, &SrcInfo, NULL );
206 pLoadInfo->pSrcInfo = &SrcInfo;
208 pLoadInfo->Format = pLoadInfo->pSrcInfo->Format;
210 for(
int i=0; i < m_TextureCache.GetSize(); i++)
212 if( !strcmp( m_TextureCache[i].cSource,
"" ) &&
213 m_TextureCache[i].Width == pLoadInfo->Width &&
214 m_TextureCache[i].Height == pLoadInfo->Height &&
215 m_TextureCache[i].MipLevels == pLoadInfo->MipLevels &&
216 m_TextureCache[i].Usage11 == pLoadInfo->Usage &&
217 m_TextureCache[i].Format == pLoadInfo->Format &&
218 m_TextureCache[i].CpuAccessFlags == pLoadInfo->CpuAccessFlags &&
219 m_TextureCache[i].BindFlags == pLoadInfo->BindFlags &&
220 m_TextureCache[i].MiscFlags == pLoadInfo->MiscFlags )
223 return m_TextureCache[i].pSRV11->QueryInterface( __uuidof( ID3D11ShaderResourceView ), ( LPVOID* )ppOutputRV );
228 strcpy_s( NewEntry.
cSource, MAX_PATH,
"" );
229 NewEntry.
Width = pLoadInfo->Width;
230 NewEntry.
Height = pLoadInfo->Height;
231 NewEntry.
MipLevels = pLoadInfo->MipLevels;
232 NewEntry.
Usage11 = pLoadInfo->Usage;
233 NewEntry.
Format = pLoadInfo->Format;
236 NewEntry.
BindFlags = pLoadInfo->BindFlags;
237 NewEntry.
MiscFlags = pLoadInfo->MiscFlags;
240 ID3D11Texture2D* pRes = NULL;
241 hr = D3DX11CreateTextureFromMemory( dx->
GetDevice(), Data, Size, pLoadInfo, pPump, ( ID3D11Resource** )&pRes, NULL );
245 D3D11_TEXTURE2D_DESC tex_dsc;
246 pRes->GetDesc(&tex_dsc);
250 ID3D11Texture2D* unormStaging = NULL;
251 ID3D11Texture2D* srgbStaging = NULL;
253 D3D11_TEXTURE2D_DESC CopyDesc;
254 pRes->GetDesc( &CopyDesc );
256 pLoadInfo->BindFlags = 0;
257 pLoadInfo->CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
258 pLoadInfo->Depth = 0;
259 pLoadInfo->Filter = D3DX11_FILTER_LINEAR;
260 pLoadInfo->FirstMipLevel = 0;
261 pLoadInfo->Format = CopyDesc.Format;
262 pLoadInfo->Height = CopyDesc.Height;
263 pLoadInfo->MipFilter = D3DX11_FILTER_LINEAR;
264 pLoadInfo->MiscFlags = CopyDesc.MiscFlags;
265 pLoadInfo->Usage = D3D11_USAGE_STAGING;
266 pLoadInfo->Width = CopyDesc.Width;
268 CopyDesc.BindFlags = 0;
269 CopyDesc.Usage = D3D11_USAGE_STAGING;
270 CopyDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
271 CopyDesc.Format =
MAKE_SRGB(CopyDesc.Format);
273 hr = D3DX11CreateTextureFromMemory( dx->
GetDevice(), Data, Size, pLoadInfo, pPump, ( ID3D11Resource** )&unormStaging, NULL );
275 hr = dx->
GetDevice()->CreateTexture2D(&CopyDesc, NULL, &srgbStaging);
277 ID3D11Texture2D* srgbGPU;
279 pRes->GetDesc( &CopyDesc );
280 CopyDesc.Format =
MAKE_SRGB(CopyDesc.Format);
281 hr = dx->
GetDevice()->CreateTexture2D(&CopyDesc, NULL, &srgbGPU);
290 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
292 SRVDesc.Format =
MAKE_SRGB( ZeroInfo.Format );
294 SRVDesc.Format = ZeroInfo.Format;
295 if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE1D )
297 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
298 SRVDesc.Texture1D.MostDetailedMip = 0;
299 SRVDesc.Texture1D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
301 else if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE2D )
303 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
304 SRVDesc.Texture2D.MostDetailedMip = 0;
305 SRVDesc.Texture2D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
307 if( pLoadInfo->pSrcInfo->MiscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE )
309 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
310 SRVDesc.TextureCube.MostDetailedMip = 0;
311 SRVDesc.TextureCube.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
314 else if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE3D )
316 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
317 SRVDesc.Texture3D.MostDetailedMip = 0;
318 SRVDesc.Texture3D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
321 SRVDesc.Format =
MAKE_SRGB(tex_dsc.Format);
323 SRVDesc.Format = tex_dsc.Format;
325 SRVDesc.Texture2D.MipLevels = tex_dsc.MipLevels;
326 SRVDesc.Texture2D.MostDetailedMip = 0;
327 hr = dx->
GetDevice()->CreateShaderResourceView( pRes, &SRVDesc, ppOutputRV );
332 ( *ppOutputRV )->QueryInterface( __uuidof( ID3D11ShaderResourceView ), ( LPVOID* )&NewEntry.
pSRV11 );
334 m_TextureCache.Add(NewEntry);
339 D3DX11_IMAGE_LOAD_INFO* pLoadInfo, ID3DX11ThreadPump* pPump,
340 ID3D11ShaderResourceView** ppOutputRV,
bool bSRGB)
343 D3DX11_IMAGE_LOAD_INFO ZeroInfo;
344 D3DX11_IMAGE_INFO SrcInfo;
348 pLoadInfo = &ZeroInfo;
351 if( !pLoadInfo->pSrcInfo )
353 D3DX11GetImageInfoFromResource( hSrcModule, pSrcResource, NULL, &SrcInfo, NULL );
354 pLoadInfo->pSrcInfo = &SrcInfo;
356 pLoadInfo->Format = pLoadInfo->pSrcInfo->Format;
358 for(
int i=0; i < m_TextureCache.GetSize(); i++)
360 if( !strcmp( m_TextureCache[i].cSource, pSrcResource ) &&
361 m_TextureCache[i].Width == pLoadInfo->Width &&
362 m_TextureCache[i].Height == pLoadInfo->Height &&
363 m_TextureCache[i].MipLevels == pLoadInfo->MipLevels &&
364 m_TextureCache[i].Usage11 == pLoadInfo->Usage &&
365 m_TextureCache[i].Format == pLoadInfo->Format &&
366 m_TextureCache[i].CpuAccessFlags == pLoadInfo->CpuAccessFlags &&
367 m_TextureCache[i].BindFlags == pLoadInfo->BindFlags &&
368 m_TextureCache[i].MiscFlags == pLoadInfo->MiscFlags )
371 return m_TextureCache[i].pSRV11->QueryInterface( __uuidof( ID3D11ShaderResourceView ), ( LPVOID* )ppOutputRV );
376 strcpy_s( NewEntry.
cSource, MAX_PATH, pSrcResource );
377 NewEntry.
Width = pLoadInfo->Width;
378 NewEntry.
Height = pLoadInfo->Height;
379 NewEntry.
MipLevels = pLoadInfo->MipLevels;
380 NewEntry.
Usage11 = pLoadInfo->Usage;
381 NewEntry.
Format = pLoadInfo->Format;
384 NewEntry.
BindFlags = pLoadInfo->BindFlags;
385 NewEntry.
MiscFlags = pLoadInfo->MiscFlags;
388 ID3D11Texture2D* pRes = NULL;
389 hr = D3DX11CreateTextureFromResource( dx->
GetDevice(), hSrcModule, pSrcResource, pLoadInfo, pPump, ( ID3D11Resource** )&pRes, NULL );
393 D3D11_TEXTURE2D_DESC tex_dsc;
394 pRes->GetDesc(&tex_dsc);
398 ID3D11Texture2D* unormStaging = NULL;
399 ID3D11Texture2D* srgbStaging = NULL;
401 D3D11_TEXTURE2D_DESC CopyDesc;
402 pRes->GetDesc( &CopyDesc );
404 pLoadInfo->BindFlags = 0;
405 pLoadInfo->CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
406 pLoadInfo->Depth = 0;
407 pLoadInfo->Filter = D3DX11_FILTER_LINEAR;
408 pLoadInfo->FirstMipLevel = 0;
409 pLoadInfo->Format = CopyDesc.Format;
410 pLoadInfo->Height = CopyDesc.Height;
411 pLoadInfo->MipFilter = D3DX11_FILTER_LINEAR;
412 pLoadInfo->MiscFlags = CopyDesc.MiscFlags;
413 pLoadInfo->Usage = D3D11_USAGE_STAGING;
414 pLoadInfo->Width = CopyDesc.Width;
416 CopyDesc.BindFlags = 0;
417 CopyDesc.Usage = D3D11_USAGE_STAGING;
418 CopyDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
419 CopyDesc.Format =
MAKE_SRGB(CopyDesc.Format);
421 hr = D3DX11CreateTextureFromResource( dx->
GetDevice(), hSrcModule, pSrcResource, pLoadInfo, pPump, ( ID3D11Resource** )&unormStaging, NULL );
423 hr = dx->
GetDevice()->CreateTexture2D(&CopyDesc, NULL, &srgbStaging);
425 ID3D11Texture2D* srgbGPU;
427 pRes->GetDesc( &CopyDesc );
428 CopyDesc.Format =
MAKE_SRGB(CopyDesc.Format);
429 hr = dx->
GetDevice()->CreateTexture2D(&CopyDesc, NULL, &srgbGPU);
438 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
440 SRVDesc.Format =
MAKE_SRGB( ZeroInfo.Format );
442 SRVDesc.Format = ZeroInfo.Format;
443 if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE1D )
445 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
446 SRVDesc.Texture1D.MostDetailedMip = 0;
447 SRVDesc.Texture1D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
449 else if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE2D )
451 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
452 SRVDesc.Texture2D.MostDetailedMip = 0;
453 SRVDesc.Texture2D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
455 if( pLoadInfo->pSrcInfo->MiscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE )
457 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
458 SRVDesc.TextureCube.MostDetailedMip = 0;
459 SRVDesc.TextureCube.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
462 else if( pLoadInfo->pSrcInfo->ResourceDimension == D3D11_RESOURCE_DIMENSION_TEXTURE3D )
464 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
465 SRVDesc.Texture3D.MostDetailedMip = 0;
466 SRVDesc.Texture3D.MipLevels = pLoadInfo->pSrcInfo->MipLevels;
469 SRVDesc.Format =
MAKE_SRGB(tex_dsc.Format);
471 SRVDesc.Format = tex_dsc.Format;
473 SRVDesc.Texture2D.MipLevels = tex_dsc.MipLevels;
474 SRVDesc.Texture2D.MostDetailedMip = 0;
475 hr = dx->
GetDevice()->CreateShaderResourceView( pRes, &SRVDesc, ppOutputRV );
480 ( *ppOutputRV )->QueryInterface( __uuidof( ID3D11ShaderResourceView ), ( LPVOID* )&NewEntry.
pSRV11 );
482 m_TextureCache.Add(NewEntry);
static raResource & Instance()
ID3D11DeviceContext * GetImmediateContext(void)
HRESULT CreateTextureFromResourceEx(raSmartPointer< raDirectX > dx, HMODULE hSrcModule, LPCSTR pSrcResource, D3DX11_IMAGE_LOAD_INFO *pLoadInfo, ID3DX11ThreadPump *pPump, ID3D11ShaderResourceView **ppOutputRV, bool bSRGB=false)
ID3D11Device * GetDevice(void)
HRESULT CreateTextureFromMemoryEx(raSmartPointer< raDirectX > dx, LPVOID Data, size_t Size, D3DX11_IMAGE_LOAD_INFO *pLoadInfo, ID3DX11ThreadPump *pPump, ID3D11ShaderResourceView **ppOutputRV, bool bSRGB=false)
DXGI_FORMAT MAKE_SRGB(DXGI_FORMAT format)
HRESULT CreateTextureFromFileEx(raSmartPointer< raDirectX > dx, raString pSrcFile, D3DX11_IMAGE_LOAD_INFO *pLoadInfo, ID3DX11ThreadPump *pPump, ID3D11ShaderResourceView **ppOutputRV, bool bSRGB=false)
ID3D11ShaderResourceView * pSRV11