2024-07-13 22:55:36 -03:00
|
|
|
#include "xna/xna-dx.hpp"
|
2024-03-18 15:41:46 -03:00
|
|
|
|
2024-05-04 21:07:39 -03:00
|
|
|
namespace xna {
|
2024-08-03 12:40:44 -03:00
|
|
|
static void setDefaultTexture2DDesc(Texture2D::PlatformImplementation& impl);
|
|
|
|
static HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data);
|
2024-05-04 21:07:39 -03:00
|
|
|
|
2024-08-05 15:42:56 -03:00
|
|
|
Texture2D::Texture2D() : Texture(nullptr) {
|
|
|
|
impl = unew<PlatformImplementation>();
|
|
|
|
setDefaultTexture2DDesc(*impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D::Texture2D(sptr<GraphicsDevice> const& device, size_t width, size_t height) : Texture(device), width(width), height(height) {
|
|
|
|
impl = unew<PlatformImplementation>();
|
|
|
|
setDefaultTexture2DDesc(*impl);
|
|
|
|
impl->dxDescription.Width = static_cast<UINT>(this->width);
|
|
|
|
impl->dxDescription.Height = static_cast<UINT>(this->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D::Texture2D(sptr<GraphicsDevice> const& device) : Texture(device) {
|
|
|
|
impl = unew<PlatformImplementation>();
|
|
|
|
setDefaultTexture2DDesc(*impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
impl = unew<PlatformImplementation>();
|
|
|
|
setDefaultTexture2DDesc(*impl);
|
|
|
|
impl->dxDescription.Width = static_cast<UINT>(this->width);
|
|
|
|
impl->dxDescription.Height = static_cast<UINT>(this->height);
|
|
|
|
impl->dxDescription.MipLevels = static_cast<UINT>(this->levelCount);
|
|
|
|
impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format);
|
|
|
|
}
|
|
|
|
|
2024-08-02 22:10:40 -03:00
|
|
|
void 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) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
|
2024-08-05 16:12:53 -03:00
|
|
|
}
|
2024-05-04 21:07:39 -03:00
|
|
|
|
2024-08-05 16:12:53 -03:00
|
|
|
auto& deviceImpl = m_device->impl;
|
2024-05-04 21:07:39 -03:00
|
|
|
|
2024-08-05 16:12:53 -03:00
|
|
|
HRESULT hr = 0;
|
|
|
|
|
2024-08-05 15:42:56 -03:00
|
|
|
if (!impl->dxTexture2D) {
|
2024-08-05 16:12:53 -03:00
|
|
|
hr = deviceImpl->_device->CreateTexture2D(
|
|
|
|
&impl->dxDescription,
|
|
|
|
nullptr,
|
|
|
|
impl->dxTexture2D.ReleaseAndGetAddressOf());
|
2024-08-05 15:42:56 -03:00
|
|
|
|
2024-08-05 16:12:53 -03:00
|
|
|
if FAILED(hr)
|
2024-08-05 15:42:56 -03:00
|
|
|
Exception::Throw(Exception::FAILED_TO_CREATE);
|
|
|
|
}
|
|
|
|
else {
|
2024-08-05 16:12:53 -03:00
|
|
|
//Updates description if texture is not null
|
2024-08-05 15:42:56 -03:00
|
|
|
impl->dxTexture2D->GetDesc(&impl->dxDescription);
|
2024-05-04 21:07:39 -03:00
|
|
|
}
|
|
|
|
|
2024-06-25 17:06:37 -03:00
|
|
|
comptr<ID3D11Resource> resource = nullptr;
|
|
|
|
hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
|
2024-05-04 21:07:39 -03:00
|
|
|
|
2024-08-05 16:12:53 -03:00
|
|
|
if FAILED(hr)
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
2024-08-05 15:42:56 -03:00
|
|
|
|
2024-08-05 16:12:53 -03:00
|
|
|
//Only initializes if it is a ShaderResource
|
2024-08-05 15:42:56 -03:00
|
|
|
if (impl->dxDescription.BindFlags & D3D11_BIND_SHADER_RESOURCE) {
|
2024-08-05 16:12:53 -03:00
|
|
|
hr = deviceImpl->_device->CreateShaderResourceView(
|
|
|
|
resource.Get(),
|
|
|
|
&impl->dxShaderDescription,
|
|
|
|
impl->dxShaderResource.ReleaseAndGetAddressOf());
|
2024-05-04 21:07:39 -03:00
|
|
|
|
2024-08-05 16:12:53 -03:00
|
|
|
if FAILED(hr)
|
2024-08-05 15:42:56 -03:00
|
|
|
Exception::Throw(Exception::FAILED_TO_CREATE);
|
2024-08-05 16:12:53 -03:00
|
|
|
|
2024-08-05 15:42:56 -03:00
|
|
|
}
|
2024-05-04 21:07:39 -03:00
|
|
|
|
2024-08-02 22:10:40 -03:00
|
|
|
surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format);
|
|
|
|
levelCount = static_cast<Int>(impl->dxShaderDescription.Texture2D.MipLevels);
|
2024-08-03 22:45:19 -03:00
|
|
|
width = static_cast<Int>(impl->dxDescription.Width);
|
|
|
|
height = static_cast<Int>(impl->dxDescription.Height);
|
2024-08-05 15:42:56 -03:00
|
|
|
}
|
2024-05-05 15:50:17 -03:00
|
|
|
|
2024-06-22 11:02:01 -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) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
|
2024-08-03 12:40:44 -03:00
|
|
|
internalTexture2DSetData(*impl, *m_device, data.data());
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -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) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
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-08-03 12:40:44 -03:00
|
|
|
internalTexture2DSetData(*impl, *m_device, finalData.data());
|
2024-08-03 12:32:30 -03:00
|
|
|
}
|
2024-05-05 15:50:17 -03:00
|
|
|
|
2024-06-22 11:02:01 -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) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
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-06-25 17:06:37 -03:00
|
|
|
auto hr = m_device->impl->_device->CreateTexture2D(&impl->dxDescription, nullptr, impl->dxTexture2D.GetAddressOf());
|
2024-05-05 15:50:17 -03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::FAILED_TO_CREATE);
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 17:06:37 -03:00
|
|
|
comptr<ID3D11Resource> resource = nullptr;
|
|
|
|
auto hr = impl->dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
|
2024-05-05 15:50:17 -03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
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;
|
2024-06-25 17:06:37 -03:00
|
|
|
m_device->impl->_context->UpdateSubresource(resource.Get(), 0, rect ? &box : nullptr, finalData.data(), impl->dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
|
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-06-25 17:06:37 -03:00
|
|
|
hr = m_device->impl->_device->CreateShaderResourceView(resource.Get(), &impl->dxShaderDescription, impl->dxShaderResource.ReleaseAndGetAddressOf());
|
2024-05-05 15:50:17 -03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::FAILED_TO_CREATE);
|
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
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -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) {
|
2024-07-06 12:20:54 -03:00
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
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;
|
|
|
|
}
|
|
|
|
|
2024-08-03 12:40:44 -03:00
|
|
|
internalTexture2DSetData(*impl, *m_device, finalData.data());
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
|
2024-08-03 12:32:30 -03:00
|
|
|
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream)
|
|
|
|
{
|
|
|
|
std::vector<Byte> data;
|
|
|
|
const auto lenght = stream->Length();
|
|
|
|
stream->Read(data, 0, lenght - 1);
|
|
|
|
|
|
|
|
return FromStream(device, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
|
|
|
|
{
|
|
|
|
auto _this = device.shared_from_this();
|
|
|
|
auto texture2d = snew<Texture2D>(_this);
|
|
|
|
comptr<ID3D11Resource> resource = nullptr;
|
|
|
|
auto wstr = XnaHelper::ToWString(fileName);
|
|
|
|
|
|
|
|
HRESULT result = DirectX::CreateWICTextureFromFile(
|
|
|
|
device.impl->_device.Get(),
|
|
|
|
device.impl->_context.Get(),
|
|
|
|
wstr.c_str(),
|
|
|
|
resource.GetAddressOf(),
|
|
|
|
texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(),
|
|
|
|
0U);
|
|
|
|
|
|
|
|
if (FAILED(result)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf());
|
|
|
|
|
|
|
|
if (FAILED(result)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
texture2d->impl->dxTexture2D->GetDesc(&desc);
|
|
|
|
texture2d->impl->dxDescription = 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();
|
2024-06-22 11:52:21 -03:00
|
|
|
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(
|
2024-06-25 17:06:37 -03:00
|
|
|
device.impl->_device.Get(),
|
|
|
|
device.impl->_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->impl->dxShaderResource.ReleaseAndGetAddressOf());
|
2024-05-05 15:50:17 -03:00
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-06-25 17:06:37 -03:00
|
|
|
hr = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf());
|
2024-05-05 15:50:17 -03:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
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
|
|
|
|
|
|
|
return texture2d;
|
2024-08-03 22:45:19 -03:00
|
|
|
}
|
2024-08-03 12:32:30 -03:00
|
|
|
|
2024-08-03 12:40:44 -03:00
|
|
|
HRESULT internalTexture2DSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data)
|
2024-08-03 12:32:30 -03:00
|
|
|
{
|
|
|
|
if (!impl.dxTexture2D) {
|
|
|
|
auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf());
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
Exception::Throw(Exception::FAILED_TO_CREATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comptr<ID3D11Resource> resource = nullptr;
|
|
|
|
auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf());
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
Exception::Throw(Exception::INVALID_OPERATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int R8G8B8A8U_BYTE_SIZE = 4;
|
|
|
|
device.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0);
|
|
|
|
|
|
|
|
impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels;
|
|
|
|
hr = device.impl->_device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf());
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
Exception::Throw(Exception::FAILED_TO_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl.dxTexture2D->GetDesc(&impl.dxDescription);
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2024-08-03 12:40:44 -03:00
|
|
|
|
|
|
|
void setDefaultTexture2DDesc(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;
|
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
}
|