bool CD3DTexture::CreateInternal(const void* pixels /* nullptr */, unsigned int srcPitch /* 0 */) { ID3D11Device* pD3DDevice = g_Windowing.Get3D11Device(); ID3D11DeviceContext* pD3D11Context = g_Windowing.Get3D11Context(); UINT miscFlags = 0; bool autogenmm = false; if (m_mipLevels == 0 && g_Windowing.IsFormatSupport(m_format, D3D11_FORMAT_SUPPORT_MIP_AUTOGEN)) { autogenmm = pixels != nullptr; miscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS; } else m_mipLevels = 1; CD3D11_TEXTURE2D_DESC textureDesc(m_format, m_width, m_height, 1, m_mipLevels, m_bindFlags, m_usage, m_cpuFlags, 1, 0, miscFlags); D3D11_SUBRESOURCE_DATA initData = { 0 }; initData.pSysMem = pixels; initData.SysMemPitch = srcPitch ? srcPitch : CD3DHelper::BitsPerPixel(m_format) * m_width / 8; initData.SysMemSlicePitch = 0; HRESULT hr = pD3DDevice->CreateTexture2D(&textureDesc, (!autogenmm && pixels) ? &initData : nullptr, &m_texture); if (SUCCEEDED(hr) && autogenmm) { pD3D11Context->UpdateSubresource(m_texture, 0, nullptr, pixels, (srcPitch ? srcPitch : CD3DHelper::BitsPerPixel(m_format) * m_width / 8), 0); } if (autogenmm) GenerateMipmaps(); return SUCCEEDED(hr); }
UnorderedAccessView::UnorderedAccessView(UnderlyingResource resource, NativeFormat::Enum format, unsigned mipSlice, bool appendBuffer, bool forceArray) { if (!resource) { Throw(::Exceptions::BasicLabel("NULL resource passed to UnorderedAccessView constructor")); } intrusive_ptr<ID3D::UnorderedAccessView> view = nullptr; if (format == NativeFormat::Unknown && mipSlice == 0 && !appendBuffer && !forceArray) { view = ObjectFactory(*resource).CreateUnorderedAccessView(resource); } else { D3D11_UNORDERED_ACCESS_VIEW_DESC viewDesc; viewDesc.Format = AsDXGIFormat(format); TextureDesc2D textureDesc(resource); if (textureDesc.Width > 0) { if (textureDesc.ArraySize > 1 || forceArray) { viewDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY; viewDesc.Texture2DArray.MipSlice = mipSlice; viewDesc.Texture2DArray.FirstArraySlice = 0; viewDesc.Texture2DArray.ArraySize = textureDesc.ArraySize; } else { viewDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D; viewDesc.Texture2D.MipSlice = mipSlice; } } else { TextureDesc3D t3dDesc(resource); if (t3dDesc.Width > 0) { viewDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D; viewDesc.Texture3D.MipSlice = mipSlice; viewDesc.Texture3D.FirstWSlice = 0; viewDesc.Texture3D.WSize = (UINT)-1; } else { TextureDesc1D t1dDesc(resource); if (t1dDesc.Width > 0) { viewDesc.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE1D; viewDesc.Texture1D.MipSlice = mipSlice; } else { D3DBufferDesc bufferDesc(resource); viewDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; viewDesc.Buffer.FirstElement = 0; viewDesc.Buffer.NumElements = bufferDesc.StructureByteStride ? (bufferDesc.ByteWidth/bufferDesc.StructureByteStride) : bufferDesc.ByteWidth; viewDesc.Buffer.Flags = appendBuffer ? D3D11_BUFFER_UAV_FLAG_APPEND : 0; } } } view = ObjectFactory(*resource).CreateUnorderedAccessView(resource, &viewDesc); } _underlying = std::move(view); }
bool CD3DTexture::CreateInternal(const void* pixels /* nullptr */, unsigned int srcPitch /* 0 */) { ID3D11Device* pD3DDevice = g_Windowing.Get3D11Device(); CD3D11_TEXTURE2D_DESC textureDesc(m_format, m_width, m_height, 1, m_mipLevels, m_bindFlags, m_usage, m_cpuFlags, 1, 0, (m_mipLevels > 1) ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0); D3D11_SUBRESOURCE_DATA initData = { 0 }; initData.pSysMem = pixels; initData.SysMemPitch = srcPitch ? srcPitch : CD3DHelper::BitsPerPixel(m_format) * m_width / 8; initData.SysMemSlicePitch = 0; return SUCCEEDED(pD3DDevice->CreateTexture2D(&textureDesc, (pixels ? &initData : NULL), &m_texture)); }
bool DirectX11Texture2DHelper::InternalConstruct(ID3D11Device* d3dDevice, int width, int height, DXGI_FORMAT format, UINT bindFlags, int mipLevels, int arraySize, int sampleCount, int sampleQuality, D3D11_RTV_DIMENSION rtvDimension, D3D11_UAV_DIMENSION uavDimension, D3D11_SRV_DIMENSION srvDimension) { CD3D11_TEXTURE2D_DESC textureDesc(format, width, height, arraySize, mipLevels, bindFlags, D3D11_USAGE_DEFAULT, 0, sampleCount, sampleQuality, mipLevels != 1 ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0); if(FAILED(d3dDevice->CreateTexture2D(&textureDesc, 0, mTexture.getModifiablePointer()))) return true; mTexture->GetDesc(&textureDesc); if(bindFlags & D3D11_BIND_RENDER_TARGET) { mRenderTargetElements.resize(arraySize); for(int i = 0; i < arraySize; ++i) { CD3D11_RENDER_TARGET_VIEW_DESC textureRTVDesc(rtvDimension, format, 0, i, 1); if(FAILED(d3dDevice->CreateRenderTargetView(mTexture, &textureRTVDesc, mRenderTargetElements[i].getModifiablePointer()))) return true; } } if(bindFlags & D3D11_BIND_UNORDERED_ACCESS) { mUnorderedAccessElements.resize(arraySize); for(int i = 0; i < arraySize; ++i) { CD3D11_UNORDERED_ACCESS_VIEW_DESC textureUAVDesc(uavDimension, format, 0, i, 1); if(FAILED(d3dDevice->CreateUnorderedAccessView(mTexture, &textureUAVDesc, mUnorderedAccessElements[i].getModifiablePointer()))) return true; } } if(bindFlags & D3D11_BIND_SHADER_RESOURCE) { CD3D11_SHADER_RESOURCE_VIEW_DESC srvDesc(srvDimension, format, 0, textureDesc.MipLevels, 0, textureDesc.ArraySize); d3dDevice->CreateShaderResourceView(mTexture, &srvDesc, mShaderResource.getModifiablePointer()); mShaderResourceElements.resize(arraySize); for(int i = 0; i < arraySize; ++i) { CD3D11_SHADER_RESOURCE_VIEW_DESC srvElementDesc(srvDimension, format, 0, 1, i, 1); if(FAILED(d3dDevice->CreateShaderResourceView(mTexture, &srvElementDesc, mShaderResourceElements[i].getModifiablePointer()))) return true; } } return false; }
DepthStencilView::DepthStencilView( UnderlyingResource resource, NativeFormat::Enum format, const SubResourceSlice& arraySlice) { if (!resource) { Throw(::Exceptions::BasicLabel("NULL resource passed to DepthStencilView constructor")); } intrusive_ptr<ID3D::DepthStencilView> view; if (format == NativeFormat::Unknown) { view = ObjectFactory(*resource).CreateDepthStencilView(resource); } else { TextureDesc2D textureDesc(resource); D3D11_DEPTH_STENCIL_VIEW_DESC viewDesc; viewDesc.Format = AsDXGIFormat(format); viewDesc.Flags = 0; if (arraySlice._arraySize == 0) { if (textureDesc.SampleDesc.Count > 1) { viewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS; } else { viewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; viewDesc.Texture2D.MipSlice = 0; } } else { if (textureDesc.SampleDesc.Count > 1) { viewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY; viewDesc.Texture2DMSArray.ArraySize = arraySlice._arraySize; viewDesc.Texture2DMSArray.FirstArraySlice = arraySlice._firstArraySlice; } else { viewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; viewDesc.Texture2DArray.ArraySize = arraySlice._arraySize; viewDesc.Texture2DArray.FirstArraySlice = arraySlice._firstArraySlice; viewDesc.Texture2DArray.MipSlice = 0; } } view = ObjectFactory(*resource).CreateDepthStencilView(resource, &viewDesc); } _underlying = std::move(view); }
void DX11::TextureSurface::OnResetDevice(Gpu::Api * gpu) { OnLostDevice(gpu); unsigned width = unsigned(viewport.Width); unsigned height = unsigned(viewport.Height); bool typeless = (format == Gpu::DrawSurface::Format_Typeless); if(relativeWindow) { gpu->GetBackbufferSize(width, height, relativeWindow); width = unsigned(widthFactor * float(width)); height = unsigned(heightFactor * float(height)); } viewport = CD3D11_VIEWPORT(0.0f, 0.0f, float(width), float(height)); static const DXGI_FORMAT SURFACE_FORMAT_TO_DXGI_FORMAT[Format_Total] = { DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R11G11B10_FLOAT, DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_R24G8_TYPELESS }; DXGI_FORMAT dxgiFormat = SURFACE_FORMAT_TO_DXGI_FORMAT[format]; unsigned mipLevels = generateMips ? min(width, height) / 4 : 1; D3D11_BIND_FLAG bindFlag = typeless ? D3D11_BIND_DEPTH_STENCIL : D3D11_BIND_RENDER_TARGET; ID3D11Texture2D * texture2D = 0; CD3D11_TEXTURE2D_DESC textureDesc( dxgiFormat, width, height, 1, mipLevels, // MIP LEVELS - *MUST NEVER* BE GREATER THAN log2(width) OR log2(height) bindFlag | D3D11_BIND_SHADER_RESOURCE); device->CreateTexture2D(&textureDesc, 0, &texture2D); if(typeless) dxgiFormat = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; ID3D11ShaderResourceView * shaderView; CD3D11_SHADER_RESOURCE_VIEW_DESC shaderViewDesc( texture2D, D3D11_SRV_DIMENSION_TEXTURE2D, dxgiFormat, 0, mipLevels); device->CreateShaderResourceView(texture2D, &shaderViewDesc, &shaderView); if(!typeless) { CD3D11_RENDER_TARGET_VIEW_DESC renderViewDesc( texture2D, D3D11_RTV_DIMENSION_TEXTURE2D, dxgiFormat); device->CreateRenderTargetView(texture2D, &renderViewDesc, &renderTargetView); CD3D11_TEXTURE2D_DESC depthStencilTextureDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, width, height, 1, 1, D3D11_BIND_DEPTH_STENCIL); device->CreateTexture2D(&depthStencilTextureDesc, 0, &depthStencilTexture); device->CreateDepthStencilView( depthStencilTexture, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D), &depthStencilView); } else { device->CreateDepthStencilView( texture2D, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D, DXGI_FORMAT_D24_UNORM_S8_UINT), &depthStencilView); } texture = new DX11::Texture(texture2D, shaderView, textureDesc); Clear(); }