public GlobalIlluminationRenderer(Device device, Vector3 sceneBoundsMin, Vector3 sceneBoundsMax) { m_CubeObject = TextureObject.CreateCubeTexture(device, 128, 128, 1, Format.R16G16B16A16_Float, false, true); m_CubeObjectDepth = TextureObject.CreateCubeTexture(device, 128, 128, 1, Format.R32_Typeless, true, true); if (System.IO.File.Exists("textures\\givolumer.dds")) { m_GIVolumeR = TextureObject.CreateTexture3DFromFile(device, VolumeSizeX, VolumeSizeY, VolumeSizeZ, Format.R16G16B16A16_Float, "textures\\givolumer.dds"); m_GIVolumeG = TextureObject.CreateTexture3DFromFile(device, VolumeSizeX, VolumeSizeY, VolumeSizeZ, Format.R16G16B16A16_Float, "textures\\givolumeg.dds"); m_GIVolumeB = TextureObject.CreateTexture3DFromFile(device, VolumeSizeX, VolumeSizeY, VolumeSizeZ, Format.R16G16B16A16_Float, "textures\\givolumeb.dds"); } else { m_GIVolumeR = TextureObject.CreateTexture3D(device, VolumeSizeX, VolumeSizeY, VolumeSizeZ, Format.R16G16B16A16_Float); m_GIVolumeG = TextureObject.CreateTexture3D(device, VolumeSizeX, VolumeSizeY, VolumeSizeZ, Format.R16G16B16A16_Float); m_GIVolumeB = TextureObject.CreateTexture3D(device, VolumeSizeX, VolumeSizeY, VolumeSizeZ, Format.R16G16B16A16_Float); } m_InitialSHSet = RenderTargetSet.CreateRenderTargetSet(device, 128, 128, Format.R16G16B16A16_Float, 3, false); m_Downsampled16x16SHSetR = RenderTargetSet.CreateRenderTargetSet(device, 32, 32, Format.R16G16B16A16_Float, 1, false); m_Downsampled16x16SHSetG = RenderTargetSet.CreateRenderTargetSet(device, 32, 32, Format.R16G16B16A16_Float, 1, false); m_Downsampled16x16SHSetB = RenderTargetSet.CreateRenderTargetSet(device, 32, 32, Format.R16G16B16A16_Float, 1, false); m_Downsampled4x4SHSetR = RenderTargetSet.CreateRenderTargetSet(device, 8, 8, Format.R16G16B16A16_Float, 1, false); m_Downsampled4x4SHSetG = RenderTargetSet.CreateRenderTargetSet(device, 8, 8, Format.R16G16B16A16_Float, 1, false); m_Downsampled4x4SHSetB = RenderTargetSet.CreateRenderTargetSet(device, 8, 8, Format.R16G16B16A16_Float, 1, false); m_GIConstantBuffer = ShaderManager.CreateConstantBufferInstance("GIConstantBuffer", device); m_SceneBoundsMin = sceneBoundsMin; m_SceneBoundsMax = sceneBoundsMax; }
public VolumetricFog() { m_DensityTexture = null; m_LightingTexturePing = null; m_LightingTexturePong = null; m_ScatteringTexture = null; m_Phase = false; }
public static void ExecuteComputeForResource(DeviceContext context, TextureObject textureResource, string shader) { ShaderWrapper wrapper = m_Shaders[shader]; context.ComputeShader.Set(wrapper.m_ComputeShader); context.Dispatch( (textureResource.m_Width + wrapper.m_ThreadsX - 1) / wrapper.m_ThreadsX, (textureResource.m_Height + wrapper.m_ThreadsY - 1) / wrapper.m_ThreadsY, (textureResource.m_Depth + wrapper.m_ThreadsZ - 1) / wrapper.m_ThreadsZ); }
public void Initialize(Device device) { m_VolumeX = (int)ShaderManager.GetUIntShaderDefine("VOLUME_WIDTH"); m_VolumeY = (int)ShaderManager.GetUIntShaderDefine("VOLUME_HEIGHT"); m_VolumeZ = (int)ShaderManager.GetUIntShaderDefine("VOLUME_DEPTH"); m_DensityTexture = TextureObject.CreateTexture3D(device, m_VolumeX, m_VolumeY, m_VolumeZ, Format.R16_Float); m_LightingTexturePing = TextureObject.CreateTexture3D(device, m_VolumeX, m_VolumeY, m_VolumeZ, Format.R16G16B16A16_Float); m_LightingTexturePong = TextureObject.CreateTexture3D(device, m_VolumeX, m_VolumeY, m_VolumeZ, Format.R16G16B16A16_Float); m_ScatteringTexture = TextureObject.CreateTexture3D(device, m_VolumeX, m_VolumeY, m_VolumeZ, Format.R16G16B16A16_Float); }
public static RenderTargetSet CreateRenderTargetSet(Device device, int width, int height, Format format, int numSurfaces, bool needsDepth) { RenderTargetSet rt = new RenderTargetSet(); rt.m_Descriptor = new RenderTargetDescriptor() { m_Format = format, m_HasDepth = needsDepth, m_Height = height, m_NumSurfaces = numSurfaces, m_Width = width }; rt.m_NumRTs = numSurfaces; for (int i = 0; i < numSurfaces; ++i) { rt.m_RenderTargets[i] = TextureObject.CreateTexture(device, width, height, 1, format, false, true); } if (needsDepth) { rt.m_DepthStencil = TextureObject.CreateTexture(device, width, height, 1, Format.R32_Typeless, true, false); } rt.m_Viewport = new Viewport(0, 0, width, height, 0.0f, 1.0f); if (m_DepthStencilState == null) { DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less, }; m_DepthStencilState = DepthStencilState.FromDescription(device, dsStateDesc); dsStateDesc = new DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.Always, }; m_DepthStencilStateNoDepth = DepthStencilState.FromDescription(device, dsStateDesc); } return(rt); }
// Static functions static public TextureObject CreateTexture3DFromFile(Device device, int width, int height, int depth, Format format, string fileName) { TextureObject newTexture = new TextureObject(); // Variables newTexture.m_Width = width; newTexture.m_Height = height; newTexture.m_Depth = depth; newTexture.m_TexFormat = format; newTexture.m_Mips = 1; BindFlags bindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget | BindFlags.UnorderedAccess; ImageLoadInformation imageLoadInfo = new ImageLoadInformation() { BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, Width = width, Depth = depth, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default }; newTexture.m_TextureObject3D = Texture3D.FromFile(device, fileName, imageLoadInfo); ShaderResourceViewDescription srvViewDesc = new ShaderResourceViewDescription { ArraySize = 0, Format = format, Dimension = ShaderResourceViewDimension.Texture3D, Flags = 0, FirstArraySlice = 0, MostDetailedMip = 0, MipLevels = 1, }; newTexture.m_ShaderResourceView = new ShaderResourceView(device, newTexture.m_TextureObject3D, srvViewDesc); newTexture.m_RenderTargetView = new RenderTargetView(device, newTexture.m_TextureObject3D); newTexture.m_UnorderedAccessView = new UnorderedAccessView(device, newTexture.m_TextureObject3D); return(newTexture); }
public RenderTargetSet() { for (int i = 0; i < MAX_RENDERTARGETS; ++i) { m_RenderTargets[i] = null; } for (int i = 0; i < MAX_UAVS; ++i) { m_UAVs[i] = null; } m_DepthStencil = null; m_NumRTs = 0; m_UAVsNum = 0; m_Viewport = new Viewport(); }
public RenderTargetSet() { for (int i = 0; i < MAX_RENDERTARGETS; ++i ) { m_RenderTargets[i] = null; } for (int i = 0; i < MAX_UAVS; ++i) { m_UAVs[i] = null; } m_DepthStencil = null; m_NumRTs = 0; m_UAVsNum = 0; m_Viewport = new Viewport(); }
public static void GenerateCubemapMips(DeviceContext context, string shader, TextureObject targetTexture, TextureObject sourceTexture) { for (int m = 0; m < targetTexture.m_Mips; ++m) { for (int i = 0; i < 6; ++i) { dynamic cdb = m_CubemapDownsampleBuffer; cdb.g_Mip = (float)m; cdb.g_Face = i; m_CubemapDownsampleBuffer.CompileAndBind(context); RenderTargetSet.BindTextureAsRenderTarget(context, targetTexture.m_ArrayRenderTargetViews[m * 6 + i], null, targetTexture.m_Width >> m, targetTexture.m_Height >> m); context.PixelShader.SetShaderResource(PostEffectHelper.m_RandomNumbersBuffer.m_ShaderResourceView, 39); context.PixelShader.SetShaderResource(sourceTexture.m_ShaderResourceView, 0); PostEffectHelper.RenderFullscreenTriangle(context, shader); RenderTargetSet.BindNull(context); } } }
public void RenderVolumetricFog(DeviceContext context, TextureObject shadowMap, GlobalIlluminationRenderer giRenderer) { // Not using temporal manager as it was designed for render target sets; TODO TextureObject previousFrameAccumulationTexture = m_Phase ? m_LightingTexturePing : m_LightingTexturePong; TextureObject currentFrameAccumulationTexture = m_Phase ? m_LightingTexturePong : m_LightingTexturePing; m_Phase = !m_Phase; using (new GpuProfilePoint(context, "VolumetricFog")) { #if false using (new GpuProfilePoint(context, "DensityEstimation")) { context.ComputeShader.SetUnorderedAccessView(m_DensityTexture.m_UnorderedAccessView, 0); ShaderManager.ExecuteComputeForResource(context, m_DensityTexture, "CalculateDensity"); ContextHelper.ClearCSContext(context); } #endif using (new GpuProfilePoint(context, "LitFogVolume")) { context.ComputeShader.SetShaderResource(giRenderer.m_GIVolumeR.m_ShaderResourceView, 5); context.ComputeShader.SetShaderResource(giRenderer.m_GIVolumeG.m_ShaderResourceView, 6); context.ComputeShader.SetShaderResource(giRenderer.m_GIVolumeB.m_ShaderResourceView, 7); context.ComputeShader.SetShaderResource(previousFrameAccumulationTexture.m_ShaderResourceView, 8); context.ComputeShader.SetShaderResource(m_DensityTexture.m_ShaderResourceView, 0); context.ComputeShader.SetShaderResource(shadowMap.m_ShaderResourceView, 1); context.ComputeShader.SetUnorderedAccessView(currentFrameAccumulationTexture.m_UnorderedAccessView, 1); ShaderManager.ExecuteComputeForResource(context, currentFrameAccumulationTexture, "LitFogVolume"); ContextHelper.ClearCSContext(context); } using (new GpuProfilePoint(context, "ComputeScattering")) { context.ComputeShader.SetShaderResource(currentFrameAccumulationTexture.m_ShaderResourceView, 0); context.ComputeShader.SetUnorderedAccessView(m_ScatteringTexture.m_UnorderedAccessView, 2); ShaderManager.ExecuteComputeForSize(context, m_VolumeX, m_VolumeY, 1, "ComputeScattering"); ContextHelper.ClearCSContext(context); } } }
public GlobalIlluminationRenderer(Device device, Vector3 sceneBoundsMin, Vector3 sceneBoundsMax) { m_VolumeSizeX = (int)ShaderManager.GetUIntShaderDefine("GI_VOLUME_RESOLUTION_X"); m_VolumeSizeY = (int)ShaderManager.GetUIntShaderDefine("GI_VOLUME_RESOLUTION_Y"); m_VolumeSizeZ = (int)ShaderManager.GetUIntShaderDefine("GI_VOLUME_RESOLUTION_Z"); m_CubeObject = TextureObject.CreateCubeTexture(device, 128, 128, 1, Format.R16G16B16A16_Float, false, true); m_CubeObjectDepth = TextureObject.CreateCubeTexture(device, 128, 128, 1, Format.R32_Typeless, true, true); if (System.IO.File.Exists("textures\\givolumer.dds")) { m_GIVolumeR = TextureObject.CreateTexture3DFromFile(device, m_VolumeSizeX, m_VolumeSizeY, m_VolumeSizeZ, Format.R16G16B16A16_Float, "textures\\givolumer.dds"); m_GIVolumeG = TextureObject.CreateTexture3DFromFile(device, m_VolumeSizeX, m_VolumeSizeY, m_VolumeSizeZ, Format.R16G16B16A16_Float, "textures\\givolumeg.dds"); m_GIVolumeB = TextureObject.CreateTexture3DFromFile(device, m_VolumeSizeX, m_VolumeSizeY, m_VolumeSizeZ, Format.R16G16B16A16_Float, "textures\\givolumeb.dds"); } else { m_GIVolumeR = TextureObject.CreateTexture3D(device, m_VolumeSizeX, m_VolumeSizeY, m_VolumeSizeZ, Format.R16G16B16A16_Float); m_GIVolumeG = TextureObject.CreateTexture3D(device, m_VolumeSizeX, m_VolumeSizeY, m_VolumeSizeZ, Format.R16G16B16A16_Float); m_GIVolumeB = TextureObject.CreateTexture3D(device, m_VolumeSizeX, m_VolumeSizeY, m_VolumeSizeZ, Format.R16G16B16A16_Float); } m_InitialSHSet = RenderTargetSet.CreateRenderTargetSet(device, 128, 128, Format.R16G16B16A16_Float, 3, false); m_Downsampled16x16SHSetR = RenderTargetSet.CreateRenderTargetSet(device, 32, 32, Format.R16G16B16A16_Float, 1, false); m_Downsampled16x16SHSetG = RenderTargetSet.CreateRenderTargetSet(device, 32, 32, Format.R16G16B16A16_Float, 1, false); m_Downsampled16x16SHSetB = RenderTargetSet.CreateRenderTargetSet(device, 32, 32, Format.R16G16B16A16_Float, 1, false); m_Downsampled4x4SHSetR = RenderTargetSet.CreateRenderTargetSet(device, 8, 8, Format.R16G16B16A16_Float, 1, false); m_Downsampled4x4SHSetG = RenderTargetSet.CreateRenderTargetSet(device, 8, 8, Format.R16G16B16A16_Float, 1, false); m_Downsampled4x4SHSetB = RenderTargetSet.CreateRenderTargetSet(device, 8, 8, Format.R16G16B16A16_Float, 1, false); m_GIConstantBuffer = ShaderManager.CreateConstantBufferInstance("GIConstantBuffer", device); m_SceneBoundsMin = sceneBoundsMin; m_SceneBoundsMax = sceneBoundsMax; }
// Static functions static public TextureObject CreateCubeTexture(Device device, int width, int height, int mips, Format format, bool isDepthStencil, bool needsGpuWrite) { if (isDepthStencil && format != Format.R32_Typeless && format != Format.R24G8_Typeless) { throw new Exception("Unsupported depth format"); } TextureObject newTexture = new TextureObject(); // Variables newTexture.m_Width = width; newTexture.m_Height = height; newTexture.m_TexFormat = format; newTexture.m_Mips = mips; newTexture.m_IsCube = true; newTexture.m_ArraySize = 6; BindFlags bindFlags = BindFlags.ShaderResource; if (isDepthStencil) bindFlags |= BindFlags.DepthStencil; if (needsGpuWrite && !isDepthStencil) bindFlags |= BindFlags.RenderTarget; if (needsGpuWrite && !isDepthStencil) bindFlags |= BindFlags.UnorderedAccess; Texture2DDescription textureDescription = new Texture2DDescription { ArraySize = 6, BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, Width = width, MipLevels = mips, OptionFlags = ResourceOptionFlags.TextureCube | ((mips > 1 && needsGpuWrite) ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None), SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; newTexture.m_TextureObject2D = new Texture2D(device, textureDescription); Format srvFormat = format; // Special case for depth if (isDepthStencil) { srvFormat = (format == Format.R32_Typeless) ? Format.R32_Float : Format.R24_UNorm_X8_Typeless; } ShaderResourceViewDescription srvViewDesc = new ShaderResourceViewDescription { ArraySize = 6, Format = srvFormat, Dimension = ShaderResourceViewDimension.Texture2DArray, Flags = 0, FirstArraySlice = 0, MostDetailedMip = 0, MipLevels = mips }; newTexture.m_ShaderResourceView = new ShaderResourceView(device, newTexture.m_TextureObject2D, srvViewDesc); newTexture.m_ArrayShaderResourceViews = new ShaderResourceView[6]; for (int i = 0; i < 6; ++ i ) { srvViewDesc.ArraySize = 1; srvViewDesc.FirstArraySlice = i; newTexture.m_ArrayShaderResourceViews[i] = new ShaderResourceView(device, newTexture.m_TextureObject2D, srvViewDesc); } if (isDepthStencil) { DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription { ArraySize = 6, Format = (format == Format.R32_Typeless) ? Format.D32_Float : Format.D24_UNorm_S8_UInt, Dimension = DepthStencilViewDimension.Texture2DArray, MipSlice = 0, Flags = 0, FirstArraySlice = 0 }; newTexture.m_DepthStencilView = new DepthStencilView(device, newTexture.m_TextureObject2D, dsViewDesc); newTexture.m_ArrayDepthStencilViews = new DepthStencilView[6]; for (int i = 0; i < 6; ++i) { dsViewDesc.ArraySize = 1; dsViewDesc.FirstArraySlice = i; newTexture.m_ArrayDepthStencilViews[i] = new DepthStencilView(device, newTexture.m_TextureObject2D, dsViewDesc); } } // No RTV for depth stencil, sorry if (needsGpuWrite && !isDepthStencil) { newTexture.m_RenderTargetView = new RenderTargetView(device, newTexture.m_TextureObject2D); RenderTargetViewDescription rtvDesc = new RenderTargetViewDescription { ArraySize = 6, Dimension = RenderTargetViewDimension.Texture2DArray, FirstArraySlice = 0, Format = format, }; newTexture.m_ArrayRenderTargetViews = new RenderTargetView[6]; for (int i = 0; i < 6; ++i) { rtvDesc.ArraySize = 1; rtvDesc.FirstArraySlice = i; newTexture.m_ArrayRenderTargetViews[i] = new RenderTargetView(device, newTexture.m_TextureObject2D, rtvDesc); } UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription { ArraySize = 6, Dimension = UnorderedAccessViewDimension.Texture2DArray, FirstArraySlice = 0, Format = format, }; newTexture.m_UnorderedAccessView = new UnorderedAccessView(device, newTexture.m_TextureObject2D, uavDesc); newTexture.m_ArrayUnorderedAccessViews = new UnorderedAccessView[6]; for (int i = 0; i < 6; ++i) { uavDesc.ArraySize = 1; uavDesc.FirstArraySlice = i; newTexture.m_ArrayUnorderedAccessViews[i] = new UnorderedAccessView(device, newTexture.m_TextureObject2D, uavDesc); } } return newTexture; }
public static void RenderCubemap(DeviceContext context, TextureObject targetTexture, TextureObject targetDepth, SimpleSceneWrapper sceneWrapper, Vector3 position, float range, bool depthOnly = false, Color4 clearColor = new Color4()) { //using (new GpuProfilePoint(context, "CubemapRendering")) { // some hardcoded near plane Matrix projectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 2.0f, 1.0f, 0.05f, range); ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.DepthCompare); // set the shaders context.VertexShader.Set(ShaderManager.GetVertexShader("VertexScene")); context.PixelShader.Set(depthOnly ? null : ShaderManager.GetPixelShader("PixelSceneSimple")); dynamic cvpb = m_CurrentViewportBuffer; for (int i = 0; i < 6; ++i) { Vector3 lookAt = new Vector3(); Vector3 upVec = new Vector3(); switch (i) { case 0: lookAt = new Vector3(1.0f, 0.0f, 0.0f); upVec = new Vector3(0.0f, 1.0f, 0.0f); break; case 1: lookAt = new Vector3(-1.0f, 0.0f, 0.0f); upVec = new Vector3(0.0f, 1.0f, 0.0f); break; case 2: lookAt = new Vector3(0.0f, 1.0f, 0.0f); upVec = new Vector3(0.0f, 0.0f, -1.0f); break; case 3: lookAt = new Vector3(0.0f, -1.0f, 0.0f); upVec = new Vector3(0.0f, 0.0f, 1.0f); break; case 4: lookAt = new Vector3(0.0f, 0.0f, 1.0f); upVec = new Vector3(0.0f, 1.0f, 0.0f); break; case 5: lookAt = new Vector3(0.0f, 0.0f, -1.0f); upVec = new Vector3(0.0f, 1.0f, 0.0f); break; } Matrix viewMatrix = Matrix.LookAtLH(position, position + lookAt, upVec); cvpb.g_ProjMatrix = projectionMatrix; cvpb.g_ViewMatrix = viewMatrix; cvpb.g_ViewProjMatrix = viewMatrix * projectionMatrix; { context.ClearRenderTargetView(targetTexture.m_ArrayRenderTargetViews[i], clearColor); context.ClearDepthStencilView(targetDepth.m_ArrayDepthStencilViews[i], DepthStencilClearFlags.Depth, 1.0f, 0); RenderTargetSet.BindTextureAsRenderTarget(context, targetTexture.m_ArrayRenderTargetViews[i], targetDepth.m_ArrayDepthStencilViews[i], targetTexture.m_Width, targetTexture.m_Height); m_CurrentViewportBuffer.CompileAndBind(context); // render triangles sceneWrapper.Render(context); RenderTargetSet.BindNull(context); } } ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.NoDepth); } }
// Static functions static public TextureObject CreateTexture(Device device, int width, int height, int mips, Format format, bool isDepthStencil, bool needsGpuWrite) { if (isDepthStencil && format != Format.R32_Typeless && format != Format.R24G8_Typeless) { throw new Exception("Unsupported depth format"); } TextureObject newTexture = new TextureObject(); // Variables newTexture.m_Width = width; newTexture.m_Height = height; newTexture.m_TexFormat = format; newTexture.m_Mips = mips; BindFlags bindFlags = BindFlags.ShaderResource; if (isDepthStencil) { bindFlags |= BindFlags.DepthStencil; } if (needsGpuWrite && !isDepthStencil) { bindFlags |= BindFlags.RenderTarget; } if (needsGpuWrite && !isDepthStencil) { bindFlags |= BindFlags.UnorderedAccess; } Texture2DDescription textureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, Width = width, MipLevels = mips, OptionFlags = (mips > 1 && needsGpuWrite) ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; newTexture.m_TextureObject2D = new Texture2D(device, textureDescription); Format srvFormat = format; // Special case for depth if (isDepthStencil) { srvFormat = (format == Format.R32_Typeless) ? Format.R32_Float : Format.R24_UNorm_X8_Typeless; } ShaderResourceViewDescription srvViewDesc = new ShaderResourceViewDescription { ArraySize = 0, Format = srvFormat, Dimension = ShaderResourceViewDimension.Texture2D, Flags = 0, FirstArraySlice = 0, MostDetailedMip = 0, MipLevels = mips }; newTexture.m_ShaderResourceView = new ShaderResourceView(device, newTexture.m_TextureObject2D, srvViewDesc); if (isDepthStencil) { DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription { ArraySize = 0, Format = (format == Format.R32_Typeless) ? Format.D32_Float : Format.D24_UNorm_S8_UInt, Dimension = DepthStencilViewDimension.Texture2D, MipSlice = 0, Flags = 0, FirstArraySlice = 0 }; newTexture.m_DepthStencilView = new DepthStencilView(device, newTexture.m_TextureObject2D, dsViewDesc); } // No RTV for depth stencil, sorry if (needsGpuWrite && !isDepthStencil) { newTexture.m_RenderTargetView = new RenderTargetView(device, newTexture.m_TextureObject2D); UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription { ArraySize = 0, DepthSliceCount = 1, Dimension = UnorderedAccessViewDimension.Texture2D, ElementCount = 1, FirstArraySlice = 0, FirstDepthSlice = 0, FirstElement = 0, Flags = UnorderedAccessViewBufferFlags.None, Format = format, MipSlice = 0 }; newTexture.m_UnorderedAccessView = new UnorderedAccessView(device, newTexture.m_TextureObject2D, uavDesc); } return(newTexture); }
public void Initialize(Device device, Form form, Panel panel, int resolutionX, int resolutionY) { m_ResolutionX = resolutionX; m_ResolutionY = resolutionY; m_FullResDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16B16A16_Float, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }; m_FullResAndDepthDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16B16A16_Float, m_HasDepth = true, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }; m_ResolvedColorDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R8G8B8A8_UNorm, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }; m_SSAODescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R8_UNorm, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY / 2, m_Width = resolutionX / 2 }; m_HalfResDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16B16A16_Float, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY / 2, m_Width = resolutionX / 2 }; m_SimpleSceneWrapper.Initialize(device, "sponza"); TemporalSurfaceManager.InitializeRenderTarget("SceneMainRender", m_FullResAndDepthDescriptor); TemporalSurfaceManager.InitializeRenderTarget("ResolvedColor", m_ResolvedColorDescriptor ); TemporalSurfaceManager.InitializeRenderTarget("MotionVectors", new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16_Float, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }); m_PostEffectsConstantBuffer = ShaderManager.CreateConstantBufferInstance("PostEffects", device); m_ForwardPassBuffer = ShaderManager.CreateConstantBufferInstance("ForwardPassBuffer", device); m_ViewportConstantBuffer = ShaderManager.CreateConstantBufferInstance("GlobalViewportBuffer", device); m_CurrentViewportBuffer = ShaderManager.CreateConstantBufferInstance("CurrentViewport", device); Vector3 min, max; m_SimpleSceneWrapper.GetSceneBounds(out min, out max); m_GIRenderer = new GlobalIlluminationRenderer(device, min, max); // Init passes m_ScatterDOFPass = new ScatterDOFPass(); m_ScatterDOFPass.Initialize(device, resolutionX, resolutionY); m_ResolveHDRPass = new ResolveHDRPass(); m_ResolveMotionVectorsPass = new ResolveMotionVectorsPass(); m_ResolveTemporalPass = new ResolveTemporalMotionBasedPass(); m_FxaaPass = new FxaaPass(); m_DepthOperationsPass = new DepthOperationsPass(resolutionX, resolutionY); m_SSAOPass = new SSAOEffectPass(device, resolutionX / 2, resolutionY / 2); m_SSReflectionsPass = new SSReflectionsEffectPass(device, resolutionX / 2, resolutionY / 2); m_LuminanceCalculations = new LuminanceCalculations(device, resolutionX, resolutionY); m_ShadowEVSMGenerator = new ShadowEVSMGenerator(); m_VolumetricFog = new VolumetricFog(); m_VolumetricFog.Initialize(device); m_StartTime = DateTime.Now; m_ViewportCamera.BindToInput(form, panel); POISSON_SAMPLES = new float[POISSON_SAMPLE_NUM * 2] { 0.655897408497f, 0.95575996511f, 0.944576716895f, 0.455478901428f, 0.203683172197f, 0.0177149729234f, 0.383628747896f, 0.379284571357f, 0.945473563065f, 0.779431977074f, 0.570420562284f, 0.576156483093f, 0.674354533289f, 0.286346887653f, 0.347776132801f, 0.734938485879f, }; m_CubeObject = TextureObject.CreateCubeTexture(device, 256, 256, 1, Format.R16G16B16A16_Float, false, true); m_CubeObjectFiltered = TextureObject.CreateCubeTexture(device, 256, 256, 7, Format.R16G16B16A16_Float, false, true); m_CubeDepthObject = TextureObject.CreateCubeTexture(device, 256, 256, 1, Format.R32_Typeless, true, true); }
public static void ExecuteComputeForResource(DeviceContext context, TextureObject textureResource, string shader) { BindDebugUAV(context); ShaderWrapper wrapper = m_Shaders[shader]; context.ComputeShader.Set((ComputeShader)wrapper.m_ShaderObject); context.Dispatch( (textureResource.m_Width + wrapper.m_ThreadsX - 1) / wrapper.m_ThreadsX, (textureResource.m_Height + wrapper.m_ThreadsY - 1) / wrapper.m_ThreadsY, (textureResource.m_Depth + wrapper.m_ThreadsZ - 1) / wrapper.m_ThreadsZ); }
public void AddUAV(TextureObject uavSource) { m_UAVs[m_UAVsNum++] = uavSource; }
// Static functions static public TextureObject CreateCubeTexture(Device device, int width, int height, int mips, Format format, bool isDepthStencil, bool needsGpuWrite) { if (isDepthStencil && format != Format.R32_Typeless && format != Format.R24G8_Typeless) { throw new Exception("Unsupported depth format"); } TextureObject newTexture = new TextureObject(); // Variables newTexture.m_Width = width; newTexture.m_Height = height; newTexture.m_TexFormat = format; newTexture.m_Mips = mips; newTexture.m_IsCube = true; newTexture.m_ArraySize = 6; BindFlags bindFlags = BindFlags.ShaderResource; if (isDepthStencil) { bindFlags |= BindFlags.DepthStencil; } if (needsGpuWrite && !isDepthStencil) { bindFlags |= BindFlags.RenderTarget; } if (needsGpuWrite && !isDepthStencil) { bindFlags |= BindFlags.UnorderedAccess; } Texture2DDescription textureDescription = new Texture2DDescription { ArraySize = 6, BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, Width = width, MipLevels = mips, OptionFlags = ResourceOptionFlags.TextureCube | ((mips > 1 && needsGpuWrite) ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None), SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; newTexture.m_TextureObject2D = new Texture2D(device, textureDescription); Format srvFormat = format; // Special case for depth if (isDepthStencil) { srvFormat = (format == Format.R32_Typeless) ? Format.R32_Float : Format.R24_UNorm_X8_Typeless; } ShaderResourceViewDescription srvViewDesc = new ShaderResourceViewDescription { Format = srvFormat, Dimension = ShaderResourceViewDimension.TextureCube, Flags = 0, FirstArraySlice = 0, MostDetailedMip = 0, MipLevels = mips, ArraySize = 6, }; newTexture.m_ShaderResourceView = new ShaderResourceView(device, newTexture.m_TextureObject2D, srvViewDesc); newTexture.m_ArrayShaderResourceViews = new ShaderResourceView[6]; for (int i = 0; i < 6; ++i) { srvViewDesc.ArraySize = 1; srvViewDesc.FirstArraySlice = i; newTexture.m_ArrayShaderResourceViews[i] = new ShaderResourceView(device, newTexture.m_TextureObject2D, srvViewDesc); } if (isDepthStencil) { DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription { ArraySize = 6, Format = (format == Format.R32_Typeless) ? Format.D32_Float : Format.D24_UNorm_S8_UInt, Dimension = DepthStencilViewDimension.Texture2DArray, MipSlice = 0, Flags = 0, FirstArraySlice = 0 }; newTexture.m_DepthStencilView = new DepthStencilView(device, newTexture.m_TextureObject2D, dsViewDesc); newTexture.m_ArrayDepthStencilViews = new DepthStencilView[6]; for (int i = 0; i < 6; ++i) { dsViewDesc.ArraySize = 1; dsViewDesc.FirstArraySlice = i; newTexture.m_ArrayDepthStencilViews[i] = new DepthStencilView(device, newTexture.m_TextureObject2D, dsViewDesc); } } // No RTV for depth stencil, sorry if (needsGpuWrite && !isDepthStencil) { newTexture.m_RenderTargetView = new RenderTargetView(device, newTexture.m_TextureObject2D); RenderTargetViewDescription rtvDesc = new RenderTargetViewDescription { ArraySize = 6, Dimension = RenderTargetViewDimension.Texture2DArray, FirstArraySlice = 0, Format = format, }; newTexture.m_ArrayRenderTargetViews = new RenderTargetView[6 * mips]; for (int m = 0; m < mips; ++m) { for (int i = 0; i < 6; ++i) { rtvDesc.MipSlice = m; rtvDesc.ArraySize = 1; rtvDesc.FirstArraySlice = i; newTexture.m_ArrayRenderTargetViews[6 * m + i] = new RenderTargetView(device, newTexture.m_TextureObject2D, rtvDesc); } } UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription { ArraySize = 6, Dimension = UnorderedAccessViewDimension.Texture2DArray, FirstArraySlice = 0, Format = format, }; newTexture.m_UnorderedAccessView = new UnorderedAccessView(device, newTexture.m_TextureObject2D, uavDesc); newTexture.m_ArrayUnorderedAccessViews = new UnorderedAccessView[6 * mips]; for (int m = 0; m < mips; ++m) { for (int i = 0; i < 6; ++i) { uavDesc.ArraySize = 1; uavDesc.FirstArraySlice = i; uavDesc.MipSlice = m; newTexture.m_ArrayUnorderedAccessViews[6 * m + i] = new UnorderedAccessView(device, newTexture.m_TextureObject2D, uavDesc); } } } return(newTexture); }
public void Initialize(Device device, Form form, Panel panel, int resolutionX, int resolutionY) { m_ResolutionX = resolutionX; m_ResolutionY = resolutionY; m_FullResDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16B16A16_Float, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }; m_FullResAndDepthDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16B16A16_Float, m_HasDepth = true, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }; m_ResolvedColorDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R8G8B8A8_UNorm, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }; m_SSAODescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R8_UNorm, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY / 2, m_Width = resolutionX / 2 }; m_HalfResDescriptor = new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16B16A16_Float, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY / 2, m_Width = resolutionX / 2 }; m_SimpleSceneWrapper.Initialize(device, "sponza"); TemporalSurfaceManager.InitializeRenderTarget("SceneMainRender", m_FullResAndDepthDescriptor); TemporalSurfaceManager.InitializeRenderTarget("ResolvedColor", m_ResolvedColorDescriptor); TemporalSurfaceManager.InitializeRenderTarget("MotionVectors", new RenderTargetSet.RenderTargetDescriptor() { m_Format = Format.R16G16_Float, m_HasDepth = false, m_NumSurfaces = 1, m_Height = resolutionY, m_Width = resolutionX }); m_PostEffectsConstantBuffer = ShaderManager.CreateConstantBufferInstance("PostEffects", device); m_ForwardPassBuffer = ShaderManager.CreateConstantBufferInstance("ForwardPassBuffer", device); m_ViewportConstantBuffer = ShaderManager.CreateConstantBufferInstance("GlobalViewportBuffer", device); m_CurrentViewportBuffer = ShaderManager.CreateConstantBufferInstance("CurrentViewport", device); Vector3 min, max; m_SimpleSceneWrapper.GetSceneBounds(out min, out max); m_GIRenderer = new GlobalIlluminationRenderer(device, min, max); // Init passes m_ScatterDOFPass = new ScatterDOFPass(); m_ScatterDOFPass.Initialize(device, resolutionX, resolutionY); m_ResolveHDRPass = new ResolveHDRPass(); m_ResolveMotionVectorsPass = new ResolveMotionVectorsPass(); m_ResolveTemporalPass = new ResolveTemporalMotionBasedPass(); m_FxaaPass = new FxaaPass(); m_DepthOperationsPass = new DepthOperationsPass(resolutionX, resolutionY); m_SSAOPass = new SSAOEffectPass(device, resolutionX / 2, resolutionY / 2); m_SSReflectionsPass = new SSReflectionsEffectPass(device, resolutionX / 2, resolutionY / 2); m_LuminanceCalculations = new LuminanceCalculations(device, resolutionX, resolutionY); m_ShadowEVSMGenerator = new ShadowEVSMGenerator(); m_VolumetricFog = new VolumetricFog(); m_VolumetricFog.Initialize(device); m_StartTime = DateTime.Now; m_ViewportCamera.BindToInput(form, panel); POISSON_SAMPLES = new float[POISSON_SAMPLE_NUM * 2] { 0.655897408497f, 0.95575996511f, 0.944576716895f, 0.455478901428f, 0.203683172197f, 0.0177149729234f, 0.383628747896f, 0.379284571357f, 0.945473563065f, 0.779431977074f, 0.570420562284f, 0.576156483093f, 0.674354533289f, 0.286346887653f, 0.347776132801f, 0.734938485879f, }; m_CubeObject = TextureObject.CreateCubeTexture(device, 256, 256, 1, Format.R16G16B16A16_Float, false, true); m_CubeObjectFiltered = TextureObject.CreateCubeTexture(device, 256, 256, 7, Format.R16G16B16A16_Float, false, true); m_CubeDepthObject = TextureObject.CreateCubeTexture(device, 256, 256, 1, Format.R32_Typeless, true, true); }
// Static functions static public TextureObject CreateTexture3DFromFile(Device device, int width, int height, int depth, Format format, string fileName) { TextureObject newTexture = new TextureObject(); // Variables newTexture.m_Width = width; newTexture.m_Height = height; newTexture.m_Depth = depth; newTexture.m_TexFormat = format; newTexture.m_Mips = 1; BindFlags bindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget | BindFlags.UnorderedAccess; ImageLoadInformation imageLoadInfo = new ImageLoadInformation() { BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, Width = width, Depth = depth, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default }; newTexture.m_TextureObject3D = Texture3D.FromFile(device, fileName, imageLoadInfo); ShaderResourceViewDescription srvViewDesc = new ShaderResourceViewDescription { ArraySize = 0, Format = format, Dimension = ShaderResourceViewDimension.Texture3D, Flags = 0, FirstArraySlice = 0, MostDetailedMip = 0, MipLevels = 1, }; newTexture.m_ShaderResourceView = new ShaderResourceView(device, newTexture.m_TextureObject3D, srvViewDesc); newTexture.m_RenderTargetView = new RenderTargetView(device, newTexture.m_TextureObject3D); newTexture.m_UnorderedAccessView = new UnorderedAccessView(device, newTexture.m_TextureObject3D); return newTexture; }
// Static functions static public TextureObject CreateTexture(Device device, int width, int height, int mips, Format format, bool isDepthStencil, bool needsGpuWrite) { if (isDepthStencil && format != Format.R32_Typeless && format != Format.R24G8_Typeless) { throw new Exception("Unsupported depth format"); } TextureObject newTexture = new TextureObject(); // Variables newTexture.m_Width = width; newTexture.m_Height = height; newTexture.m_TexFormat = format; newTexture.m_Mips = mips; BindFlags bindFlags = BindFlags.ShaderResource; if (isDepthStencil) bindFlags |= BindFlags.DepthStencil; if (needsGpuWrite && !isDepthStencil) bindFlags |= BindFlags.RenderTarget; if (needsGpuWrite && !isDepthStencil) bindFlags |= BindFlags.UnorderedAccess; Texture2DDescription textureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, Width = width, MipLevels = mips, OptionFlags = (mips > 1 && needsGpuWrite) ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; newTexture.m_TextureObject2D = new Texture2D(device, textureDescription); Format srvFormat = format; // Special case for depth if (isDepthStencil) { srvFormat = (format == Format.R32_Typeless) ? Format.R32_Float : Format.R24_UNorm_X8_Typeless; } ShaderResourceViewDescription srvViewDesc = new ShaderResourceViewDescription { ArraySize = 0, Format = srvFormat, Dimension = ShaderResourceViewDimension.Texture2D, Flags = 0, FirstArraySlice = 0, MostDetailedMip = 0, MipLevels = mips }; newTexture.m_ShaderResourceView = new ShaderResourceView(device, newTexture.m_TextureObject2D, srvViewDesc); if (isDepthStencil) { DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription { ArraySize = 0, Format = (format == Format.R32_Typeless) ? Format.D32_Float : Format.D24_UNorm_S8_UInt, Dimension = DepthStencilViewDimension.Texture2D, MipSlice = 0, Flags = 0, FirstArraySlice = 0 }; newTexture.m_DepthStencilView = new DepthStencilView(device, newTexture.m_TextureObject2D, dsViewDesc); } // No RTV for depth stencil, sorry if (needsGpuWrite && !isDepthStencil) { newTexture.m_RenderTargetView = new RenderTargetView(device, newTexture.m_TextureObject2D); UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription { ArraySize = 0, DepthSliceCount = 1, Dimension = UnorderedAccessViewDimension.Texture2D, ElementCount = 1, FirstArraySlice = 0, FirstDepthSlice = 0, FirstElement = 0, Flags = UnorderedAccessViewBufferFlags.None, Format = format, MipSlice = 0 }; newTexture.m_UnorderedAccessView = new UnorderedAccessView(device, newTexture.m_TextureObject2D, uavDesc); } return newTexture; }