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

Modificações em buffers

This commit is contained in:
Danilo 2024-04-12 15:21:00 -03:00
parent f920900519
commit 2dc806fb38
21 changed files with 194 additions and 62 deletions

View File

@ -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)

View File

@ -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<float>(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<float>(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<float>(cos(radians));
const auto num2 = static_cast<float>(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<float>(cos(radians));
const auto num2 = static_cast<float>(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<float>(cos(radians));
const auto num2 = static_cast<float>(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<float>(sin(angle));
const auto num2 = static_cast<float>(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<float>(1.0 / tan(fieldOfView * 0.5));
const auto num2 = num1 / aspectRatio;
Matrix perspectiveFieldOfView;

View File

@ -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);

View File

@ -87,6 +87,8 @@ namespace xna {
using PBlendState = std::shared_ptr<BlendState>;
class ConstantBuffer;
using PConstantBuffer = std::shared_ptr<ConstantBuffer>;
class DataBuffer;
using PDataBuffer = std::shared_ptr<DataBuffer>;
class DisplayMode;
using PDisplayMode = std::shared_ptr<DisplayMode>;
class DisplayModeCollection;

View File

@ -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

View File

@ -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

View File

@ -7,6 +7,7 @@ namespace xna {
class IShader {
public:
virtual ~IShader() {}
virtual bool Initialize(GraphicsDevice& device, DataBuffer& buffer, xna_error_nullarg) = 0;
};
}

View File

@ -7,6 +7,7 @@ namespace xna {
class IVertexInputLayout {
public:
virtual ~IVertexInputLayout(){}
virtual bool Initialize(GraphicsDevice& device, DataBuffer& blob, xna_error_nullarg) = 0;
};
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -0,0 +1,6 @@
#include "databuffer-dx.hpp"
namespace xna {
}

View File

@ -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

View File

@ -1,3 +1,4 @@
#include "dxgi.h"
#include "d3d11.h"
#include <d3dcompiler.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>

View File

@ -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);

View File

@ -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{};
};
}

View File

@ -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;
}

View File

@ -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;

View File

@ -16,7 +16,7 @@ namespace xna {
const auto hr = device._device->CreateBuffer(
&_description,
_useInitialData ? &_initialData : nullptr,
&_subResource,
&_buffer);
if (FAILED(hr)) {

View File

@ -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;
};
}

View File

@ -3,23 +3,29 @@
#include <d3dcompiler.h>
#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<UINT>(_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;
}

View File

@ -8,10 +8,11 @@
namespace xna {
class VertexInputLayout : public IVertexInputLayout {
public:
VertexInputLayout() = default;
VertexInputLayout(
GraphicsDevice* device,
std::vector<D3D11_INPUT_ELEMENT_DESC> 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<D3D11_INPUT_ELEMENT_DESC> _description{};
GraphicsDevice* _device = nullptr;
std::vector<D3D11_INPUT_ELEMENT_DESC> _description{};
};
}