Skip to content

Commit

Permalink
Add render targets creation flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Hozar2002 committed Nov 30, 2024
1 parent 1e31aad commit b8f104c
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/Layers/xrRender/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ECORE_API CResourceManager
#endif //USE_DX11

#ifdef USE_DX11
CRT* _CreateRT (LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount = 1, bool useUAV=false );
CRT* _CreateRT (LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount = 1, CRT::CRTCreationFlags CreationFlags = (CRT::CRTCreationFlags)NULL);
#else
CRT* _CreateRT (LPCSTR Name, u32 w, u32 h, D3DFORMAT f, u32 SampleCount = 1 );
#endif
Expand Down
30 changes: 21 additions & 9 deletions src/Layers/xrRender/SH_RT.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@ class CRT : public xr_resource_named {
CRT();
~CRT();
#ifdef USE_DX11
void create(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount = 1, bool useUAV = false );
enum CRTCreationFlags
{
USE_UAV_FLAG = u32(1 << 0),
MIPPED_RT_FLAG = u32(1 << 1)
};
void create(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount = 1, CRT::CRTCreationFlags CreationFlags = (CRT::CRTCreationFlags)NULL);
#else
void create(LPCSTR Name, u32 w, u32 h, D3DFORMAT f, u32 SampleCount = 1 );
void create(LPCSTR Name, u32 w, u32 h, D3DFORMAT f, u32 SampleCount = 1);
#endif
void destroy();
void reset_begin();
void reset_end();
IC BOOL valid() { return !!pTexture; }

IC BOOL valid() {
return !!pTexture;
}

public:
ID3DTexture2D* pSurface;
ID3DRenderTargetView* pRT;

xr_vector<ID3DRenderTargetView*> pMippedRT;
#ifdef USE_DX11
ID3DDepthStencilView* pZRT;
ID3D11UnorderedAccessView* pUAView;
Expand All @@ -35,13 +45,15 @@ class CRT : public xr_resource_named {

u64 _order;
};
struct resptrcode_crt : public resptr_base<CRT>
{

struct resptrcode_crt : public resptr_base<CRT> {
#ifdef USE_DX11
void create (LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount = 1, bool useUAV = false );
void create(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount = 1, CRT::CRTCreationFlags CreationFlags = (CRT::CRTCreationFlags)NULL);
#else
void create (LPCSTR Name, u32 w, u32 h, D3DFORMAT f, u32 SampleCount = 1);
void create(LPCSTR Name, u32 w, u32 h, D3DFORMAT f, u32 SampleCount = 1);
#endif
void destroy () { _set(NULL); }
void destroy() {
_set(NULL);
}
};
typedef resptr_core<CRT,resptrcode_crt> ref_rt;
typedef resptr_core<CRT, resptrcode_crt> ref_rt;
4 changes: 2 additions & 2 deletions src/Layers/xrRenderDX10/dx11ResourceManager_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ void CResourceManager::_DeleteConstantTable (const R_constant_table* C)
}

//--------------------------------------------------------------------------------------------------------------
CRT* CResourceManager::_CreateRT(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount, bool useUAV )
CRT* CResourceManager::_CreateRT(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount, CRT::CRTCreationFlags CreationFlags)
{
R_ASSERT(Name && Name[0] && w && h);

Expand All @@ -468,7 +468,7 @@ CRT* CResourceManager::_CreateRT(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 Sa
CRT *RT = new CRT();
RT->dwFlags |= xr_resource_flagged::RF_REGISTERED;
m_rtargets.insert (std::make_pair(RT->set_name(Name),RT));
if (Device.b_is_Ready) RT->create (Name,w,h,f, SampleCount, useUAV );
if (Device.b_is_Ready) RT->create (Name,w,h,f, SampleCount, CreationFlags);
return RT;
}
}
Expand Down
236 changes: 141 additions & 95 deletions src/Layers/xrRenderDX10/dx11SH_RT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,157 +7,203 @@

#include "dx10TextureUtils.h"

CRT::CRT ()
{
pSurface = nullptr;
pRT = nullptr;
pZRT = nullptr;
pUAView = nullptr;
dwWidth = 0;
dwHeight = 0;
CRT::CRT() {
pSurface = nullptr;
pRT = nullptr;
pZRT = nullptr;
pUAView = nullptr;

dwWidth = 0;
dwHeight = 0;

fmt = DxgiFormat::DXGI_FORMAT_UNKNOWN;
pMippedRT.clear();
}
CRT::~CRT ()
{
destroy ();

CRT::~CRT() {
destroy();

// release external reference
DEV->_DeleteRT (this);
DEV->_DeleteRT(this);
}

void CRT::create (LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount, bool useUAV )
{
if (pSurface) return;
void CRT::create(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount, CRT::CRTCreationFlags CreationFlags) {
if(pSurface) return;

R_ASSERT (RDevice && Name && Name[0] && w && h);
_order = CPU::GetCLK();
R_ASSERT(RDevice && Name && Name[0] && w && h);
_order = CPU::GetCLK();

dwWidth = w;
dwHeight = h;
fmt = f;
dwWidth = w;
dwHeight = h;
fmt = f;

// Check width-and-height of render target surface
if (w > D3D_REQ_TEXTURE2D_U_OR_V_DIMENSION) return;
if (h > D3D_REQ_TEXTURE2D_U_OR_V_DIMENSION) return;
if(w > D3D_REQ_TEXTURE2D_U_OR_V_DIMENSION) return;
if(h > D3D_REQ_TEXTURE2D_U_OR_V_DIMENSION) return;

// Select usage
u32 usage = 0;
if (DxgiFormat::DXGI_FORMAT_D24_UNORM_S8_UINT == fmt) usage = D3DUSAGE_DEPTHSTENCIL;
else if (DxgiFormat::DXGI_FORMAT_R24_UNORM_X8_TYPELESS == fmt) usage = D3DUSAGE_DEPTHSTENCIL;
else if (DxgiFormat::DXGI_FORMAT_D16_UNORM == fmt) {
usage = D3DUSAGE_DEPTHSTENCIL;
fmt = DxgiFormat::DXGI_FORMAT_R16_TYPELESS;
}
else if (DxgiFormat::DXGI_FORMAT_D32_FLOAT == fmt) {
usage = D3DUSAGE_DEPTHSTENCIL;
fmt = DxgiFormat::DXGI_FORMAT_R32_TYPELESS;
u32 usage = D3DUSAGE_RENDERTARGET;

switch(fmt) {
case DxgiFormat::DXGI_FORMAT_R24G8_TYPELESS:
case DxgiFormat::DXGI_FORMAT_D24_UNORM_S8_UINT:
case DxgiFormat::DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
{
usage = D3DUSAGE_DEPTHSTENCIL;
break;
}
case DxgiFormat::DXGI_FORMAT_D16_UNORM:
{
fmt = DxgiFormat::DXGI_FORMAT_R16_TYPELESS;
usage = D3DUSAGE_DEPTHSTENCIL;
break;
}
case DxgiFormat::DXGI_FORMAT_D32_FLOAT:
{
fmt = DxgiFormat::DXGI_FORMAT_R32_TYPELESS;
usage = D3DUSAGE_DEPTHSTENCIL;
break;
}
}
else if (DxgiFormat::DXGI_FORMAT_R24G8_TYPELESS == fmt) usage = D3DUSAGE_DEPTHSTENCIL;
else usage = D3DUSAGE_RENDERTARGET;

DXGI_FORMAT dx10FMT = (DXGI_FORMAT)fmt;
bool bUseAsDepth = (usage == D3DUSAGE_RENDERTARGET) ? false : true;
bool bUseAsDepth = (usage != D3DUSAGE_RENDERTARGET);

// Try to create texture/surface
DEV->Evict ();
DEV->Evict();

// Create the render target texture
D3D_TEXTURE2D_DESC desc;
ZeroMemory( &desc, sizeof(desc) );
ZeroMemory(&desc, sizeof(desc));

desc.Width = dwWidth;
desc.Height = dwHeight;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = dx10FMT;
desc.SampleDesc.Count = SampleCount;
desc.Usage = D3D_USAGE_DEFAULT;
if( SampleCount <= 1 )
desc.BindFlags = D3D_BIND_SHADER_RESOURCE | (bUseAsDepth ? D3D_BIND_DEPTH_STENCIL : D3D_BIND_RENDER_TARGET);
else
{
desc.BindFlags = (bUseAsDepth ? D3D_BIND_DEPTH_STENCIL : (D3D_BIND_SHADER_RESOURCE | D3D_BIND_RENDER_TARGET));
}

if (!bUseAsDepth && SampleCount == 1 && useUAV )
desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
if(SampleCount <= 1) {
desc.BindFlags = D3D_BIND_SHADER_RESOURCE | (bUseAsDepth ? D3D_BIND_DEPTH_STENCIL : D3D_BIND_RENDER_TARGET);
}
else {
desc.BindFlags = bUseAsDepth ? D3D_BIND_DEPTH_STENCIL : (D3D_BIND_SHADER_RESOURCE | D3D_BIND_RENDER_TARGET);
}

CHK_DX( RDevice->CreateTexture2D( &desc, nullptr, &pSurface ) );
if(!bUseAsDepth) {
if(CreationFlags & CRTCreationFlags::MIPPED_RT_FLAG) {
auto dwSize = std::min(dwWidth, dwHeight);

while((dwSize /= 2) >= 4) {
++desc.MipLevels;
}
}

if(SampleCount == 1 && CreationFlags & CRTCreationFlags::USE_UAV_FLAG) {
desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
}

CHK_DX(RDevice->CreateTexture2D(&desc, nullptr, &pSurface));

// OK
#ifdef DEBUG
Msg ("* created RT(%s), %dx%d, format = %d samples = %d",Name,w,h, dx10FMT, SampleCount );
#endif // DEBUG
//R_CHK (pSurface->GetSurfaceLevel (0,&pRT));
if (bUseAsDepth)
{
Msg("* created RT(%s), %dx%d, format = %d samples = %d", Name, w, h, dx10FMT, SampleCount);
#endif

if(bUseAsDepth) {
D3D_DEPTH_STENCIL_VIEW_DESC ViewDesc;
ZeroMemory( &ViewDesc, sizeof(ViewDesc) );
ZeroMemory(&ViewDesc, sizeof(ViewDesc));

ViewDesc.Format = DXGI_FORMAT_UNKNOWN;
if( SampleCount <= 1 )
{
ViewDesc.Texture2D.MipSlice = 0;

if(SampleCount <= 1) {
ViewDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2D;
}
else
{
else {
ViewDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2DMS;
ViewDesc.Texture2DMS.UnusedField_NothingToDefine = 0;
}

ViewDesc.Texture2D.MipSlice = 0;
switch (desc.Format)
{
case DXGI_FORMAT_R24G8_TYPELESS:
switch(desc.Format) {
case DXGI_FORMAT_R24G8_TYPELESS:
ViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
break;
case DXGI_FORMAT_R32_TYPELESS:
case DXGI_FORMAT_R32_TYPELESS:
ViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
break;
}

CHK_DX( RDevice->CreateDepthStencilView( pSurface, &ViewDesc, &pZRT) );
CHK_DX(RDevice->CreateDepthStencilView(pSurface, &ViewDesc, &pZRT));
}
else
CHK_DX( RDevice->CreateRenderTargetView( pSurface, 0, &pRT ) );

if (!bUseAsDepth && SampleCount == 1 && useUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVDesc;
ZeroMemory( &UAVDesc, sizeof( D3D11_UNORDERED_ACCESS_VIEW_DESC ) );
UAVDesc.Format = dx10FMT;
UAVDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
UAVDesc.Buffer.FirstElement = 0;
UAVDesc.Buffer.NumElements = dwWidth * dwHeight;
CHK_DX( RDevice->CreateUnorderedAccessView( pSurface, &UAVDesc, &pUAView ) );
}
else {
D3D_RENDER_TARGET_VIEW_DESC descRTV{};
ZeroMemory(&descRTV, sizeof(descRTV));

pTexture = DEV->_CreateTexture (Name);
descRTV.Format = desc.Format;
descRTV.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
descRTV.Texture2D.MipSlice = 0;

CHK_DX(RDevice->CreateRenderTargetView(pSurface, &descRTV, &pRT));

if(SampleCount == 1) {
if(CreationFlags & CRTCreationFlags::USE_UAV_FLAG) {
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVDesc;
ZeroMemory(&UAVDesc, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC));

UAVDesc.Format = dx10FMT;
UAVDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D;
UAVDesc.Buffer.FirstElement = 0;
UAVDesc.Buffer.NumElements = dwWidth * dwHeight;

CHK_DX(RDevice->CreateUnorderedAccessView(pSurface, &UAVDesc, &pUAView));
}
}

pMippedRT.resize(desc.MipLevels);
pMippedRT[0] = pRT;

for(UINT mip_level = 1; mip_level < desc.MipLevels; ++mip_level) {
descRTV.Texture2D.MipSlice = mip_level;
CHK_DX(RDevice->CreateRenderTargetView(pSurface, &descRTV, &pMippedRT[mip_level]));
}
}

pTexture = DEV->_CreateTexture(Name);
pTexture->surface_set(pSurface);
}

void CRT::destroy ()
void CRT::destroy()
{
if (pTexture._get()) {
pTexture->surface_set (0);
pTexture = nullptr;
if(pTexture._get()) {
pTexture->surface_set(0);
pTexture = nullptr;
}
_RELEASE (pRT );
_RELEASE (pZRT );

_RELEASE (pSurface );
_RELEASE (pUAView);

_RELEASE(pRT);
_RELEASE(pZRT);

_RELEASE(pSurface);
_RELEASE(pUAView);

for(auto& MippedRT : pMippedRT) {
_RELEASE(MippedRT);
}

pMippedRT.clear();
}
void CRT::reset_begin ()
{
destroy ();

void CRT::reset_begin() {
destroy();
}
void CRT::reset_end ()
{
create (*cName,dwWidth,dwHeight,fmt);

void CRT::reset_end() {
create(*cName, dwWidth, dwHeight, fmt);
}

void resptrcode_crt::create(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount, bool useUAV )
{
_set (DEV->_CreateRT(Name,w,h,f, SampleCount, useUAV ));
void resptrcode_crt::create(LPCSTR Name, u32 w, u32 h, DxgiFormat f, u32 SampleCount, CRT::CRTCreationFlags CreationFlags) {
_set(DEV->_CreateRT(Name, w, h, f, SampleCount, CreationFlags));
}

//////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit b8f104c

Please sign in to comment.