PDA

View Full Version : [ C++ | Direct3D11] - Errori nel rilascio delle interfacce


zarko
12-07-2010, 18:50
Ciao a tutti.

Sto sviluppando lo scheletro di un motore grafico. Per comodità di sviluppo il codice nativo è ridotto all'osso, ho quindi una libreria scritta in C# che si interfaccia con Windows e Direct3D11 tramite una .dll scritta in C++.

I metodi per la creazione del device sono questi (le variabili sono dichiarate su un header a parte):


#define CHECK(hr) if (hr != S_OK) { return hr; }

HRESULT CreateRenderTargetView()
{
CHECK(pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer));
CHECK(pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView));
pBackBuffer->Release();

return S_OK;
}

HRESULT CreateDepthStencilView(int width, int height)
{
ID3D11Texture2D* pDepthBuffer = NULL;

D3D11_TEXTURE2D_DESC zDesc;
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;

zDesc.Width = width;
zDesc.Height = height;
zDesc.MipLevels = 1;
zDesc.ArraySize = 1;
zDesc.Format = DXGI_FORMAT_D32_FLOAT;
zDesc.SampleDesc.Count = 1;
zDesc.SampleDesc.Quality = 0;
zDesc.Usage = D3D11_USAGE_DEFAULT;
zDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
zDesc.CPUAccessFlags = 0;
zDesc.MiscFlags = 0;

dsvDesc.Flags=0;
dsvDesc.Format = DXGI_FORMAT_D32_FLOAT;
dsvDesc.ViewDimension= D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice=0;

CHECK(pDevice->CreateTexture2D(&zDesc, NULL, &pDepthBuffer));
CHECK(pDevice->CreateDepthStencilView(pDepthBuffer, &dsvDesc, &pRenderTargetDepth));
pDepthBuffer->Release();

return S_OK;
}

void SetViewPort(int width, int height)
{
D3D11_VIEWPORT vp;
vp.Width = (float)width;
vp.Height = (float)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;

pContext->OMSetRenderTargets(1, &pRenderTargetView, pRenderTargetDepth);
pContext->RSSetViewports(1, &vp);
}

extern "C" __declspec(dllexport) HRESULT CreateDevice()
{
DXGI_SWAP_CHAIN_DESC sc;
ZeroMemory(&sc, sizeof(sc));

RECT rect;
CHECK_BOOL(GetClientRect(HWnd, &rect), "Impossibile misurare le dimensioni della finestra.");
int width = rect.right;
int height = rect.bottom;

sc.BufferCount = 1;
sc.BufferDesc.Width = rect.right;
sc.BufferDesc.Height = rect.bottom;
sc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sc.BufferDesc.RefreshRate.Numerator = 60;
sc.BufferDesc.RefreshRate.Denominator = 1;
sc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sc.OutputWindow = HWnd;
sc.SampleDesc.Count = 1;
sc.SampleDesc.Quality = 0;
sc.Windowed = TRUE;

D3D_FEATURE_LEVEL requestedFL[1] = { D3D_FEATURE_LEVEL_11_0 };
D3D_FEATURE_LEVEL supportedFL[6];

CHECK(D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_DEBUG,
requestedFL,
1,
D3D11_SDK_VERSION,
&sc,
_ppSwapChain,
_ppDevice,
supportedFL,
_ppContext));

CHECK(CreateRenderTargetView());
CHECK(CreateDepthStencilView(width, height));
SetViewPort(width, height);

pContext->OMSetRenderTargets(1, &pRenderTargetView, pRenderTargetDepth);

D3D11_DEPTH_STENCIL_DESC DSDesc = D3D11_DEPTH_STENCIL_DESC();
DSDesc.DepthEnable = true;
DSDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
DSDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;

D3D11_RASTERIZER_DESC RDesc = D3D11_RASTERIZER_DESC();
RDesc.CullMode = D3D11_CULL_BACK;
RDesc.FillMode = D3D11_FILL_SOLID;

