diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 5f6d11b..8bf3471 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -3,7 +3,7 @@ # # Add source to this project's executable. -add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizer-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp") +add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizer-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/common/matrix.cpp b/framework/common/matrix.cpp index 09815be..5aa447e 100644 --- a/framework/common/matrix.cpp +++ b/framework/common/matrix.cpp @@ -9,7 +9,7 @@ namespace xna { if (d < 9.9999997473787516E-05) result1 = cameraForwardVector ? -*cameraForwardVector : Vector3::Forward(); else - result1 = Vector3::Multiply(result1, 1.0f / sqrt(d)); + result1 = Vector3::Multiply(result1, static_cast(1.0 / sqrt(d))); Vector3 result2 = Vector3::Cross(cameraUpVector, result1); result2.Normalize(); @@ -45,7 +45,7 @@ namespace xna { if (d < 9.9999997473787516E-05) result1 = cameraForwardVector ? -*cameraForwardVector : Vector3::Forward(); else - result1 = Vector3::Multiply(result1, 1.0f / sqrt(d)); + result1 = Vector3::Multiply(result1, static_cast(1.0 / sqrt(d))); //Vector3 vector2 = rotateAxis; float result2 = Vector3::Dot(rotateAxis, result1); @@ -106,8 +106,8 @@ namespace xna { Matrix Matrix::CreateRotationX(float radians) { - const auto num1 = cos(radians); - const auto num2 = sin(radians); + const auto num1 = static_cast(cos(radians)); + const auto num2 = static_cast(sin(radians)); Matrix rotationX; rotationX.M11 = 1.0f; @@ -131,8 +131,8 @@ namespace xna { Matrix Matrix::CreateRotationY(float radians) { - const auto num1 = cos(radians); - const auto num2 = sin(radians); + const auto num1 = static_cast(cos(radians)); + const auto num2 = static_cast(sin(radians)); Matrix rotationY; rotationY.M11 = num1; rotationY.M12 = 0.0f; @@ -155,8 +155,8 @@ namespace xna { Matrix Matrix::CreateRotationZ(float radians) { - const auto num1 = cos(radians); - const auto num2 = sin(radians); + const auto num1 = static_cast(cos(radians)); + const auto num2 = static_cast(sin(radians)); Matrix rotationZ; rotationZ.M11 = num1; rotationZ.M12 = num2; @@ -182,8 +182,8 @@ namespace xna { const auto x = axis.X; const auto y = axis.Y; const auto z = axis.Z; - const auto num1 = sin(angle); - const auto num2 = cos(angle); + const auto num1 = static_cast(sin(angle)); + const auto num2 = static_cast(cos(angle)); const auto num3 = x * x; const auto num4 = y * y; const auto num5 = z * z; @@ -216,7 +216,7 @@ namespace xna { return Matrix(); } - const auto num1 = 1.0f / tan(fieldOfView * 0.5); + const auto num1 = static_cast(1.0 / tan(fieldOfView * 0.5)); const auto num2 = num1 / aspectRatio; Matrix perspectiveFieldOfView; diff --git a/framework/common/matrix.hpp b/framework/common/matrix.hpp index c503fb1..ea8de08 100644 --- a/framework/common/matrix.hpp +++ b/framework/common/matrix.hpp @@ -249,9 +249,9 @@ namespace xna { } Matrix perspectiveOffCenter; - perspectiveOffCenter.M11 = (2.0 * nearPlaneDistance / (right - left)); + perspectiveOffCenter.M11 = (2.0F * nearPlaneDistance / (right - left)); perspectiveOffCenter.M12 = perspectiveOffCenter.M13 = perspectiveOffCenter.M14 = 0.0f; - perspectiveOffCenter.M22 = (2.0 * nearPlaneDistance / (top - bottom)); + perspectiveOffCenter.M22 = (2.0F * nearPlaneDistance / (top - bottom)); perspectiveOffCenter.M21 = perspectiveOffCenter.M23 = perspectiveOffCenter.M24 = 0.0f; perspectiveOffCenter.M31 = (left + right) / (right - left); perspectiveOffCenter.M32 = (top + bottom) / (top - bottom); @@ -268,7 +268,7 @@ namespace xna { orthographic.M12 = orthographic.M13 = orthographic.M14 = 0.0f; orthographic.M22 = 2.0f / height; orthographic.M21 = orthographic.M23 = orthographic.M24 = 0.0f; - orthographic.M33 = (1.0 / (zNearPlane - zFarPlane)); + orthographic.M33 = (1.0F / (zNearPlane - zFarPlane)); orthographic.M31 = orthographic.M32 = orthographic.M34 = 0.0f; orthographic.M41 = orthographic.M42 = 0.0f; orthographic.M43 = zNearPlane / (zNearPlane - zFarPlane); diff --git a/framework/forward.hpp b/framework/forward.hpp index 16f1ac7..3e4e948 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -87,6 +87,8 @@ namespace xna { using PBlendState = std::shared_ptr; class ConstantBuffer; using PConstantBuffer = std::shared_ptr; + class DataBuffer; + using PDataBuffer = std::shared_ptr; class DisplayMode; using PDisplayMode = std::shared_ptr; class DisplayModeCollection; diff --git a/framework/graphics/constbuffer.hpp b/framework/graphics/constbuffer.hpp index e69de29..2dd94ac 100644 --- a/framework/graphics/constbuffer.hpp +++ b/framework/graphics/constbuffer.hpp @@ -0,0 +1,14 @@ +#ifndef XNA_GRAPHICS_CONSTBUFFER_HPP +#define XNA_GRAPHICS_CONSTBUFFER_HPP + +#include "../default.hpp" + +namespace xna { + class IConstantBuffer { + public: + virtual ~IConstantBuffer(){} + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/graphics/databuffer.hpp b/framework/graphics/databuffer.hpp new file mode 100644 index 0000000..95ba4d1 --- /dev/null +++ b/framework/graphics/databuffer.hpp @@ -0,0 +1,14 @@ +#ifndef XNA_GRAPHICS_DATABUFFER_HPP +#define XNA_GRAPHICS_DATABUFFER_HPP + +#include "../default.hpp" + +namespace xna { + class IDataBuffer { + public: + virtual ~IDataBuffer(){} + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/graphics/shader.hpp b/framework/graphics/shader.hpp index ce5eb5b..f6d0db7 100644 --- a/framework/graphics/shader.hpp +++ b/framework/graphics/shader.hpp @@ -7,6 +7,7 @@ namespace xna { class IShader { public: virtual ~IShader() {} + virtual bool Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_nullarg) = 0; }; } diff --git a/framework/graphics/vertexinput.hpp b/framework/graphics/vertexinput.hpp index 924d6d0..672e76d 100644 --- a/framework/graphics/vertexinput.hpp +++ b/framework/graphics/vertexinput.hpp @@ -7,6 +7,7 @@ namespace xna { class IVertexInputLayout { public: virtual ~IVertexInputLayout(){} + virtual bool Initialize(GraphicsDevice& device, DataBuffer& blob, xna_error_nullarg) = 0; }; } diff --git a/framework/platform/constbuffer-dx.cpp b/framework/platform/constbuffer-dx.cpp new file mode 100644 index 0000000..ab2a7a6 --- /dev/null +++ b/framework/platform/constbuffer-dx.cpp @@ -0,0 +1,29 @@ +#include "constbuffer-dx.hpp" +#include "device-dx.hpp" + +namespace xna { + bool ConstantBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg) + { + if (!device._device) { + xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL); + return false; + } + + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + + const auto hr = device._device->CreateBuffer( + &_description, + &_subResource, + &_buffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/framework/platform/constbuffer-dx.hpp b/framework/platform/constbuffer-dx.hpp new file mode 100644 index 0000000..b7a775d --- /dev/null +++ b/framework/platform/constbuffer-dx.hpp @@ -0,0 +1,28 @@ +#ifndef XNA_PLATFORM_CONSTBUFFER_DX_HPP +#define XNA_PLATFORM_CONSTBUFFER_DX_HPP + +#include "../graphics/constbuffer.hpp" +#include "../common/matrix.hpp" +#include "dxheaders.hpp" + +namespace xna { + class ConstantBuffer : public IConstantBuffer { + public: + virtual ~ConstantBuffer() override { + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + } + + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override; + + public: + D3D11_BUFFER_DESC _description{}; + D3D11_SUBRESOURCE_DATA _subResource{}; + ID3D11Buffer* _buffer = nullptr; + DirectX::XMMATRIX _worldViewProjection; + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/databuffer-dx.cpp b/framework/platform/databuffer-dx.cpp new file mode 100644 index 0000000..6dd3b24 --- /dev/null +++ b/framework/platform/databuffer-dx.cpp @@ -0,0 +1,6 @@ +#include "databuffer-dx.hpp" + +namespace xna { + + +} \ No newline at end of file diff --git a/framework/platform/databuffer-dx.hpp b/framework/platform/databuffer-dx.hpp new file mode 100644 index 0000000..8ce2a20 --- /dev/null +++ b/framework/platform/databuffer-dx.hpp @@ -0,0 +1,26 @@ +#ifndef XNA_PLATFORM_DATABUFFER_DX_HPP +#define XNA_PLATFORM_DATABUFFER_DX_HPP + +#include "../graphics/databuffer.hpp" +#include "dxheaders.hpp" + +namespace xna { + class DataBuffer : public IDataBuffer { + public: + virtual ~DataBuffer() override { + if (_blob) { + _blob->Release(); + _blob = nullptr; + } + } + + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override { + return false; + } + + public: + ID3DBlob* _blob = nullptr; + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/dxheaders.hpp b/framework/platform/dxheaders.hpp index 133be21..02ce37a 100644 --- a/framework/platform/dxheaders.hpp +++ b/framework/platform/dxheaders.hpp @@ -1,3 +1,4 @@ #include "dxgi.h" #include "d3d11.h" -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/framework/platform/indexbuffer-dx.cpp b/framework/platform/indexbuffer-dx.cpp index 529f423..67d5d2a 100644 --- a/framework/platform/indexbuffer-dx.cpp +++ b/framework/platform/indexbuffer-dx.cpp @@ -33,9 +33,9 @@ namespace xna { indices.push_back(i + 2); } - _initialData.pSysMem = &indices.front(); + _subResource.pSysMem = &indices.front(); - const auto hr = device._device->CreateBuffer(&_description, &_initialData, &_buffer); + const auto hr = device._device->CreateBuffer(&_description, &_subResource, &_buffer); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); diff --git a/framework/platform/indexbuffer-dx.hpp b/framework/platform/indexbuffer-dx.hpp index dd7bba5..e105270 100644 --- a/framework/platform/indexbuffer-dx.hpp +++ b/framework/platform/indexbuffer-dx.hpp @@ -9,8 +9,7 @@ namespace xna { public: IndexBuffer() { _description.Usage = D3D11_USAGE_DEFAULT; - _description.BindFlags = D3D11_BIND_INDEX_BUFFER; - + _description.BindFlags = D3D11_BIND_INDEX_BUFFER; } IndexBuffer(size_t size) { @@ -33,7 +32,7 @@ namespace xna { public: D3D11_BUFFER_DESC _description; ID3D11Buffer* _buffer = nullptr; - D3D11_SUBRESOURCE_DATA _initialData{}; + D3D11_SUBRESOURCE_DATA _subResource{}; }; } diff --git a/framework/platform/shader-dx.cpp b/framework/platform/shader-dx.cpp index 11c3adc..df04e59 100644 --- a/framework/platform/shader-dx.cpp +++ b/framework/platform/shader-dx.cpp @@ -1,8 +1,9 @@ #include "shader-dx.hpp" #include "device-dx.hpp" +#include "databuffer-dx.hpp" namespace xna { - HRESULT Shader::CompileFromFile(LPCWSTR srcFile, LPCSTR entryPoint, LPCSTR profile, ID3DBlob** blob) + HRESULT Shader::CompileFromFile(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob) { //https://learn.microsoft.com/en-us/windows/win32/direct3d11/how-to--compile-a-shader @@ -44,43 +45,54 @@ namespace xna { *blob = shaderBlob; return hr; - } + } - bool VertexShader::Initialize(ID3DBlob* blob, xna_error_ptr_arg) { - if (!_device || !blob) { + bool VertexShader::Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_ptr_arg) + { + if (!device._device || !buffer._blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - const auto hr = _device->_device->CreateVertexShader( - blob->GetBufferPointer(), - blob->GetBufferSize(), + if (_vertexShader) { + _vertexShader->Release(); + _vertexShader = nullptr; + } + + const auto hr = device._device->CreateVertexShader( + buffer._blob->GetBufferPointer(), + buffer._blob->GetBufferSize(), NULL, &_vertexShader); if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); return false; } return true; } - bool PixelShader::Initialize(ID3DBlob* blob, xna_error_ptr_arg) + bool PixelShader::Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_ptr_arg) { - if (!_device || !blob) { + if (!device._device || !buffer._blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - const auto hr = _device->_device->CreatePixelShader( - blob->GetBufferPointer(), - blob->GetBufferSize(), + if (_pixelShader) { + _pixelShader->Release(); + _pixelShader = nullptr; + } + + const auto hr = device._device->CreatePixelShader( + buffer._blob->GetBufferPointer(), + buffer._blob->GetBufferSize(), NULL, &_pixelShader); if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); return false; } diff --git a/framework/platform/shader-dx.hpp b/framework/platform/shader-dx.hpp index 6e31508..3a8e948 100644 --- a/framework/platform/shader-dx.hpp +++ b/framework/platform/shader-dx.hpp @@ -7,23 +7,18 @@ namespace xna { class Shader : public IShader { public: - Shader(GraphicsDevice* device) : _device(device) { - } + Shader() = default; virtual ~Shader() override {} - virtual bool Initialize(ID3DBlob* blob, xna_error_nullarg) = 0; + virtual bool Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_nullarg) override {} - static HRESULT CompileFromFile(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob); - - public: - GraphicsDevice* _device = nullptr; + static HRESULT CompileFromFile(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob); }; class VertexShader : public Shader { public: - VertexShader(GraphicsDevice* device) : - Shader(device) {} + VertexShader() = default; virtual ~VertexShader() override { if (_vertexShader) { @@ -32,7 +27,7 @@ namespace xna { } } - virtual bool Initialize(ID3DBlob* blob, xna_error_nullarg) override; + virtual bool Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_nullarg) override; public: ID3D11VertexShader* _vertexShader = nullptr; @@ -40,8 +35,7 @@ namespace xna { class PixelShader : public Shader { public: - PixelShader(GraphicsDevice* device) : - Shader(device) {} + PixelShader() = default; virtual ~PixelShader() override { if (_pixelShader) { @@ -50,7 +44,7 @@ namespace xna { } } - virtual bool Initialize(ID3DBlob* blob, xna_error_nullarg) override; + virtual bool Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_nullarg) override; public: ID3D11PixelShader* _pixelShader = nullptr; diff --git a/framework/platform/vertexbuffer-dx.cpp b/framework/platform/vertexbuffer-dx.cpp index fd37f62..ea69e2e 100644 --- a/framework/platform/vertexbuffer-dx.cpp +++ b/framework/platform/vertexbuffer-dx.cpp @@ -16,7 +16,7 @@ namespace xna { const auto hr = device._device->CreateBuffer( &_description, - _useInitialData ? &_initialData : nullptr, + &_subResource, &_buffer); if (FAILED(hr)) { diff --git a/framework/platform/vertexbuffer-dx.hpp b/framework/platform/vertexbuffer-dx.hpp index b906905..a9f84d5 100644 --- a/framework/platform/vertexbuffer-dx.hpp +++ b/framework/platform/vertexbuffer-dx.hpp @@ -34,8 +34,7 @@ namespace xna { public: D3D11_BUFFER_DESC _description{}; - D3D11_SUBRESOURCE_DATA _initialData{}; - bool _useInitialData{ false }; + D3D11_SUBRESOURCE_DATA _subResource{}; ID3D11Buffer* _buffer = nullptr; }; } diff --git a/framework/platform/vertexinput-dx.cpp b/framework/platform/vertexinput-dx.cpp index b1f7e0f..b570fb9 100644 --- a/framework/platform/vertexinput-dx.cpp +++ b/framework/platform/vertexinput-dx.cpp @@ -3,23 +3,29 @@ #include #include "dxgi.h" #include "d3d11.h" +#include "databuffer-dx.hpp" namespace xna { - bool VertexInputLayout::Initialize(ID3DBlob* blob, xna_error_ptr_arg){ - if (!_device || !blob) { + bool VertexInputLayout::Initialize(GraphicsDevice& device, DataBuffer& blob, xna_error_ptr_arg){ + if (!device._device || !blob._blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - const auto hr = _device->_device->CreateInputLayout( + if (_inputLayout) { + _inputLayout->Release(); + _inputLayout = nullptr; + } + + const auto hr = device._device->CreateInputLayout( _description.data(), static_cast(_description.size()), - blob->GetBufferPointer(), - blob->GetBufferSize(), + blob._blob->GetBufferPointer(), + blob._blob->GetBufferSize(), &_inputLayout); if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); return false; } diff --git a/framework/platform/vertexinput-dx.hpp b/framework/platform/vertexinput-dx.hpp index c97e379..c646e0c 100644 --- a/framework/platform/vertexinput-dx.hpp +++ b/framework/platform/vertexinput-dx.hpp @@ -8,10 +8,11 @@ namespace xna { class VertexInputLayout : public IVertexInputLayout { public: + VertexInputLayout() = default; + VertexInputLayout( - GraphicsDevice* device, std::vector const& description) : - _device(device), _description(description){} + _description(description){} virtual ~VertexInputLayout() override{ if (_inputLayout) { @@ -20,12 +21,11 @@ namespace xna { } } - virtual bool Initialize(ID3DBlob* blob, xna_error_nullarg); + virtual bool Initialize(GraphicsDevice& device, DataBuffer& blob, xna_error_nullarg); public: ID3D11InputLayout* _inputLayout{ nullptr }; - std::vector _description{}; - GraphicsDevice* _device = nullptr; + std::vector _description{}; }; }