1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementa ponteiros ComPtr

This commit is contained in:
Danilo 2024-06-25 17:06:37 -03:00
parent b983ebcf74
commit 49345ff7df
14 changed files with 143 additions and 382 deletions

View File

@ -12,14 +12,14 @@ namespace xna {
}
uptr<GraphicsAdapter> GraphicsAdapter::DefaultAdapter() {
IDXGIFactory1* pFactory = nullptr;
comptr<IDXGIFactory1> pFactory = nullptr;
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory))
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf()))
Exception::Throw(ExMessage::CreateComponent);
IDXGIAdapter1* pAdapter = nullptr;
comptr<IDXGIAdapter1> pAdapter = nullptr;
if (pFactory->EnumAdapters1(0, &pAdapter) != DXGI_ERROR_NOT_FOUND) {
if (pFactory->EnumAdapters1(0, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND) {
auto adp = unew<GraphicsAdapter>();
adp->impl->_index = 0;
@ -28,22 +28,19 @@ namespace xna {
return adp;
}
pFactory->Release();
pFactory = nullptr;
return nullptr;
}
void GraphicsAdapter::Adapters(std::vector<uptr<GraphicsAdapter>>& adapters) {
IDXGIFactory1* pFactory = nullptr;
comptr<IDXGIFactory1> pFactory = nullptr;
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory))
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)pFactory.GetAddressOf()))
Exception::Throw(ExMessage::CreateComponent);
IDXGIAdapter1* pAdapter = nullptr;
comptr<IDXGIAdapter1> pAdapter = nullptr;
UINT count = 0;
for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) {
for (; pFactory->EnumAdapters1(count, pAdapter.GetAddressOf()) != DXGI_ERROR_NOT_FOUND; ++count) {
auto adp = unew<GraphicsAdapter>();
adp->impl->_index = count;
@ -51,9 +48,6 @@ namespace xna {
adapters.push_back(std::move(adp));
}
pFactory->Release();
pFactory = nullptr;
}
String GraphicsAdapter::Description() const {
@ -141,7 +135,7 @@ namespace xna {
uptr<DisplayModeCollection> GraphicsAdapter::SupportedDisplayModes() const {
if (!impl->dxadapter) return nullptr;
const auto totalDisplay = getDisplayModesCount(impl->dxadapter);
const auto totalDisplay = getDisplayModesCount(impl->dxadapter.Get());
if (totalDisplay == 0)
return nullptr;

View File

@ -102,13 +102,12 @@ namespace xna {
}
if (impl->dxBlendState) {
impl->dxBlendState->Release();
impl->dxBlendState = nullptr;
}
const auto hr = m_device->impl->_device->CreateBlendState(
&impl->dxDescription,
&impl->dxBlendState);
impl->dxBlendState.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -127,7 +126,7 @@ namespace xna {
}
m_device->impl->_context->OMSetBlendState(
impl->dxBlendState,
impl->dxBlendState.Get(),
impl->blendFactor,
impl->sampleMask);

View File

@ -22,14 +22,13 @@ namespace xna {
}
if (impl->_buffer) {
impl->_buffer->Release();
impl->_buffer = nullptr;
}
const auto hr = m_device->impl->_device->CreateBuffer(
&impl->_description,
&impl->_subResource,
&impl->_buffer);
impl->_buffer.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -56,7 +55,6 @@ namespace xna {
}
if (impl->_blob) {
impl->_blob->Release();
impl->_blob = nullptr;
}
@ -80,7 +78,7 @@ namespace xna {
Exception::Throw(ExMessage::ApplyComponent);
}
m_device->impl->_context->IASetIndexBuffer(impl->dxBuffer, DXGI_FORMAT_R16_UINT, 0);
m_device->impl->_context->IASetIndexBuffer(impl->dxBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
return true;
}
@ -109,7 +107,7 @@ namespace xna {
UINT stride = impl->size;
UINT offset = 0;
m_device->impl->_context->IASetVertexBuffers(0, 1,
&impl->dxBuffer, &stride, &offset);
impl->dxBuffer.GetAddressOf(), &stride, &offset);
return true;
}
@ -132,7 +130,6 @@ namespace xna {
}
if (impl->_inputLayout) {
impl->_inputLayout->Release();
impl->_inputLayout = nullptr;
}
@ -141,7 +138,7 @@ namespace xna {
static_cast<UINT>(impl->_description.size()),
blob.impl->_blob->GetBufferPointer(),
blob.impl->_blob->GetBufferSize(),
&impl->_inputLayout);
impl->_inputLayout.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);

View File

@ -42,13 +42,12 @@ namespace xna {
}
if (impl->dxDepthStencil) {
impl->dxDepthStencil->Release();
impl->dxDepthStencil = nullptr;
}
const auto hr = m_device->impl->_device->CreateDepthStencilState(
&impl->dxDescription,
&impl->dxDepthStencil);
impl->dxDepthStencil.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -67,7 +66,7 @@ namespace xna {
Exception::Throw(ExMessage::UnintializedComponent);
}
m_device->impl->_context->OMSetDepthStencilState(impl->dxDepthStencil, 0);
m_device->impl->_context->OMSetDepthStencilState(impl->dxDepthStencil.Get(), 0);
return true;
}

View File

@ -26,16 +26,16 @@ namespace xna {
createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG;
#endif
auto hr = D3D11CreateDevice(
impl._adapter->impl->dxadapter,
impl._adapter->impl->dxadapter.Get(),
D3D_DRIVER_TYPE_UNKNOWN,
NULL,
createDeviceFlags,
NULL,
0,
D3D11_SDK_VERSION,
&impl._device,
impl._device.GetAddressOf(),
&impl._featureLevel,
&impl._context);
impl._context.GetAddressOf());
if (FAILED(hr)) {
OutputDebugString("---> Usando Adaptador WARP: não há suporte ao D3D11\n");
@ -48,9 +48,9 @@ namespace xna {
NULL,
0,
D3D11_SDK_VERSION,
&impl._device,
impl._device.GetAddressOf(),
&impl._featureLevel,
&impl._context);
impl._context.GetAddressOf());
if FAILED(hr)
Exception::Throw(ExMessage::CreateComponent);
@ -156,7 +156,10 @@ namespace xna {
if (!impl) return false;
const auto result = impl->_swapChain->Present(impl->_usevsync);
impl->_context->OMSetRenderTargets(1, &impl->_renderTarget2D->render_impl->_renderTargetView, nullptr);
impl->_context->OMSetRenderTargets(
1,
impl->_renderTarget2D->render_impl->_renderTargetView.GetAddressOf(),
nullptr);
return result;
}
@ -172,7 +175,7 @@ namespace xna {
impl->_backgroundColor[3] = v4.W;
impl->_context->ClearRenderTargetView(
impl->_renderTarget2D->render_impl->_renderTargetView,
impl->_renderTarget2D->render_impl->_renderTargetView.Get(),
impl->_backgroundColor);
}

View File

@ -18,9 +18,9 @@ namespace xna {
//UINT FXFlags,
0,
//ID3D11Device * pDevice,
device->impl->_device,
device->impl->_device.Get(),
//ID3DX11Effect * *ppEffect
&impl->dxEffect
impl->dxEffect.ReleaseAndGetAddressOf()
);
if FAILED(result)
@ -331,7 +331,7 @@ namespace xna {
if (!impl->dxPass)
throw std::runtime_error("EffectPass::Apply: The class was not initialized correctly");
const auto hr = impl->dxPass->Apply(0, impl->dxContext);
const auto hr = impl->dxPass->Apply(0, impl->dxContext.Get());
if FAILED(hr)
throw std::runtime_error("EffectPass::Apply: error to call Apply");
@ -399,14 +399,12 @@ namespace xna {
auto current = impl->dxTechnique->GetPassByIndex(i);
auto pass = snew<EffectPass>();
pass->impl->dxPass = current;
pass->impl->dxPass->AddRef();
pass->impl->dxPass.Attach(current);
current->Release();
current = nullptr;
pass->impl->dxContext = impl->dxContext;
pass->impl->dxContext->AddRef();
list[i] = pass;
}
@ -580,8 +578,7 @@ namespace xna {
break;
auto efparam = snew<EffectParameter>();
efparam->impl->dxVariable = el;
efparam->impl->dxVariable->AddRef();
efparam->impl->dxVariable.Attach(el);
el->Release();
el = nullptr;
@ -603,8 +600,7 @@ namespace xna {
break;
auto efparam = snew<EffectParameter>();
efparam->impl->dxVariable = member;
efparam->impl->dxVariable->AddRef();
efparam->impl->dxVariable.Attach(member);
member->Release();
member = nullptr;

View File

@ -22,13 +22,12 @@ namespace xna {
}
if (impl->dxRasterizerState) {
impl->dxRasterizerState->Release();
impl->dxRasterizerState = nullptr;
}
const auto hr = m_device->impl->_device->CreateRasterizerState(
&impl->dxDescription,
&impl->dxRasterizerState);
impl->dxRasterizerState.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -47,7 +46,7 @@ namespace xna {
Exception::Throw(ExMessage::UnintializedComponent);
}
m_device->impl->_context->RSSetState(impl->dxRasterizerState);
m_device->impl->_context->RSSetState(impl->dxRasterizerState.Get());
return true;
}

View File

@ -16,19 +16,14 @@ namespace xna {
bool RenderTarget2D::Initialize() {
if (!impl || !m_device || !m_device->impl->_device) {
Exception::Throw(ExMessage::InitializeComponent);
}
if (impl->dxTexture2D) {
impl->dxTexture2D->Release();
impl->dxTexture2D = nullptr;
}
}
if (!m_device->impl->_swapChain->impl->GetBackBuffer(impl->dxTexture2D))
return false;
auto& dxdevice = m_device->impl->_device;
const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D, NULL, &render_impl->_renderTargetView);
const auto hr = dxdevice->CreateRenderTargetView(impl->dxTexture2D.Get(), NULL, render_impl->_renderTargetView.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -46,7 +41,7 @@ namespace xna {
Exception::Throw(ExMessage::UnintializedComponent);
auto& context = m_device->impl->_context;
context->OMSetRenderTargets(1, &render_impl->_renderTargetView, nullptr);
context->OMSetRenderTargets(1, render_impl->_renderTargetView.GetAddressOf(), nullptr);
return true;
}
}

View File

@ -16,13 +16,12 @@ namespace xna {
}
if (impl->_samplerState) {
impl->_samplerState->Release();
impl->_samplerState = nullptr;
}
const auto hr = m_device->impl->_device->CreateSamplerState(
&impl->_description,
&impl->_samplerState);
impl->_samplerState.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -41,7 +40,7 @@ namespace xna {
Exception::Throw(ExMessage::UnintializedComponent);
}
m_device->impl->_context->PSSetSamplers(0, 1, &impl->_samplerState);
m_device->impl->_context->PSSetSamplers(0, 1, impl->_samplerState.GetAddressOf());
return true;
}
@ -62,7 +61,7 @@ namespace xna {
if (!current || !current->impl || !current->impl->_samplerState)
Exception::Throw(ExMessage::InvalidOperation);
states[i] = current->impl->_samplerState;
states[i] = current->impl->_samplerState.Get();
states[i]->AddRef();
}

View File

@ -68,15 +68,14 @@ namespace xna {
}
if (impl->_vertexShader) {
impl->_vertexShader->Release();
impl->_vertexShader = nullptr;
impl->_vertexShader.ReleaseAndGetAddressOf();
}
const auto hr = m_device->impl->_device->CreateVertexShader(
buffer.impl->_blob->GetBufferPointer(),
buffer.impl->_blob->GetBufferSize(),
NULL,
&impl->_vertexShader);
impl->_vertexShader.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -92,15 +91,14 @@ namespace xna {
}
if (impl->_pixelShader) {
impl->_pixelShader->Release();
impl->_pixelShader = nullptr;
impl->_pixelShader.ReleaseAndGetAddressOf();
}
const auto hr = m_device->impl->_device->CreatePixelShader(
buffer.impl->_blob->GetBufferPointer(),
buffer.impl->_blob->GetBufferSize(),
NULL,
&impl->_pixelShader);
impl->_pixelShader.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);

View File

@ -63,7 +63,7 @@ namespace xna {
impl = unew<PlatformImplementation>();
impl->_dxSpriteFont = unew<DxSpriteFont>(
//ID3D11ShaderResourceView* texture
texture->impl->dxShaderResource,
texture->impl->dxShaderResource.Get(),
//Glyph const* glyphs
dxGlyps.data(),
//size_t glyphCount
@ -130,7 +130,7 @@ namespace xna {
implementation = unew<PlatformImplementation>();
implementation->_dxspriteBatch = snew<DxSpriteBatch>(
//ID3D11DeviceContext* deviceContext
device->impl->_context
device->impl->_context.Get()
);
Viewport(device->Viewport());
@ -154,10 +154,10 @@ namespace xna {
implementation->_dxspriteBatch->Begin(
sort,
blendState ? blendState->impl->dxBlendState : nullptr,
samplerState ? samplerState->impl->_samplerState : nullptr,
depthStencil ? depthStencil->impl->dxDepthStencil : nullptr,
rasterizerState ? rasterizerState->impl->dxRasterizerState : nullptr,
blendState ? blendState->impl->dxBlendState.Get() : nullptr,
samplerState ? samplerState->impl->_samplerState.Get() : nullptr,
depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr,
rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr,
nullptr,
matrix
);
@ -182,7 +182,7 @@ namespace xna {
XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
implementation->_dxspriteBatch->Draw(
texture.impl->dxShaderResource,
texture.impl->dxShaderResource.Get(),
_position,
_color
);
@ -209,7 +209,7 @@ namespace xna {
};
implementation->_dxspriteBatch->Draw(
texture.impl->dxShaderResource,
texture.impl->dxShaderResource.Get(),
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color);
@ -239,7 +239,7 @@ namespace xna {
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
implementation->_dxspriteBatch->Draw(
texture.impl->dxShaderResource,
texture.impl->dxShaderResource.Get(),
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color,
@ -275,7 +275,7 @@ namespace xna {
const XMFLOAT2 _scale = { scale.X, scale.Y };
implementation->_dxspriteBatch->Draw(
texture.impl->dxShaderResource,
texture.impl->dxShaderResource.Get(),
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color,
@ -302,7 +302,7 @@ namespace xna {
const auto v4 = color.ToVector4();
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, _color);
implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) {
@ -330,7 +330,7 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
};
implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
@ -362,7 +362,7 @@ namespace xna {
const auto _effects = static_cast<DxSpriteEffects>(effects);
implementation->_dxspriteBatch->Draw(
texture.impl->dxShaderResource,
texture.impl->dxShaderResource.Get(),
_destinationRect,
sourceRectangle ? &_sourceRect : nullptr,
_color,

View File

@ -16,13 +16,12 @@ namespace xna {
impl = nullptr;
}
static bool internalInit(GraphicsDevice& device, HWND windowHandle, IDXGISwapChain1*& swapChain, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fdesc) {
static bool internalInit(GraphicsDevice& device, HWND windowHandle, comptr<IDXGISwapChain1>& swapChain, DXGI_SWAP_CHAIN_DESC1 const& desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC const& fdesc) {
if (!device.impl->_device || !windowHandle)
return false;
if (swapChain) {
swapChain->Release();
swapChain = nullptr;
swapChain.ReleaseAndGetAddressOf();
}
auto adapter = device.Adapter();
@ -39,12 +38,12 @@ namespace xna {
if (FAILED(hr)) return false;
dxFactory2->CreateSwapChainForHwnd(
device.impl->_device,
device.impl->_device.Get(),
windowHandle,
&desc,
&fdesc,
nullptr,
&swapChain);
swapChain.GetAddressOf());
return true;

View File

@ -9,44 +9,30 @@ namespace xna {
{
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
ID3D11Resource* resource = nullptr;
comptr<ID3D11Resource> resource = nullptr;
auto wstr = XnaHelper::ToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile(
device.impl->_device,
device.impl->_context,
device.impl->_device.Get(),
device.impl->_context.Get(),
wstr.c_str(),
&resource,
&texture2d->impl->dxShaderResource,
resource.GetAddressOf(),
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(),
0U);
if (FAILED(result))
{
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(result)) {
return nullptr;
}
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D);
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(result)) {
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc;
resource->Release();
resource = nullptr;
texture2d->impl->dxDescription = desc;
return texture2d;
}
@ -57,25 +43,20 @@ namespace xna {
Exception::Throw(ExMessage::InitializeComponent);
}
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D);
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
}
ID3D11Resource* resource = nullptr;
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
comptr<ID3D11Resource> resource = nullptr;
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::InvalidOperation);
}
hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource);
if (resource) {
resource->Release();
resource = nullptr;
}
hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, &impl->dxShaderResource);
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -128,35 +109,25 @@ namespace xna {
HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)
{
if (!impl.dxTexture2D) {
auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D);
auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
}
}
ID3D11Resource* resource = nullptr;
auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
comptr<ID3D11Resource> resource = nullptr;
auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::InvalidOperation);
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
device.impl->_context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
if (impl.dxShaderResource) {
impl.dxShaderResource->Release();
impl.dxShaderResource = nullptr;
}
device.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
hr = device.impl->_device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource);
if (resource) {
resource->Release();
resource = nullptr;
}
hr = device.impl->_device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -216,15 +187,15 @@ namespace xna {
}
if (!impl->dxTexture2D) {
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D);
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
}
}
ID3D11Resource* resource = nullptr;
auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
comptr<ID3D11Resource> resource = nullptr;
auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::InvalidOperation);
@ -233,30 +204,20 @@ namespace xna {
D3D11_BOX box{};
if (rect) {
box.left = rect->X;
box.right = rect->X + rect->Width;
box.top = rect->Y;
box.bottom = rect->Y + rect->Height;
box.left = rect->Left();
box.right = rect->Right();
box.top = rect->Top();
box.bottom = rect->Bottom();
box.back = level;
box.front = 0;
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
m_device->impl->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
if (impl->dxShaderResource) {
impl->dxShaderResource->Release();
impl->dxShaderResource = nullptr;
}
m_device->impl->_context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
impl->dxShaderDescription.Format = impl->dxDescription.Format;
impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels;
hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource);
if (resource) {
resource->Release();
resource = nullptr;
}
hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, impl->dxShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -286,34 +247,24 @@ namespace xna {
{
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
ID3D11Resource* resource = nullptr;
comptr<ID3D11Resource> resource = nullptr;
auto hr = DirectX::CreateWICTextureFromMemory(
device.impl->_device,
device.impl->_context,
device.impl->_device.Get(),
device.impl->_context.Get(),
data.data(),
data.size(),
&resource,
&texture2d->impl->dxShaderResource);
resource.GetAddressOf(),
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr))
{
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D);
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
@ -321,9 +272,6 @@ namespace xna {
texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc;
resource->Release();
resource = nullptr;
return texture2d;
}

View File

@ -512,14 +512,8 @@ namespace xna {
};
struct GraphicsAdapter::PlatformImplementation {
~PlatformImplementation() {
if (dxadapter) {
dxadapter->Release();
dxadapter = nullptr;
}
}
comptr<IDXGIAdapter1> dxadapter = nullptr;
IDXGIAdapter1* dxadapter = nullptr;
private:
friend class GraphicsAdapter;
Uint _index{ 0 };
@ -543,57 +537,29 @@ namespace xna {
};
struct BlendState::PlatformImplementation {
~PlatformImplementation() {
if (dxBlendState) {
dxBlendState->Release();
dxBlendState = nullptr;
}
}
ID3D11BlendState* dxBlendState = nullptr;
comptr<ID3D11BlendState> dxBlendState = nullptr;
D3D11_BLEND_DESC dxDescription{};
float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F };
UINT sampleMask{ 0xffffffff };
};
struct ConstantBuffer::PlatformImplementation {
~PlatformImplementation() {
if (_buffer) {
_buffer->Release();
_buffer = nullptr;
}
}
D3D11_BUFFER_DESC _description{};
D3D11_SUBRESOURCE_DATA _subResource{};
ID3D11Buffer* _buffer = nullptr;
comptr<ID3D11Buffer> _buffer = nullptr;
DirectX::XMMATRIX _worldViewProjection{};
};
struct DataBuffer::PlatformImplementation {
~PlatformImplementation() {
if (_blob) {
_blob->Release();
_blob = nullptr;
}
}
comptr<ID3DBlob> _blob = nullptr;
ID3DBlob* _blob = nullptr;
void Set(ID3DBlob*& blob) {
void Set(comptr<ID3DBlob> const& blob) {
_blob = blob;
}
};
struct DepthStencilState::PlatformImplementation {
~PlatformImplementation() {
if (dxDepthStencil) {
dxDepthStencil->Release();
dxDepthStencil = nullptr;
}
}
ID3D11DepthStencilState* dxDepthStencil = nullptr;
comptr<ID3D11DepthStencilState> dxDepthStencil = nullptr;
D3D11_DEPTH_STENCIL_DESC dxDescription{};
};
@ -646,14 +612,7 @@ namespace xna {
};
struct IndexBuffer::PlatformImplementation {
~PlatformImplementation() {
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
}
}
ID3D11Buffer* dxBuffer = nullptr;
comptr<ID3D11Buffer> dxBuffer = nullptr;
};
struct Keyboard::PlatformImplementation {
@ -675,126 +634,58 @@ namespace xna {
};
struct RasterizerState::PlatformImplementation {
~PlatformImplementation() {
if (dxRasterizerState) {
dxRasterizerState->Release();
dxRasterizerState = nullptr;
}
}
ID3D11RasterizerState* dxRasterizerState = nullptr;
comptr<ID3D11RasterizerState> dxRasterizerState = nullptr;
D3D11_RASTERIZER_DESC dxDescription{};
};
struct SamplerState::PlatformImplementation {
~PlatformImplementation() {
if (_samplerState) {
_samplerState->Release();
_samplerState = nullptr;
}
}
ID3D11SamplerState* _samplerState = nullptr;
comptr<ID3D11SamplerState> _samplerState = nullptr;
D3D11_SAMPLER_DESC _description{};
};
struct VertexShader::PlatformImplementation {
~PlatformImplementation() {
if (_vertexShader) {
_vertexShader->Release();
_vertexShader = nullptr;
}
}
ID3D11VertexShader* _vertexShader = nullptr;
comptr<ID3D11VertexShader> _vertexShader = nullptr;
};
struct PixelShader::PlatformImplementation {
~PlatformImplementation() {
if (_pixelShader) {
_pixelShader->Release();
_pixelShader = nullptr;
}
}
ID3D11PixelShader* _pixelShader = nullptr;
comptr<ID3D11PixelShader> _pixelShader = nullptr;
};
struct SwapChain::PlatformImplementation {
~PlatformImplementation() {
if (dxSwapChain) {
dxSwapChain->Release();
dxSwapChain = nullptr;
}
}
IDXGISwapChain1* dxSwapChain{ nullptr };
comptr<IDXGISwapChain1> dxSwapChain{ nullptr };
DXGI_SWAP_CHAIN_DESC1 dxDescription{};
DXGI_SWAP_CHAIN_FULLSCREEN_DESC dxFullScreenDescription{};
bool GetBackBuffer(ID3D11Texture2D*& texture2D) {
bool GetBackBuffer(comptr<ID3D11Texture2D>& texture2D) {
if (!dxSwapChain)
return false;
const auto hr = dxSwapChain->GetBuffer(0, __uuidof(texture2D), (void**)(&texture2D));
const auto hr = dxSwapChain->GetBuffer(0, __uuidof(texture2D), (void**)texture2D.GetAddressOf());
return !FAILED(hr);
}
};
struct Texture2D::PlatformImplementation {
~PlatformImplementation() {
if (dxTexture2D) {
dxTexture2D->Release();
dxTexture2D = nullptr;
}
if (dxShaderResource) {
dxShaderResource->Release();
dxShaderResource = nullptr;
}
}
ID3D11Texture2D* dxTexture2D{ nullptr };
ID3D11ShaderResourceView* dxShaderResource{ nullptr };
comptr<ID3D11Texture2D> dxTexture2D{ nullptr };
comptr<ID3D11ShaderResourceView> dxShaderResource{ nullptr };
D3D11_SUBRESOURCE_DATA dxSubResource{};
D3D11_TEXTURE2D_DESC dxDescription{};
D3D11_SHADER_RESOURCE_VIEW_DESC dxShaderDescription{};
};
struct RenderTarget2D::PlatformImplementation {
~PlatformImplementation() {
if (_renderTargetView) {
_renderTargetView->Release();
_renderTargetView = nullptr;
}
}
ID3D11RenderTargetView* _renderTargetView = nullptr;
struct RenderTarget2D::PlatformImplementation {
comptr<ID3D11RenderTargetView> _renderTargetView = nullptr;
D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{};
};
struct VertexBuffer::PlatformImplementation {
~PlatformImplementation() {
if (dxBuffer) {
dxBuffer->Release();
dxBuffer = nullptr;
}
}
ID3D11Buffer* dxBuffer = nullptr;
comptr<ID3D11Buffer> dxBuffer = nullptr;
UINT size{ 0 };
};
struct VertexInputLayout::PlatformImplementation {
~PlatformImplementation() {
if (_inputLayout) {
_inputLayout->Release();
_inputLayout = nullptr;
}
}
ID3D11InputLayout* _inputLayout{ nullptr };
comptr<ID3D11InputLayout> _inputLayout{ nullptr };
std::vector<D3D11_INPUT_ELEMENT_DESC> _description{};
};
@ -934,29 +825,12 @@ namespace xna {
_depthStencilState = xna::DepthStencilState::Default();
_rasterizerState = xna::RasterizerState::CullCounterClockwise();
_samplerStates = snew<SamplerStateCollection>();
}
~PlatformImplementation() {
if (_device) {
_device->Release();
_device = nullptr;
}
if (_context) {
_context->Release();
_device = nullptr;
}
if (_factory) {
_factory->Release();
_factory = nullptr;
}
}
}
public:
ID3D11Device* _device = nullptr;
ID3D11DeviceContext* _context = nullptr;
IDXGIFactory1* _factory = nullptr;
comptr<ID3D11Device> _device = nullptr;
comptr<ID3D11DeviceContext> _context = nullptr;
comptr<IDXGIFactory1> _factory = nullptr;
PBlendState _blendState = nullptr;
PRasterizerState _rasterizerState = nullptr;
@ -990,84 +864,29 @@ namespace xna {
};
struct SoundEffect::PlatformImplementation {
~PlatformImplementation() {
}
uptr<DirectX::SoundEffect> _dxSoundEffect = nullptr;
};
struct Effect::PlatformImplementation {
~PlatformImplementation() {
if (dxEffect) {
dxEffect->Release();
dxEffect = nullptr;
}
}
ID3DX11Effect* dxEffect = nullptr;
comptr<ID3DX11Effect> dxEffect = nullptr;
};
struct EffectAnnotation::PlatformImplementation {
~PlatformImplementation() {
if (dxVariable) {
dxVariable->Release();
dxVariable = nullptr;
}
}
ID3DX11EffectVariable* dxVariable = nullptr;
comptr<ID3DX11EffectVariable> dxVariable = nullptr;
};
struct EffectPass::PlatformImplementation {
~PlatformImplementation() {
if (dxPass) {
dxPass->Release();
dxPass = nullptr;
}
if (dxContext) {
dxContext->Release();
dxContext = nullptr;
}
}
ID3DX11EffectPass* dxPass = nullptr;
ID3D11DeviceContext* dxContext = nullptr;
comptr<ID3DX11EffectPass> dxPass = nullptr;
comptr<ID3D11DeviceContext> dxContext = nullptr;
};
struct EffectTechnique::PlatformImplementation {
~PlatformImplementation() {
if (dxTechnique) {
dxTechnique->Release();
dxTechnique = nullptr;
}
if (dxContext) {
dxContext->Release();
dxContext = nullptr;
}
}
ID3DX11EffectTechnique* dxTechnique = nullptr;
ID3D11DeviceContext* dxContext = nullptr;
comptr<ID3DX11EffectTechnique> dxTechnique = nullptr;
comptr<ID3D11DeviceContext> dxContext = nullptr;
};
struct EffectParameter::PlatformImplementation {
PlatformImplementation(){}
PlatformImplementation(ID3DX11EffectVariable* value) {
dxVariable = value;
dxVariable->AddRef();
}
~PlatformImplementation() {
if (dxVariable) {
dxVariable->Release();
dxVariable = nullptr;
}
}
ID3DX11EffectVariable* dxVariable = nullptr;
comptr<ID3DX11EffectVariable> dxVariable = nullptr;
};
template <typename T>
@ -1076,7 +895,17 @@ namespace xna {
Exception::Throw(ExMessage::InitializeComponent);
}
const auto hr = DirectX::CreateStaticBuffer(m_device->impl->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_INDEX_BUFFER, &impl->dxBuffer);
if (impl->dxBuffer) {
impl->dxBuffer = nullptr;
}
const auto hr = DirectX::CreateStaticBuffer(
m_device->impl->_device.Get(),
data.data(),
data.size(),
sizeof(T),
D3D11_BIND_INDEX_BUFFER,
impl->dxBuffer.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
@ -1091,7 +920,13 @@ namespace xna {
Exception::Throw(ExMessage::InitializeComponent);
}
const auto hr = DirectX::CreateStaticBuffer(m_device->impl->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &impl->dxBuffer);
const auto hr = DirectX::CreateStaticBuffer(
m_device->impl->_device.Get(),
data.data(),
data.size(),
sizeof(T),
D3D11_BIND_VERTEX_BUFFER,
impl->dxBuffer.GetAddressOf());
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);