gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle)
	: isShared        (true),
	  sharedHandle    (handle)
{
	HRESULT hr;
	hr = device->device->OpenSharedResource((HANDLE)(uintptr_t)handle,
			__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);
}
static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
{
	D3D11_TEXTURE2D_DESC desc;
	tex->GetDesc(&desc);

	if (!d->texture ||
	    d->texture->width != desc.Width ||
	    d->texture->height != desc.Height) {

		delete d->texture;
		d->texture = (gs_texture_2d*)gs_texture_create(
				desc.Width, desc.Height,
				ConvertDXGITextureFormat(desc.Format), 1,
				nullptr, 0);
	}

	if (!!d->texture)
		d->device->context->CopyResource(d->texture->texture,
				tex);
}