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-04-16 16:13:36 -03:00
|
|
|
|
#include "../common/color.hpp"
|
2024-04-23 16:11:17 -03:00
|
|
|
|
#include "gdevicemanager-dx.hpp"
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
|
|
|
|
namespace xna {
|
2024-04-26 10:13:00 -03:00
|
|
|
|
GraphicsDevice::GraphicsDevice() {
|
|
|
|
|
_adapter = GraphicsAdapter::DefaultAdapter();
|
|
|
|
|
_adapter->CurrentDisplayMode(SurfaceFormat::Color, GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight);
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) {
|
|
|
|
|
_adapter = info.Adapter();
|
|
|
|
|
_presentationParameters = info.PresentationParameters();
|
|
|
|
|
_adapter->CurrentDisplayMode(_presentationParameters.backBufferFormat, _presentationParameters.backBufferWidth, _presentationParameters.backBufferHeight);
|
|
|
|
|
}
|
2024-03-30 14:25:08 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
bool GraphicsDevice::Initialize(GameWindow& gameWindow) {
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
|
|
if (!createDevice()) return false;
|
2024-04-14 13:02:22 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&_factory);
|
|
|
|
|
if (FAILED(hr)) return false;
|
2024-04-14 13:02:22 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
const auto bounds = gameWindow.ClientBounds();
|
2024-04-14 13:02:22 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -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-30 14:25:08 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -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
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_swapChain = New<xna::SwapChain>(this);
|
|
|
|
|
_swapChain->Initialize();
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
hr = _factory->MakeWindowAssociation(gameWindow.WindowHandle(), DXGI_MWA_NO_ALT_ENTER);
|
|
|
|
|
if (FAILED(hr)) return false;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_renderTarget2D = New<RenderTarget2D>();
|
|
|
|
|
if (!_renderTarget2D->Initialize(*this)) return false;
|
2024-04-01 11:29:32 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_renderTarget2D->Apply(*this);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
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-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_context->RSSetViewports(1, &view);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_blendState = BlendState::NonPremultiplied();
|
|
|
|
|
_blendState->Bind(this);
|
|
|
|
|
_blendState->Apply();
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
bool GraphicsDevice::Present() {
|
|
|
|
|
const auto result = _swapChain->Present(_usevsync);
|
|
|
|
|
_context->OMSetRenderTargets(1, &_renderTarget2D->_renderTargetView, nullptr);
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
bool GraphicsDevice::createDevice() {
|
|
|
|
|
#if _DEBUG
|
|
|
|
|
_createDeviceFlags |= D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG;
|
|
|
|
|
#endif
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
if FAILED(
|
|
|
|
|
D3D11CreateDevice(
|
|
|
|
|
_adapter->dxadapter,
|
|
|
|
|
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");
|
|
|
|
|
}
|
2024-04-22 11:22:18 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
void GraphicsDevice::reset()
|
|
|
|
|
{
|
|
|
|
|
if (_device) {
|
|
|
|
|
_device->Release();
|
|
|
|
|
_device = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_context) {
|
|
|
|
|
_context->Release();
|
|
|
|
|
_context = nullptr;
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
if (_factory) {
|
|
|
|
|
_factory->Release();
|
|
|
|
|
_factory = nullptr;
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_blendState = nullptr;
|
|
|
|
|
_swapChain = nullptr;
|
|
|
|
|
_renderTarget2D = nullptr;
|
2024-03-18 15:41:46 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
void GraphicsDevice::Clear() {
|
|
|
|
|
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
void GraphicsDevice::Clear(Color const& color) {
|
|
|
|
|
const auto v4 = color.ToVector4();
|
2024-03-18 15:41:46 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_backgroundColor[0] = v4.X;
|
|
|
|
|
_backgroundColor[1] = v4.Y;
|
|
|
|
|
_backgroundColor[2] = v4.Z;
|
|
|
|
|
_backgroundColor[3] = v4.W;
|
2024-03-21 16:01:47 -03:00
|
|
|
|
|
2024-04-26 10:13:00 -03:00
|
|
|
|
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
|
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
}
|