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

292 lines
9.3 KiB
C++
Raw Permalink Normal View History

2024-09-06 22:23:32 -03:00
#include "xna-dx/framework.hpp"
2024-03-18 15:41:46 -03:00
2024-05-04 21:07:39 -03:00
namespace xna {
HRESULT Texture2DImplementation::SetData(GraphicsDevice& device, UINT const* data) {
if (!Texture2D) {
auto hr = device.Implementation->Device->CreateTexture2D(&Description, nullptr, Texture2D.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
return hr;
}
}
comptr<ID3D11Resource> resource = nullptr;
auto hr = Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
if (FAILED(hr)) {
return hr;
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
device.Implementation->Context->UpdateSubresource(resource.Get(), 0, nullptr, data, Description.Width * R8G8B8A8U_BYTE_SIZE, 0);
ShaderDescription.Texture2D.MipLevels = Description.MipLevels;
hr = device.Implementation->Device->CreateShaderResourceView(resource.Get(), &ShaderDescription, ShaderResource.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
return hr;
}
Texture2D->GetDesc(&Description);
return NO_ERROR;
}
2024-05-04 21:07:39 -03:00
Texture2D::Texture2D() : Texture(nullptr) {
Implementation = unew<Texture2DImplementation>();
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device), width(width), height(height) {
Implementation = unew<Texture2DImplementation>();
Implementation->Description.Width = static_cast<UINT>(this->width);
Implementation->Description.Height = static_cast<UINT>(this->height);
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : Texture(device) {
Implementation = unew<Texture2DImplementation>();
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format)
: Texture(device), width(width), height(height), levelCount(mipMap) {
Implementation = unew<Texture2DImplementation>();
Implementation->Description.Width = static_cast<UINT>(this->width);
Implementation->Description.Height = static_cast<UINT>(this->height);
Implementation->Description.MipLevels = static_cast<UINT>(this->levelCount);
Implementation->Description.Format = DxHelpers::SurfaceFormatToDx(format);
}
void Texture2D::Initialize()
2024-05-04 21:07:39 -03:00
{
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
throw csharp::InvalidOperationException();
}
2024-05-04 21:07:39 -03:00
auto& deviceImpl = BaseGraphicsDevice->Implementation;
2024-05-04 21:07:39 -03:00
HRESULT hr = 0;
if (!Implementation->Texture2D) {
hr = deviceImpl->Device->CreateTexture2D(
&Implementation->Description,
nullptr,
Implementation->Texture2D.ReleaseAndGetAddressOf());
if FAILED(hr)
throw csharp::InvalidOperationException();
}
else {
//Updates description if texture is not null
Implementation->Texture2D->GetDesc(&Implementation->Description);
2024-05-04 21:07:39 -03:00
}
2024-06-25 17:06:37 -03:00
comptr<ID3D11Resource> resource = nullptr;
hr = Implementation->Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
2024-05-04 21:07:39 -03:00
if FAILED(hr)
throw csharp::InvalidOperationException();
//Only initializes if it is a ShaderResource
if (Implementation->Description.BindFlags & D3D11_BIND_SHADER_RESOURCE) {
hr = deviceImpl->Device->CreateShaderResourceView(
resource.Get(),
&Implementation->ShaderDescription,
Implementation->ShaderResource.ReleaseAndGetAddressOf());
2024-05-04 21:07:39 -03:00
if FAILED(hr)
throw csharp::InvalidOperationException();
}
2024-05-04 21:07:39 -03:00
surfaceFormat = DxHelpers::SurfaceFormatToXna(Implementation->Description.Format);
levelCount = static_cast<Int>(Implementation->ShaderDescription.Texture2D.MipLevels);
width = static_cast<Int>(Implementation->Description.Width);
height = static_cast<Int>(Implementation->Description.Height);
}
2024-05-05 15:50:17 -03:00
void Texture2D::SetData(std::vector<Uint> const& data, size_t startIndex, size_t elementCount)
2024-05-05 15:50:17 -03:00
{
if (!Implementation || !BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
auto hr = Implementation->SetData(*BaseGraphicsDevice, data.data());
if (FAILED(hr))
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
void Texture2D::SetData(std::vector<Byte> const& data, size_t startIndex, size_t elementCount)
2024-05-05 15:50:17 -03:00
{
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
std::vector<UINT> finalData(elementCount / 4);
auto fIndex = 0;
for (size_t i = startIndex; i < elementCount; ++i) {
const auto& r = data[i];
const auto& g = data[++i];
const auto& b = data[++i];
const auto& a = data[++i];
finalData[fIndex] = Color(r, g, b, a);
++fIndex;
}
auto hr = Implementation->SetData(*BaseGraphicsDevice, finalData.data());
if (FAILED(hr))
throw csharp::InvalidOperationException();
}
2024-05-05 15:50:17 -03:00
void Texture2D::SetData(Int level, Rectangle* rect, std::vector<Byte> const& data, size_t startIndex, size_t elementCount)
2024-05-05 15:50:17 -03:00
{
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
std::vector<UINT> finalData(elementCount / 4);
auto fIndex = 0;
for (size_t i = startIndex; i < elementCount; ++i) {
const auto& r = data[i];
const auto& g = data[++i];
const auto& b = data[++i];
const auto& a = data[++i];
finalData[fIndex] = Color(r, g, b, a);
++fIndex;
}
if (!Implementation->Texture2D) {
auto hr = BaseGraphicsDevice->Implementation->Device->CreateTexture2D(&Implementation->Description, nullptr, Implementation->Texture2D.GetAddressOf());
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
}
2024-06-25 17:06:37 -03:00
comptr<ID3D11Resource> resource = nullptr;
auto hr = Implementation->Texture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
D3D11_BOX box{};
if (rect) {
2024-06-25 17:06:37 -03:00
box.left = rect->Left();
box.right = rect->Right();
box.top = rect->Top();
box.bottom = rect->Bottom();
2024-05-05 15:50:17 -03:00
box.back = level;
box.front = 0;
}
2024-05-23 14:38:16 -03:00
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
BaseGraphicsDevice->Implementation->Context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), Implementation->Description.Width * R8G8B8A8U_BYTE_SIZE, 0);
2024-05-05 15:50:17 -03:00
Implementation->ShaderDescription.Format = Implementation->Description.Format;
Implementation->ShaderDescription.Texture2D.MipLevels = Implementation->Description.MipLevels;
hr = BaseGraphicsDevice->Implementation->Device->CreateShaderResourceView(resource.Get(), &Implementation->ShaderDescription, Implementation->ShaderResource.ReleaseAndGetAddressOf());
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
Implementation->Texture2D->GetDesc(&Implementation->Description);
2024-05-05 15:50:17 -03:00
}
void Texture2D::SetData(std::vector<Color> const& data, size_t startIndex, size_t elementCount)
2024-05-05 15:50:17 -03:00
{
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device || !BaseGraphicsDevice->Implementation->Context) {
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
std::vector<UINT> finalData(elementCount);
auto finalDataIndex = 0;
for (size_t i = startIndex; i < elementCount; ++i) {
finalData[finalDataIndex] = static_cast<UINT>(data[i]);
++finalDataIndex;
}
auto hr = Implementation->SetData(*BaseGraphicsDevice, finalData.data());
if (FAILED(hr))
throw csharp::InvalidOperationException();
2024-05-05 15:50:17 -03:00
}
2024-12-13 17:28:46 -03:00
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, csharp::Stream& stream)
{
std::vector<Byte> data;
2024-12-13 17:28:46 -03:00
const auto lenght = stream.Length();
stream.Read(data.data(), data.size(), 0, lenght - 1);
return FromStream(device, data);
}
2024-12-13 17:28:46 -03:00
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, std::string const& fileName)
{
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
comptr<ID3D11Resource> resource = nullptr;
auto wstr = misc::ToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile(
device.Implementation->Device.Get(),
device.Implementation->Context.Get(),
wstr.c_str(),
resource.GetAddressOf(),
texture2d->Implementation->ShaderResource.ReleaseAndGetAddressOf(),
0U);
if (FAILED(result)) {
return nullptr;
}
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->Implementation->Texture2D.ReleaseAndGetAddressOf());
if (FAILED(result)) {
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
texture2d->Implementation->Texture2D->GetDesc(&desc);
texture2d->Implementation->Description = desc;
return texture2d;
}
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, std::vector<Byte> const& data)
2024-05-05 15:50:17 -03:00
{
2024-05-06 15:57:09 -03:00
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
2024-06-25 17:06:37 -03:00
comptr<ID3D11Resource> resource = nullptr;
2024-05-05 15:50:17 -03:00
auto hr = DirectX::CreateWICTextureFromMemory(
device.Implementation->Device.Get(),
device.Implementation->Context.Get(),
2024-05-05 15:50:17 -03:00
data.data(),
data.size(),
2024-06-25 17:06:37 -03:00
resource.GetAddressOf(),
texture2d->Implementation->ShaderResource.ReleaseAndGetAddressOf());
2024-05-05 15:50:17 -03:00
if (FAILED(hr))
{
return nullptr;
}
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->Implementation->Texture2D.ReleaseAndGetAddressOf());
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
texture2d->Implementation->Texture2D->GetDesc(&desc);
texture2d->Implementation->Description = desc;
2024-05-05 15:50:17 -03:00
return texture2d;
}
2024-03-18 15:41:46 -03:00
}