2013-09-30 19:37:13 -07: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
|
2013-12-02 21:24:38 -08:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2013-09-30 19:37:13 -07:00
|
|
|
(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/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-11-26 22:07:27 -08:00
|
|
|
#include <util/base.h>
|
2013-10-10 08:01:09 -07:00
|
|
|
#include "d3d11-subsystem.hpp"
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-10-16 23:31:18 -07:00
|
|
|
void gs_texture_2d::InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd,
|
2014-06-27 21:29:06 -07:00
|
|
|
const uint8_t **data)
|
2013-10-04 08:55:33 -07:00
|
|
|
{
|
2013-10-05 09:40:43 -07:00
|
|
|
uint32_t rowSizeBytes = width * gs_get_format_bpp(format);
|
2013-10-05 00:34:43 -07:00
|
|
|
uint32_t texSizeBytes = height * rowSizeBytes / 8;
|
2013-10-04 08:55:33 -07:00
|
|
|
size_t textures = type == GS_TEXTURE_2D ? 1 : 6;
|
2013-10-05 09:40:43 -07:00
|
|
|
uint32_t actual_levels = levels;
|
2014-10-13 21:41:09 -07:00
|
|
|
|
2013-10-05 09:40:43 -07:00
|
|
|
if (!actual_levels)
|
2014-08-07 23:42:07 -07:00
|
|
|
actual_levels = gs_get_total_levels(width, height);
|
2013-10-04 08:55:33 -07:00
|
|
|
|
2013-10-05 00:34:43 -07:00
|
|
|
rowSizeBytes /= 8;
|
|
|
|
|
2013-10-04 08:55:33 -07:00
|
|
|
for (size_t i = 0; i < textures; i++) {
|
|
|
|
uint32_t newRowSize = rowSizeBytes;
|
|
|
|
uint32_t newTexSize = texSizeBytes;
|
|
|
|
|
2013-10-05 00:34:43 -07:00
|
|
|
for (uint32_t j = 0; j < actual_levels; j++) {
|
2013-10-04 08:55:33 -07:00
|
|
|
D3D11_SUBRESOURCE_DATA newSRD;
|
2013-10-04 12:47:44 -07:00
|
|
|
newSRD.pSysMem = *data;
|
2014-10-13 21:41:09 -07:00
|
|
|
newSRD.SysMemPitch = newRowSize;
|
2013-10-04 08:55:33 -07:00
|
|
|
newSRD.SysMemSlicePitch = newTexSize;
|
|
|
|
srd.push_back(newSRD);
|
|
|
|
|
|
|
|
newRowSize /= 2;
|
|
|
|
newTexSize /= 4;
|
|
|
|
data++;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 21:29:06 -07:00
|
|
|
void gs_texture_2d::InitTexture(const uint8_t **data)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2013-10-04 08:55:33 -07:00
|
|
|
vector<D3D11_SUBRESOURCE_DATA> srd;
|
2013-09-30 19:37:13 -07:00
|
|
|
D3D11_TEXTURE2D_DESC td;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
memset(&td, 0, sizeof(td));
|
|
|
|
td.Width = width;
|
|
|
|
td.Height = height;
|
2013-10-04 08:55:33 -07:00
|
|
|
td.MipLevels = genMipmaps ? 0 : levels;
|
2013-09-30 19:37:13 -07:00
|
|
|
td.ArraySize = type == GS_TEXTURE_CUBE ? 6 : 1;
|
|
|
|
td.Format = dxgiFormat;
|
2013-11-10 05:31:55 -08:00
|
|
|
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
2013-09-30 19:37:13 -07:00
|
|
|
td.SampleDesc.Count = 1;
|
|
|
|
td.CPUAccessFlags = isDynamic ? D3D11_CPU_ACCESS_WRITE : 0;
|
|
|
|
td.Usage = isDynamic ? D3D11_USAGE_DYNAMIC :
|
|
|
|
D3D11_USAGE_DEFAULT;
|
|
|
|
|
|
|
|
if (type == GS_TEXTURE_CUBE)
|
|
|
|
td.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
|
|
|
|
|
|
|
|
if (isRenderTarget || isGDICompatible)
|
2013-11-10 05:31:55 -08:00
|
|
|
td.BindFlags |= D3D11_BIND_RENDER_TARGET;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-03-05 09:43:14 -08:00
|
|
|
if (isGDICompatible)
|
|
|
|
td.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
if (data)
|
|
|
|
InitSRD(srd, data);
|
|
|
|
|
2013-10-04 08:55:33 -07:00
|
|
|
hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL,
|
2013-09-30 19:37:13 -07:00
|
|
|
texture.Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create 2D texture", hr);
|
2014-03-05 09:43:14 -08:00
|
|
|
|
|
|
|
if (isGDICompatible) {
|
|
|
|
hr = texture->QueryInterface(__uuidof(IDXGISurface1),
|
|
|
|
(void**)gdiSurface.Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create GDI surface", hr);
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void gs_texture_2d::InitResourceView()
|
|
|
|
{
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
memset(&resourceDesc, 0, sizeof(resourceDesc));
|
|
|
|
resourceDesc.Format = dxgiFormat;
|
|
|
|
|
|
|
|
if (type == GS_TEXTURE_CUBE) {
|
2013-11-10 05:31:55 -08:00
|
|
|
resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
|
2013-09-30 19:37:13 -07:00
|
|
|
resourceDesc.TextureCube.MipLevels = genMipmaps ? -1 : 1;
|
|
|
|
} else {
|
2013-11-10 05:31:55 -08:00
|
|
|
resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
2013-09-30 19:37:13 -07:00
|
|
|
resourceDesc.Texture2D.MipLevels = genMipmaps ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
|
|
|
|
shaderRes.Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create resource view", hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gs_texture_2d::InitRenderTargets()
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
if (type == GS_TEXTURE_2D) {
|
|
|
|
hr = device->device->CreateRenderTargetView(texture, NULL,
|
|
|
|
renderTarget[0].Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create render target view",
|
|
|
|
hr);
|
|
|
|
} else {
|
|
|
|
D3D11_RENDER_TARGET_VIEW_DESC rtv;
|
|
|
|
rtv.Format = dxgiFormat;
|
|
|
|
rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
|
|
|
|
rtv.Texture2DArray.MipSlice = 0;
|
|
|
|
rtv.Texture2DArray.ArraySize = 1;
|
|
|
|
|
|
|
|
for (UINT i = 0; i < 6; i++) {
|
|
|
|
rtv.Texture2DArray.FirstArraySlice = i;
|
|
|
|
hr = device->device->CreateRenderTargetView(texture,
|
|
|
|
&rtv, renderTarget[i].Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create cube render "
|
|
|
|
"target view", hr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width,
|
|
|
|
uint32_t height, gs_color_format colorFormat, uint32_t levels,
|
2014-06-27 21:29:06 -07:00
|
|
|
const uint8_t **data, uint32_t flags, gs_texture_type type,
|
|
|
|
bool gdiCompatible, bool shared)
|
2013-10-16 23:31:18 -07:00
|
|
|
: gs_texture (device, type, levels, colorFormat),
|
2013-09-30 19:37:13 -07:00
|
|
|
width (width),
|
|
|
|
height (height),
|
|
|
|
dxgiFormat (ConvertGSTextureFormat(format)),
|
2015-02-09 01:03:33 -08:00
|
|
|
isRenderTarget ((flags & GS_RENDER_TARGET) != 0),
|
2013-09-30 19:37:13 -07:00
|
|
|
isGDICompatible (gdiCompatible),
|
|
|
|
isDynamic ((flags & GS_DYNAMIC) != 0),
|
2015-02-09 01:03:33 -08:00
|
|
|
isShared (shared),
|
2014-08-07 23:42:07 -07:00
|
|
|
genMipmaps ((flags & GS_BUILD_MIPMAPS) != 0)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
InitTexture(data);
|
|
|
|
InitResourceView();
|
|
|
|
|
|
|
|
if (isRenderTarget)
|
|
|
|
InitRenderTargets();
|
|
|
|
}
|
2014-10-13 21:41:09 -07:00
|
|
|
|
|
|
|
gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle)
|
2015-02-09 01:02:12 -08:00
|
|
|
: isShared (true),
|
2014-10-13 21:41:09 -07:00
|
|
|
sharedHandle (handle)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
2015-02-09 01:16:27 -08:00
|
|
|
hr = device->device->OpenSharedResource((HANDLE)(uintptr_t)handle,
|
2014-10-13 21:41:09 -07:00
|
|
|
__uuidof(ID3D11Texture2D), (void**)texture.Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to open resource", hr);
|
|
|
|
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
texture->GetDesc(&desc);
|
|
|
|
|
|
|
|
this->type = GS_TEXTURE_2D;
|
|
|
|
this->format = ConvertDXGITextureFormat(desc.Format);
|
|
|
|
this->levels = 1;
|
|
|
|
this->device = device;
|
|
|
|
|
|
|
|
this->width = desc.Width;
|
|
|
|
this->height = desc.Height;
|
|
|
|
this->dxgiFormat = desc.Format;
|
|
|
|
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc = {};
|
|
|
|
resourceDesc.Format = desc.Format;
|
|
|
|
resourceDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
resourceDesc.Texture2D.MipLevels = 1;
|
|
|
|
|
|
|
|
hr = device->device->CreateShaderResourceView(texture, &resourceDesc,
|
|
|
|
shaderRes.Assign());
|
|
|
|
if (FAILED(hr))
|
|
|
|
throw HRError("Failed to create shader resource view", hr);
|
|
|
|
}
|