gs_texture_2d成员函数
2024-06-19
3
0
2d纹理可创建的类型是多样的。有是2d的,也有的是2d-cube的。有的是需要绑定的是渲染目标视图,有的是渲染着色器资源视图。其它的一些配置参数多样。
InitTexture(data);//根据纹理数据创建纹理
InitResourceView();//创建着色器资源视图
if (isRenderTarget) //需要需要创建渲染目标资源视图
InitRenderTargets();
创建纹理
void gs_texture_2d::InitTexture(const uint8_t *const *data)
{
HRESULT hr;
memset(&td, 0, sizeof(td));
td.Width = width;
td.Height = height;
td.MipLevels = genMipmaps ? 0 : levels;
td.ArraySize = type == GS_TEXTURE_CUBE ? 6 : 1;
td.Format = twoPlane ? ((format == GS_R16) ? DXGI_FORMAT_P010 : DXGI_FORMAT_NV12) : dxgiFormatResource;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
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)
td.BindFlags |= D3D11_BIND_RENDER_TARGET;
if (isGDICompatible)
td.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
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;
if (data) {
BackupTexture(data);
InitSRD(srd);
}
hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL, texture.Assign());
if (FAILED(hr))
throw HRError("Failed to create 2D texture", hr);
if (isGDICompatible) {
hr = texture->QueryInterface(__uuidof(IDXGISurface1),(void **)gdiSurface.Assign());
if (FAILED(hr))
throw HRError("Failed to create GDI surface", hr);
}
if (isShared) {
ComPtr<IDXGIResource> dxgi_res;
texture->SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM);
hr = texture->QueryInterface(__uuidof(IDXGIResource),(void **)&dxgi_res);
if (FAILED(hr)) {
blog(LOG_WARNING,
"InitTexture: Failed to query "
"interface: %08lX",
hr);
} else {
GetSharedHandle(dxgi_res);
if (flags & GS_SHARED_KM_TEX) {
ComPtr<IDXGIKeyedMutex> km;
hr = texture->QueryInterface(__uuidof(IDXGIKeyedMutex),(void **)&km);
if (FAILED(hr)) {
throw HRError("Failed to query "
"IDXGIKeyedMutex",
hr);
}
km->AcquireSync(0, INFINITE);
acquired = true;
}
}
}
}
着色器资源视图
进行了相应的成员函数初始化后,最终调用的是InitResourceView来构造着色器资源视图。
void gs_texture_2d::InitResourceView()
{
HRESULT hr;
memset(&viewDesc, 0, sizeof(viewDesc));
viewDesc.Format = dxgiFormatView;
if (type == GS_TEXTURE_CUBE) {
viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
viewDesc.TextureCube.MipLevels = genMipmaps || !levels ? -1 : levels;
} else {
viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
viewDesc.Texture2D.MipLevels = genMipmaps || !levels ? -1 : levels;
}
hr = device->device->CreateShaderResourceView(texture, &viewDesc,shaderRes.Assign());
if (FAILED(hr))
throw HRError("Failed to create SRV", hr);
viewDescLinear = viewDesc;
viewDescLinear.Format = dxgiFormatViewLinear;
if (dxgiFormatView == dxgiFormatViewLinear) {
shaderResLinear = shaderRes;
} else {
hr = device->device->CreateShaderResourceView(
texture, &viewDescLinear, shaderResLinear.Assign());
if (FAILED(hr))
throw HRError("Failed to create linear SRV", hr);
}
}
渲染目标视图
void gs_texture_2d::InitRenderTargets()
{
HRESULT hr;
if (type == GS_TEXTURE_2D) {
D3D11_RENDER_TARGET_VIEW_DESC rtv;
rtv.Format = dxgiFormatView;
rtv.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtv.Texture2D.MipSlice = 0;
hr = device->device->CreateRenderTargetView(
texture, &rtv, renderTarget[0].Assign());
if (FAILED(hr))
throw HRError("Failed to create RTV", hr);
if (dxgiFormatView == dxgiFormatViewLinear) {
renderTargetLinear[0] = renderTarget[0];
} else {
rtv.Format = dxgiFormatViewLinear;
hr = device->device->CreateRenderTargetView(
texture, &rtv, renderTargetLinear[0].Assign());
if (FAILED(hr))
throw HRError("Failed to create linear RTV",
hr);
}
} else {
D3D11_RENDER_TARGET_VIEW_DESC rtv;
rtv.Format = dxgiFormatView;
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 RTV", hr);
if (dxgiFormatView == dxgiFormatViewLinear) {
renderTargetLinear[i] = renderTarget[i];
} else {
rtv.Format = dxgiFormatViewLinear;
hr = device->device->CreateRenderTargetView(
texture, &rtv,
renderTargetLinear[i].Assign());
if (FAILED(hr))
throw HRError(
"Failed to create linear cube RTV",
hr);
}
}
}
}