2024-03-18 15:41:46 -03:00
|
|
|
|
#include "device-dx.hpp"
|
|
|
|
|
#include "window-dx.hpp"
|
|
|
|
|
#include "swapchain-dx.hpp"
|
|
|
|
|
#include "rendertarget-dx.hpp"
|
|
|
|
|
#include "adapter-dx.hpp"
|
2024-03-21 16:01:47 -03:00
|
|
|
|
#include "blendstate-dx.hpp"
|
2024-03-30 14:25:08 -03:00
|
|
|
|
#include "gdeviceinfo-dx.hpp"
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
|
|
|
|
namespace xna {
|
2024-03-21 16:01:47 -03:00
|
|
|
|
GraphicsDevice::GraphicsDevice() {
|
2024-03-18 15:41:46 -03:00
|
|
|
|
_blendState = BlendState::NonPremultiplied();
|
2024-04-01 11:29:32 -03:00
|
|
|
|
_adapter = GraphicsAdapter::DefaultAdapter();
|
2024-03-18 15:41:46 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 14:25:08 -03:00
|
|
|
|
GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) {
|
|
|
|
|
_adapter = info.Adapter();
|
|
|
|
|
_presentParameters = info.PresentationParameters();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GraphicsDevice::Initialize(GameWindow& gameWindow) {
|
|
|
|
|
if (_blendState == nullptr)
|
|
|
|
|
_blendState = BlendState::NonPremultiplied();
|
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
_createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
if (_device) {
|
|
|
|
|
_device->Release();
|
|
|
|
|
_device = nullptr;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
if (_context) {
|
|
|
|
|
_context->Release();
|
|
|
|
|
_context = nullptr;
|
2024-04-01 11:29:32 -03:00
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-01 11:29:32 -03:00
|
|
|
|
gameWindow.Size(_presentParameters.BackBufferWidth, _presentParameters.BackBufferHeight);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
const auto bounds = gameWindow.ClientBounds();
|
2024-04-01 11:29:32 -03:00
|
|
|
|
|
2024-03-18 15:41:46 -03:00
|
|
|
|
_viewport = xna::Viewport(0.0F, 0.0F,
|
|
|
|
|
static_cast<float>(bounds.Width),
|
|
|
|
|
static_cast<float>(bounds.Height),
|
|
|
|
|
0.0F, 1.F);
|
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
if (!createDevice())
|
|
|
|
|
return false;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
COLORREF color = gameWindow.Color();
|
|
|
|
|
_backgroundColor[0] = GetRValue(color) / 255.0f;
|
|
|
|
|
_backgroundColor[1] = GetGValue(color) / 255.0f;
|
|
|
|
|
_backgroundColor[2] = GetBValue(color) / 255.0f;
|
|
|
|
|
_backgroundColor[3] = 1.0f;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
|
|
|
|
if (!_swapChain)
|
|
|
|
|
_swapChain = New<xna::SwapChain>(this);
|
|
|
|
|
|
|
|
|
|
_swapChain->Initialize(gameWindow);
|
|
|
|
|
|
|
|
|
|
if (!_swapChain->Apply())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
IDXGIFactory1* dxgiFactory = nullptr;
|
|
|
|
|
if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&dxgiFactory))
|
|
|
|
|
return false;
|
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
if FAILED(dxgiFactory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER))
|
2024-03-18 15:41:46 -03:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!_renderTarget2D) {
|
|
|
|
|
_renderTarget2D = New<RenderTarget2D>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 14:06:12 -03:00
|
|
|
|
if (!_renderTarget2D->Bind())
|
2024-03-18 15:41:46 -03:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
D3D11_VIEWPORT view{};
|
|
|
|
|
view.TopLeftX = _viewport.X;
|
|
|
|
|
view.TopLeftY = _viewport.Y;
|
|
|
|
|
view.Width = _viewport.Width;
|
|
|
|
|
view.Height = _viewport.Height;
|
|
|
|
|
view.MinDepth = _viewport.MinDetph;
|
|
|
|
|
view.MaxDepth = _viewport.MaxDepth;
|
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
_context->RSSetViewports(1, &view);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-13 21:04:12 -03:00
|
|
|
|
_blendState->Apply(*this);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GraphicsDevice::Present() {
|
2024-03-21 16:01:47 -03:00
|
|
|
|
_swapChain->_swapChain->Present(_usevsync, NULL);
|
|
|
|
|
_context->OMSetRenderTargets(1, &_renderTarget2D->_renderTargetView, nullptr);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GraphicsDevice::GetSwapChainBackBuffer(ID3D11Texture2D*& texture2D) {
|
|
|
|
|
if FAILED(_swapChain->_swapChain->GetBuffer(0, __uuidof(texture2D), (void**)(&texture2D)))
|
|
|
|
|
return false;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
bool GraphicsDevice::createDevice() {
|
|
|
|
|
#if _DEBUG
|
|
|
|
|
_createDeviceFlags |= D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG;
|
2024-04-01 11:29:32 -03:00
|
|
|
|
#endif
|
2024-03-21 16:01:47 -03:00
|
|
|
|
|
|
|
|
|
if FAILED(
|
|
|
|
|
D3D11CreateDevice(
|
|
|
|
|
_adapter->_adapter,
|
|
|
|
|
D3D_DRIVER_TYPE_UNKNOWN,
|
|
|
|
|
NULL,
|
|
|
|
|
_createDeviceFlags,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
D3D11_SDK_VERSION,
|
|
|
|
|
&_device,
|
|
|
|
|
&_featureLevel,
|
|
|
|
|
&_context)) {
|
|
|
|
|
|
|
|
|
|
if FAILED(D3D11CreateDevice(
|
|
|
|
|
NULL,
|
|
|
|
|
D3D_DRIVER_TYPE_WARP,
|
|
|
|
|
NULL,
|
|
|
|
|
_createDeviceFlags,
|
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
D3D11_SDK_VERSION,
|
|
|
|
|
&_device,
|
|
|
|
|
&_featureLevel,
|
|
|
|
|
&_context))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
OutputDebugString("---> Usando Adaptador WARP: n<>o h<> suporte ao D3D11\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-03-21 16:01:47 -03:00
|
|
|
|
void GraphicsDevice::Clear() {
|
|
|
|
|
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
}
|
|
|
|
|
}
|