D3D11_SAMPLER_DESC SDesc = D3D11_SAMPLER_DESC();
SDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
SDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
SDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
SDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;

D3D11_BLEND_DESC BDesc = D3D11_BLEND_DESC();
BDesc.IndependentBlendEnable = false;
BDesc.RenderTarget[0].BlendEnable = false;
BDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

CHECK(pDevice->CreateRasterizerState(&RDesc, &pRasterState));
CHECK(pDevice->CreateDepthStencilState(&DSDesc, &pDepthState));
CHECK(pDevice->CreateSamplerState(&SDesc, &pSamplerState));
CHECK(pDevice->CreateBlendState(&BDesc, &pBlendState));

pContext->RSSetState(pRasterState);
pContext->OMSetDepthStencilState(pDepthState, 0);
pContext->OMSetBlendState(pBlendState, 0, 0xffffffff);

ID3D11SamplerState* samples[1] = { pSamplerState };
pContext->PSSetSamplers(0, 1, samples);
pContext->DSSetSamplers(0, 1, samples);

return S_OK;
}


Alla chiusura del programma invece viene chiamata questa funzione per rilasciare la memoria:


#define SAFE_RELEASE(x) if( x ) { (x)->Release(); (x) = NULL; }

extern "C" __declspec(dllexport) void DestroyDevice()
{
SAFE_RELEASE(pBlendState);
SAFE_RELEASE(pSamplerState);
SAFE_RELEASE(pDepthState);
SAFE_RELEASE(pRasterState);

SAFE_RELEASE(pRenderTargetDepth);
SAFE_RELEASE(pRenderTargetView);
SAFE_RELEASE(pSwapChain);
SAFE_RELEASE(pContext);
SAFE_RELEASE(pDevice);
}


Durante l'esecuzione tutto funziona bene, ma non riesco a interpretare l'output di debug:


