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

351 lines
9.3 KiB
C++
Raw Normal View History

2024-06-05 21:28:53 -03:00
#include "xna/platform-dx/dx.hpp"
2024-03-18 15:41:46 -03:00
2024-05-04 21:07:39 -03:00
namespace xna {
2024-05-23 14:38:16 -03:00
Texture2D::~Texture2D() {
impl = nullptr;
}
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
2024-04-07 14:06:12 -03:00
{
2024-05-06 15:57:09 -03:00
auto _this = device.shared_from_this();
auto texture2d = snew<Texture2D>(_this);
2024-04-14 16:11:15 -03:00
ID3D11Resource* resource = nullptr;
2024-06-01 20:45:00 -03:00
auto wstr = XnaHelper::ToWString(fileName);
2024-04-07 14:06:12 -03:00
HRESULT result = DirectX::CreateWICTextureFromFile(
2024-05-24 22:26:10 -03:00
device.impl->_device,
device.impl->_context,
2024-04-14 16:11:15 -03:00
wstr.c_str(),
&resource,
2024-05-23 14:38:16 -03:00
&texture2d->impl->dxShaderResource,
2024-04-14 16:11:15 -03:00
0U);
2024-04-07 14:06:12 -03:00
if (FAILED(result))
{
2024-04-07 14:06:12 -03:00
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
2024-04-14 16:11:15 -03:00
2024-05-23 14:38:16 -03:00
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D);
2024-04-07 14:06:12 -03:00
if (FAILED(result)) {
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
2024-04-14 16:11:15 -03:00
D3D11_TEXTURE2D_DESC desc;
2024-05-23 14:38:16 -03:00
texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc;
2024-04-07 14:06:12 -03:00
resource->Release();
resource = nullptr;
return texture2d;
}
2024-05-04 21:07:39 -03:00
bool Texture2D::Initialize()
2024-05-04 21:07:39 -03:00
{
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device) {
Exception::Throw(ExMessage::InitializeComponent);
2024-05-04 21:07:39 -03:00
}
2024-05-24 22:26:10 -03:00
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D);
2024-05-04 21:07:39 -03:00
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
2024-05-04 21:07:39 -03:00
}
ID3D11Resource* resource = nullptr;
2024-05-23 14:38:16 -03:00
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
2024-05-04 21:07:39 -03:00
if (FAILED(hr)) {
Exception::Throw(ExMessage::InvalidOperation);
2024-05-04 21:07:39 -03:00
}
2024-05-24 22:26:10 -03:00
hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource);
2024-05-04 21:07:39 -03:00
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
2024-05-04 21:07:39 -03:00
}
return true;
}
2024-05-05 15:50:17 -03:00
2024-05-23 14:38:16 -03:00
void setDefaultDesc(Texture2D::PlatformImplementation& impl) {
impl.dxDescription.MipLevels = 1;
impl.dxDescription.ArraySize = 1;
impl.dxDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
impl.dxDescription.SampleDesc.Count = 1;
impl.dxDescription.Usage = D3D11_USAGE_DEFAULT;
impl.dxDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE;
impl.dxShaderDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
impl.dxShaderDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
impl.dxShaderDescription.Texture2D.MostDetailedMip = 0;
}
Texture2D::Texture2D() : GraphicsResource(nullptr) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
}
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : GraphicsResource(device) {
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
impl->dxDescription.Width = static_cast<UINT>(width);
impl->dxDescription.Height = static_cast<UINT>(height);
2024-05-05 15:50:17 -03:00
}
2024-05-06 15:57:09 -03:00
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
2024-05-23 14:38:16 -03:00
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
2024-05-05 15:50:17 -03:00
}
2024-05-06 15:57:09 -03:00
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device)
2024-05-05 15:50:17 -03:00
{
2024-05-23 14:38:16 -03:00
impl = unew<PlatformImplementation>();
setDefaultDesc(*impl);
impl->dxDescription.Width = static_cast<UINT>(width);
impl->dxDescription.Height = static_cast<UINT>(height);
impl->dxDescription.MipLevels = static_cast<UINT>(mipMap);
impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format);
}
HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)
2024-05-23 14:38:16 -03:00
{
if (!impl.dxTexture2D) {
2024-05-24 22:26:10 -03:00
auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, &impl.dxTexture2D);
2024-05-23 14:38:16 -03:00
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
2024-05-23 14:38:16 -03:00
}
}
ID3D11Resource* resource = nullptr;
auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
if (FAILED(hr)) {
Exception::Throw(ExMessage::InvalidOperation);
2024-05-23 14:38:16 -03:00
}
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
2024-05-24 22:26:10 -03:00
device.impl->_context->UpdateSubresource(resource, 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
2024-05-23 14:38:16 -03:00
if (impl.dxShaderResource) {
impl.dxShaderResource->Release();
impl.dxShaderResource = nullptr;
}
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
2024-05-24 22:26:10 -03:00
hr = device.impl->_device->CreateShaderResourceView(resource, &impl.dxShaderDescription, &impl.dxShaderResource);
2024-05-23 14:38:16 -03:00
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
2024-05-23 14:38:16 -03:00
}
impl.dxTexture2D->GetDesc(&impl.dxDescription);
return NO_ERROR;
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
{
2024-05-24 22:26:10 -03:00
if (!impl || !m_device || !m_device->impl->_device || !m_device->impl->_context) {
Exception::Throw(ExMessage::InvalidOperation);
2024-05-05 15:50:17 -03:00
}
internalSetData(*impl, *m_device, data.data());
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
{
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device || !m_device->impl->_context) {
Exception::Throw(ExMessage::InvalidOperation);
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;
}
internalSetData(*impl, *m_device, finalData.data());
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
{
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device || !m_device->impl->_context) {
Exception::Throw(ExMessage::InvalidOperation);
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;
}
2024-05-23 14:38:16 -03:00
if (!impl->dxTexture2D) {
2024-05-24 22:26:10 -03:00
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, &impl->dxTexture2D);
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
2024-05-05 15:50:17 -03:00
}
}
ID3D11Resource* resource = nullptr;
2024-05-23 14:38:16 -03:00
auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)&resource);
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
Exception::Throw(ExMessage::InvalidOperation);
2024-05-05 15:50:17 -03:00
}
D3D11_BOX box{};
if (rect) {
box.left = rect->X;
box.right = rect->X + rect->Width;
box.top = rect->Y;
box.bottom = rect->Y + rect->Height;
box.back = level;
box.front = 0;
}
2024-05-23 14:38:16 -03:00
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
2024-05-24 22:26:10 -03:00
m_device->impl->_context->UpdateSubresource(resource, 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
2024-05-05 15:50:17 -03:00
2024-05-23 14:38:16 -03:00
if (impl->dxShaderResource) {
impl->dxShaderResource->Release();
impl->dxShaderResource = nullptr;
2024-05-05 15:50:17 -03:00
}
2024-05-31 22:30:45 -03:00
impl->dxShaderDescription.Format = impl->dxDescription.Format;
2024-05-23 14:38:16 -03:00
impl->dxShaderDescription.Texture2D.MipLevels = impl->dxDescription.MipLevels;
2024-05-24 22:26:10 -03:00
hr = m_device->impl->_device->CreateShaderResourceView(resource, &impl->dxShaderDescription, &impl->dxShaderResource);
2024-05-05 15:50:17 -03:00
if (resource) {
resource->Release();
resource = nullptr;
}
if (FAILED(hr)) {
Exception::Throw(ExMessage::CreateComponent);
2024-05-05 15:50:17 -03:00
}
2024-05-23 14:38:16 -03:00
impl->dxTexture2D->GetDesc(&impl->dxDescription);
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
{
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device || !m_device->impl->_context) {
Exception::Throw(ExMessage::InvalidOperation);
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;
}
internalSetData(*impl, *m_device, finalData.data());
2024-05-05 15:50:17 -03:00
}
sptr<Texture2D> Texture2D::FromMemory(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-05-05 15:50:17 -03:00
ID3D11Resource* resource = nullptr;
auto hr = DirectX::CreateWICTextureFromMemory(
2024-05-24 22:26:10 -03:00
device.impl->_device,
device.impl->_context,
2024-05-05 15:50:17 -03:00
data.data(),
data.size(),
&resource,
2024-05-23 14:38:16 -03:00
&texture2d->impl->dxShaderResource);
2024-05-05 15:50:17 -03:00
if (FAILED(hr))
{
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
2024-05-23 14:38:16 -03:00
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture2d->impl->dxTexture2D);
2024-05-05 15:50:17 -03:00
if (FAILED(hr)) {
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
D3D11_TEXTURE2D_DESC desc;
2024-05-23 14:38:16 -03:00
texture2d->impl->dxTexture2D->GetDesc(&desc);
texture2d->impl->dxDescription = desc;
2024-05-05 15:50:17 -03:00
resource->Release();
resource = nullptr;
return texture2d;
2024-05-23 14:38:16 -03:00
}
2024-05-27 16:44:01 -03:00
Int Texture2D::Width() const {
if (!impl) return 0;
return static_cast<Int>(impl->dxDescription.Width);
}
Int Texture2D::Height() const {
if (!impl) return 0;
return static_cast<Int>(impl->dxDescription.Height);
}
Rectangle Texture2D::Bounds() const {
if (!impl) return {};
return Rectangle(
0, 0,
static_cast<Int>(impl->dxDescription.Width),
static_cast<Int>(impl->dxDescription.Height)
);
}
2024-03-18 15:41:46 -03:00
}