2016-02-23 23:16:51 +00:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <util/base.h>
|
|
|
|
#include "d3d11-subsystem.hpp"
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
void gs_texture_2d::InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
uint32_t rowSizeBytes = width * gs_get_format_bpp(format);
|
|
|
|
uint32_t texSizeBytes = height * rowSizeBytes / 8;
|
|
|
|
size_t textures = type == GS_TEXTURE_2D ? 1 : 6;
|
2016-02-23 23:16:51 +00:00
|
|
|
uint32_t actual_levels = levels;
|
2019-09-22 21:19:10 +00:00
|
|
|
size_t curTex = 0;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (!actual_levels)
|
|
|
|
actual_levels = gs_get_total_levels(width, height);
|
|
|
|
|
|
|
|
rowSizeBytes /= 8;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < textures; i++) {
|
|
|
|
uint32_t newRowSize = rowSizeBytes;
|
|
|
|
uint32_t newTexSize = texSizeBytes;
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < actual_levels; j++) {
|
|
|
|
D3D11_SUBRESOURCE_DATA newSRD;
|
2019-09-22 21:19:10 +00:00
|
|
|
newSRD.pSysMem = data[curTex++].data();
|
|
|
|
newSRD.SysMemPitch = newRowSize;
|
2016-02-23 23:16:51 +00:00
|
|
|
newSRD.SysMemSlicePitch = newTexSize;
|
|
|
|
srd.push_back(newSRD);
|
|
|
|
|
|
|
|
newRowSize /= 2;
|
|
|
|
newTexSize /= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
void gs_texture_2d::BackupTexture(const uint8_t **data)
|
|
|
|
{
|
|
|
|
this->data.resize(levels);
|
|
|
|
|
|
|
|
uint32_t w = width;
|
|
|
|
uint32_t h = height;
|
|
|
|
uint32_t bbp = gs_get_format_bpp(format);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < levels; i++) {
|
|
|
|
if (!data[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
uint32_t texSize = bbp * w * h / 8;
|
|
|
|
this->data[i].resize(texSize);
|
|
|
|
|
|
|
|
vector<uint8_t> &subData = this->data[i];
|
|
|
|
memcpy(&subData[0], data[i], texSize);
|
|
|
|
|
|
|
|
w /= 2;
|
|
|
|
h /= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
void gs_texture_2d::GetSharedHandle(IDXGIResource *dxgi_res)
|
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
hr = dxgi_res->GetSharedHandle(&handle);
|
|
|
|
if (FAILED(hr)) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_WARNING,
|
|
|
|
"GetSharedHandle: Failed to "
|
|
|
|
"get shared handle: %08lX",
|
|
|
|
hr);
|
2019-07-27 12:47:10 +00:00
|
|
|
} else {
|
|
|
|
sharedHandle = (uint32_t)(uintptr_t)handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
void gs_texture_2d::InitTexture(const uint8_t **data)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
memset(&td, 0, sizeof(td));
|
2019-09-22 21:19:10 +00:00
|
|
|
td.Width = width;
|
|
|
|
td.Height = height;
|
|
|
|
td.MipLevels = genMipmaps ? 0 : levels;
|
|
|
|
td.ArraySize = type == GS_TEXTURE_CUBE ? 6 : 1;
|
|
|
|
td.Format = nv12 ? DXGI_FORMAT_NV12 : dxgiFormat;
|
|
|
|
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
2016-02-23 23:16:51 +00:00
|
|
|
td.SampleDesc.Count = 1;
|
2019-09-22 21:19:10 +00:00
|
|
|
td.CPUAccessFlags = isDynamic ? D3D11_CPU_ACCESS_WRITE : 0;
|
|
|
|
td.Usage = isDynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
if (type == GS_TEXTURE_CUBE)
|
|
|
|
td.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
|
|
|
|
|
|
|
|
if (isRenderTarget || isGDICompatible)
|
|
|
|
td.BindFlags |= D3D11_BIND_RENDER_TARGET;
|
|
|
|
|
|
|
|
if (isGDICompatible)
|
|
|
|
td.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
if ((flags & GS_SHARED_KM_TEX) != 0)
|
|
|
|
td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
|
|
|
|
else if ((flags & GS_SHARED_TEX) != 0)
|
|
|
|
td.MiscFlags |= D3D11_RESOURCE_MISC_SHARED;
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
if (data) {
|
|
|
|
BackupTexture(data);
|
|
|
|
InitSRD(srd);
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL,
|
2019-09-22 21:19:10 +00:00
|
|
|
texture.Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create 2D texture", hr);
|
|
|
|
|
|
|
|
if (isGDICompatible) {
|
|
|
|
hr = texture->QueryInterface(__uuidof(IDXGISurface1),
|
2019-09-22 21:19:10 +00:00
|
|
|
(void **)gdiSurface.Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create GDI surface", hr);
|
|
|
|
}
|
2019-07-27 12:47:10 +00:00
|
|
|
|
|
|
|
if (isShared) {
|
|
|
|
ComPtr<IDXGIResource> dxgi_res;
|
|
|
|
|
|
|
|
texture->SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM);
|
|
|
|
|
|
|
|
hr = texture->QueryInterface(__uuidof(IDXGIResource),
|
2019-09-22 21:19:10 +00:00
|
|
|
(void **)&dxgi_res);
|
2019-07-27 12:47:10 +00:00
|
|
|
if (FAILED(hr)) {
|
2019-09-22 21:19:10 +00:00
|
|
|
blog(LOG_WARNING,
|
|
|
|
"InitTexture: Failed to query "
|
|
|
|
"interface: %08lX",
|
|
|
|
hr);
|
2019-07-27 12:47:10 +00:00
|
|
|
} else {
|
|
|
|
GetSharedHandle(dxgi_res);
|
|
|
|
|
|
|
|
if (flags & GS_SHARED_KM_TEX) {
|
|
|
|
ComPtr<IDXGIKeyedMutex> km;
|
|
|
|
hr = texture->QueryInterface(
|
2019-09-22 21:19:10 +00:00
|
|
|
__uuidof(IDXGIKeyedMutex),
|
|
|
|
(void **)&km);
|
2019-07-27 12:47:10 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
throw HRError("Failed to query "
|
2019-09-22 21:19:10 +00:00
|
|
|
"IDXGIKeyedMutex",
|
|
|
|
hr);
|
2019-07-27 12:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
km->AcquireSync(0, INFINITE);
|
|
|
|
acquired = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gs_texture_2d::InitResourceView()
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
memset(&resourceDesc, 0, sizeof(resourceDesc));
|
|
|
|
resourceDesc.Format = dxgiFormat;
|
|
|
|
|
|
|
|
if (type == GS_TEXTURE_CUBE) {
|
|
|
|
resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
|
2019-07-27 12:47:10 +00:00
|
|
|
resourceDesc.TextureCube.MipLevels =
|
|
|
|
genMipmaps || !levels ? -1 : levels;
|
2016-02-23 23:16:51 +00:00
|
|
|
} else {
|
|
|
|
resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
2019-07-27 12:47:10 +00:00
|
|
|
resourceDesc.Texture2D.MipLevels =
|
|
|
|
genMipmaps || !levels ? -1 : levels;
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
|
2019-09-22 21:19:10 +00:00
|
|
|
shaderRes.Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create resource view", hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gs_texture_2d::InitRenderTargets()
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
if (type == GS_TEXTURE_2D) {
|
2019-07-27 12:47:10 +00:00
|
|
|
D3D11_RENDER_TARGET_VIEW_DESC rtv;
|
|
|
|
rtv.Format = dxgiFormat;
|
|
|
|
rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
2019-09-22 21:19:10 +00:00
|
|
|
rtv.Texture2D.MipSlice = 0;
|
2019-07-27 12:47:10 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
hr = device->device->CreateRenderTargetView(
|
|
|
|
texture, &rtv, renderTarget[0].Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create render target view",
|
2019-09-22 21:19:10 +00:00
|
|
|
hr);
|
2016-02-23 23:16:51 +00:00
|
|
|
} else {
|
|
|
|
D3D11_RENDER_TARGET_VIEW_DESC rtv;
|
|
|
|
rtv.Format = dxgiFormat;
|
|
|
|
rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
|
2019-09-22 21:19:10 +00:00
|
|
|
rtv.Texture2DArray.MipSlice = 0;
|
2016-02-23 23:16:51 +00:00
|
|
|
rtv.Texture2DArray.ArraySize = 1;
|
|
|
|
|
|
|
|
for (UINT i = 0; i < 6; i++) {
|
|
|
|
rtv.Texture2DArray.FirstArraySlice = i;
|
2019-09-22 21:19:10 +00:00
|
|
|
hr = device->device->CreateRenderTargetView(
|
|
|
|
texture, &rtv, renderTarget[i].Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create cube render "
|
2019-09-22 21:19:10 +00:00
|
|
|
"target view",
|
|
|
|
hr);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
#define SHARED_FLAGS (GS_SHARED_TEX | GS_SHARED_KM_TEX)
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width,
|
2019-09-22 21:19:10 +00:00
|
|
|
uint32_t height, gs_color_format colorFormat,
|
|
|
|
uint32_t levels, const uint8_t **data,
|
|
|
|
uint32_t flags_, gs_texture_type type,
|
|
|
|
bool gdiCompatible, bool nv12_)
|
|
|
|
: gs_texture(device, gs_type::gs_texture_2d, type, levels, colorFormat),
|
|
|
|
width(width),
|
|
|
|
height(height),
|
|
|
|
flags(flags_),
|
|
|
|
dxgiFormat(ConvertGSTextureFormat(format)),
|
|
|
|
isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
|
|
|
|
isGDICompatible(gdiCompatible),
|
|
|
|
isDynamic((flags_ & GS_DYNAMIC) != 0),
|
|
|
|
isShared((flags_ & SHARED_FLAGS) != 0),
|
|
|
|
genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
|
|
|
|
sharedHandle(GS_INVALID_HANDLE),
|
|
|
|
nv12(nv12_)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
InitTexture(data);
|
|
|
|
InitResourceView();
|
|
|
|
|
|
|
|
if (isRenderTarget)
|
|
|
|
InitRenderTargets();
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
gs_texture_2d::gs_texture_2d(gs_device_t *device, ID3D11Texture2D *nv12tex,
|
2019-09-22 21:19:10 +00:00
|
|
|
uint32_t flags_)
|
|
|
|
: gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
|
|
|
|
isRenderTarget((flags_ & GS_RENDER_TARGET) != 0),
|
|
|
|
isDynamic((flags_ & GS_DYNAMIC) != 0),
|
|
|
|
isShared((flags_ & SHARED_FLAGS) != 0),
|
|
|
|
genMipmaps((flags_ & GS_BUILD_MIPMAPS) != 0),
|
|
|
|
nv12(true),
|
|
|
|
texture(nv12tex)
|
2019-07-27 12:47:10 +00:00
|
|
|
{
|
|
|
|
texture->GetDesc(&td);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
this->type = GS_TEXTURE_2D;
|
|
|
|
this->format = GS_R8G8;
|
|
|
|
this->flags = flags_;
|
|
|
|
this->levels = 1;
|
|
|
|
this->device = device;
|
|
|
|
this->chroma = true;
|
|
|
|
this->width = td.Width / 2;
|
|
|
|
this->height = td.Height / 2;
|
2019-07-27 12:47:10 +00:00
|
|
|
this->dxgiFormat = DXGI_FORMAT_R8G8_UNORM;
|
|
|
|
|
|
|
|
InitResourceView();
|
|
|
|
if (isRenderTarget)
|
|
|
|
InitRenderTargets();
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle)
|
2019-09-22 21:19:10 +00:00
|
|
|
: gs_texture(device, gs_type::gs_texture_2d, GS_TEXTURE_2D),
|
|
|
|
isShared(true),
|
|
|
|
sharedHandle(handle)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
hr = device->device->OpenSharedResource((HANDLE)(uintptr_t)handle,
|
2019-09-22 21:19:10 +00:00
|
|
|
__uuidof(ID3D11Texture2D),
|
|
|
|
(void **)texture.Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
2017-04-19 19:54:15 +00:00
|
|
|
throw HRError("Failed to open shared 2D texture", hr);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
texture->GetDesc(&td);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
this->type = GS_TEXTURE_2D;
|
|
|
|
this->format = ConvertDXGITextureFormat(td.Format);
|
|
|
|
this->levels = 1;
|
|
|
|
this->device = device;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
this->width = td.Width;
|
|
|
|
this->height = td.Height;
|
2017-04-19 19:54:15 +00:00
|
|
|
this->dxgiFormat = td.Format;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
memset(&resourceDesc, 0, sizeof(resourceDesc));
|
2019-09-22 21:19:10 +00:00
|
|
|
resourceDesc.Format = td.Format;
|
|
|
|
resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
2016-02-23 23:16:51 +00:00
|
|
|
resourceDesc.Texture2D.MipLevels = 1;
|
|
|
|
|
|
|
|
hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
|
2019-09-22 21:19:10 +00:00
|
|
|
shaderRes.Assign());
|
2016-02-23 23:16:51 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create shader resource view", hr);
|
|
|
|
}
|