D3D11: WARNING: Live Device: Name="unnamed", Addr=0x00226C58, ExtRef=1 [ STATE_CREATION WARNING #2097297: LIVE_DEVICE ]
D3D11: WARNING: Live Sampler: Name="unnamed", Addr=0x0022CB6C, ExtRef=0, IntRef=0 [ STATE_CREATION WARNING #2097268: LIVE_SAMPLER ]
D3D11: WARNING: Live DepthStencilState: Name="unnamed", Addr=0x0022CA24, ExtRef=0, IntRef=0 [ STATE_CREATION WARNING #2097274: LIVE_DEPTHSTENCILSTATE ]
D3D11: WARNING: Live RasterizerState: Name="unnamed", Addr=0x0022C8D4, ExtRef=0, IntRef=0 [ STATE_CREATION WARNING #2097277: LIVE_RASTERIZERSTATE ]
D3D11: WARNING: Live DepthStencilView: Name="unnamed", Addr=0x0022C72C, ExtRef=0, IntRef=0 [ STATE_CREATION WARNING #2097247: LIVE_DEPTHSTENCILVIEW ]
D3D11: WARNING: Live Texture2D: Name="unnamed", Addr=0x0022C51C, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097235: LIVE_TEXTURE2D ]
D3D11: WARNING: Live RenderTargetView: Name="unnamed", Addr=0x0022B334, ExtRef=0, IntRef=0 [ STATE_CREATION WARNING #2097244: LIVE_RENDERTARGETVIEW ]
D3D11: WARNING: Live Texture2D: Name="unnamed", Addr=0x0022AFBC, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097235: LIVE_TEXTURE2D ]
D3D11: WARNING: Live Query: Name="unnamed", Addr=0x04B5C7F4, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097280: LIVE_QUERY ]
D3D11: WARNING: Live Sampler: Name="unnamed", Addr=0x04B5C644, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097268: LIVE_SAMPLER ]
D3D11: WARNING: Live RasterizerState: Name="unnamed", Addr=0x04B5C4B4, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097277: LIVE_RASTERIZERSTATE ]
D3D11: WARNING: Live DepthStencilState: Name="unnamed", Addr=0x04B5C384, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097274: LIVE_DEPTHSTENCILSTATE ]
D3D11: WARNING: Live BlendState: Name="unnamed", Addr=0x04B5C1F4, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097271: LIVE_BLENDSTATE ]
D3D11: WARNING: Live Context: Name="unnamed", Addr=0x04B4006C, ExtRef=0, IntRef=1 [ STATE_CREATION WARNING #2097226: LIVE_CONTEXT ]
D3D11: WARNING: Live Device Child Summary: Device Addr=0x00226C58
Using ID3D11Debug::ReportLiveDeviceObjects with D3D11_RLDO_DETAIL will help drill into object lifetimes. Objects with ExtRef=0 and IntRef=0 will be eventually destroyed through typical Immediate Context usage. However, if the application requires these objects to be destroyed sooner, ClearState followed by Flush on the Immediate Context will realize their destruction.
Live Context: 1
Live Buffer: 0
Live Texture1D: 0
Live Texture2D: 2
Live Texture3D: 0
Live ShaderResourceView: 0
Live RenderTargetView: 1
Live DepthStencilView: 1
Live VertexShader: 0
Live GeometryShader: 0
Live PixelShader: 0
Live InputLayout: 0
Live Sampler: 2
Live BlendState: 1
Live DepthStencilState: 2
Live RasterizerState: 2
Live Query: 1
Live Predicate: 0
Live Counter: 0
Live CommandList: 0
Live HullShader: 0
Live DomainShader: 0
Live ClassInstance: 0
Live ClassLinkage: 0
Live ComputeShader: 0
Live UnorderedAccessView: 0
[ STATE_CREATION WARNING #2097298: LIVE_OBJECT_SUMMARY ]


Cosa vuol dire? C'è un errore nel rilascio delle risorse? Come faccio a rilasciarle correttamente? O è semplicemente un rapporto su quali oggetti sono stati creati?
Grazie in anticipo!

Zarko

fero86
12-07-2010, 19:10
ma se stai programmando in C# perché non usi DirectX Managed? :mbe:
e comunque in C++ ti converrebbe usare ATL, in particolare per tenere un riferimento ad un oggetto COM ti converrebbe usare CComPtr e compagnia.

zarko
12-07-2010, 19:23
ma se stai programmando in C# perché non usi DirectX Managed? :mbe:
e comunque in C++ ti converrebbe usare ATL, in particolare per tenere un riferimento ad un oggetto COM ti converrebbe usare CComPtr e compagnia.

Da quello che ho capito le MDX non esistono più da parecchie versioni dell'SDK, altri wrapper o XNA non mi entusiasmano poichè il codice nativo mi permetterebbe di utilizzare più avanti altre librerie, tra le quali physX (anch'esse esistono solo per C++). Il C# lo uso solo a un livello più alto: engine -> C++ (interfaccia con windows e direct3D) / gioco C# (più rapido da sviluppare a mio modo di vedere)

ATL e CComPtr mi sta diventando arabo...

Comunque quello che volevo capire è specifico di Direct3D11: cosa significa esattamente quell'output?

lock cmpxchg8b %ebx
12-07-2010, 19:47
Si, le DirectX Managed sono morte e sepolte.
Ci sarebbe http://slimdx.org/ comunque (supporta DX9/10/11, XAudio2 e altro).

zarko
13-07-2010, 10:28
Si, le DirectX Managed sono morte e sepolte.
Ci sarebbe http://slimdx.org/ comunque (supporta DX9/10/11, XAudio2 e altro).

Grazie, gli avevo già dato un'occhiata ma, come detto, preferisco rimanere in C++.

Altre idee su cosa sia quell output? Non riesco neppure a trovare la documentzione MS.. :help: