internal static void CreateScreenResources() { var width = m_resolution.X; var height = m_resolution.Y; var samples = RenderSettings.AntialiasingMode.SamplesCount(); if (MyGBuffer.Main == null) { MyGBuffer.Main = new MyGBuffer(); } MyGBuffer.Main.Resize(width, height, samples, 0); MyScreenDependants.Resize(width, height, samples, 0); RemoveScreenResources(); m_resolvedLight = new MyRenderTarget(width, height, Format.R11G11B10_Float, 1, 0); m_reduce0 = new MyUnorderedAccessTexture(width, height, Format.R32G32_Float); m_reduce0.SetDebugName("reduce0"); m_reduce1 = new MyUnorderedAccessTexture(width, height, Format.R32G32_Float); m_reduce1.SetDebugName("reduce1"); m_uav3 = new MyUnorderedAccessTexture(width, height, Format.R11G11B10_Float); m_localLum = new MyUnorderedAccessTexture( (width + MyLuminanceAverage.NumThreads - 1) / MyLuminanceAverage.NumThreads, (height + MyLuminanceAverage.NumThreads - 1) / MyLuminanceAverage.NumThreads, Format.R32_Float); m_div2 = new MyUnorderedAccessTexture(width / 2, height / 2, Format.R11G11B10_Float); m_div4 = new MyUnorderedAccessTexture(width / 4, height / 4, Format.R11G11B10_Float); m_div8 = new MyUnorderedAccessTexture(width / 8, height / 8, Format.R11G11B10_Float); m_div8_1 = new MyUnorderedAccessTexture(width / 8, height / 8, Format.R11G11B10_Float); m_rgba8_linear = new MyUnorderedAccessTexture(width, height, Format.R8G8B8A8_UNorm); m_rgba8_0 = new MyCustomTexture(width, height, BindFlags.RenderTarget | BindFlags.ShaderResource, Format.R8G8B8A8_Typeless); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.SrvView }); m_rgba8_1 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); m_rgba8_2 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); if (samples > 1) { m_rgba8_ms = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, samples, 0); } m_prevLum = new MyUnorderedAccessTexture(1, 1, Format.R32G32_Float); Debug.Assert(m_shadowsHelper == RwTexId.NULL); m_shadowsHelper = MyRwTextures.CreateUav2D(width, height, Format.R8_UNorm, "cascade shadows gather"); m_shadowsHelper1 = MyRwTextures.CreateUav2D(width, height, Format.R8_UNorm, "cascade shadows gather 2"); m_gbuffer1Copy = MyRwTextures.CreateScratch2D(width, height, Format.R8G8B8A8_UNorm, samples, 0, "gbuffer 1 copy"); }
internal static void RemoveScreenResources() { if (m_resolvedLight != null) { m_resolvedLight.Release(); m_reduce0.Release(); m_reduce1.Release(); m_uav3.Release(); m_cloudLayerFogTargetUAV.Release(); m_localLum.Release(); m_div2.Release(); m_div4.Release(); m_div8.Release(); m_div8_1.Release(); m_rgba8_linear.Release(); m_rgba8_0.Release(); m_rgba8_1.Release(); m_rgba8_2.Release(); if (m_rgba8_ms != null) { m_rgba8_ms.Release(); m_rgba8_ms = null; } m_prevLum.Release(); MyRwTextures.Destroy(ref m_shadowsHelper); MyRwTextures.Destroy(ref m_shadowsHelper1); MyRwTextures.Destroy(ref m_gbuffer1Copy); } }
internal static void CreateScreenResources() { var width = m_resolution.X; var height = m_resolution.Y; var samples = RenderSettings.AntialiasingMode.SamplesCount(); MyUtils.Init(ref MyGBuffer.Main); MyGBuffer.Main.Resize(width, height, samples, 0); MyScreenDependants.Resize(width, height, samples, 0); RemoveScreenResources(); m_reduce0 = new MyUnorderedAccessTexture(width, height, Format.R32G32_Float); m_reduce0.SetDebugName("reduce0"); m_reduce1 = new MyUnorderedAccessTexture(width, height, Format.R32G32_Float); m_reduce1.SetDebugName("reduce1"); m_uav3 = new MyUnorderedAccessTexture(width, height, MyGBuffer.LBufferFormat); m_transparencyAccum = new MyUnorderedAccessTexture(width, height, Format.R16G16B16A16_Float); m_transparencyCoverage = new MyUnorderedAccessTexture(width, height, Format.R8_UNorm); HalfScreenUavHDR = new MyUnorderedAccessTexture(width / 2, height / 2, MyGBuffer.LBufferFormat); QuarterScreenUavHDR = new MyUnorderedAccessTexture(width / 4, height / 4, MyGBuffer.LBufferFormat); EighthScreenUavHDR = new MyUnorderedAccessTexture(width / 8, height / 8, MyGBuffer.LBufferFormat); EighthScreenUavHDRHelper = new MyUnorderedAccessTexture(width / 8, height / 8, MyGBuffer.LBufferFormat); m_rgba8_linear = new MyUnorderedAccessTexture(width, height, Format.R8G8B8A8_UNorm); m_rgba8_0 = new MyCustomTexture(width, height, BindFlags.RenderTarget | BindFlags.ShaderResource, Format.R8G8B8A8_Typeless); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.SrvView }); m_rgba8_1 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); m_rgba8_2 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); if (samples > 1) { m_rgba8_ms = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, samples, 0); } m_prevLum = new MyUnorderedAccessTexture(1, 1, Format.R32G32_Float); Debug.Assert(PostProcessedShadows == RwTexId.NULL); Debug.Assert(CascadesHelper == RwTexId.NULL); PostProcessedShadows = MyRwTextures.CreateUavRenderTarget(width, height, Format.R8_UNorm); CascadesHelper = MyRwTextures.CreateRenderTarget(width, height, Format.R8_UNorm); m_gbuffer1Copy = MyRwTextures.CreateScratch2D(width, height, Format.R8G8B8A8_UNorm, samples, 0, "gbuffer 1 copy"); }
internal static void RemoveScreenResources() { if (m_reduce0 != null) { m_reduce0.Release(); m_reduce1.Release(); m_uav3.Release(); HalfScreenUavHDR.Release(); QuarterScreenUavHDR.Release(); EighthScreenUavHDR.Release(); EighthScreenUavHDRHelper.Release(); m_rgba8_linear.Release(); m_rgba8_0.Release(); m_rgba8_1.Release(); m_rgba8_2.Release(); if (m_rgba8_ms != null) { m_rgba8_ms.Release(); m_rgba8_ms = null; } m_prevLum.Release(); m_transparencyAccum.Release(); m_transparencyCoverage.Release(); MyRwTextures.Destroy(ref PostProcessedShadows); MyRwTextures.Destroy(ref CascadesHelper); MyRwTextures.Destroy(ref m_gbuffer1Copy); } if (m_backbufferCopyResource != null) { m_backbufferCopyResource.Release(); m_backbufferCopyResource = null; } if (m_lastScreenDataResource != null && m_lastScreenDataResource != Backbuffer) { m_lastScreenDataResource.Release(); m_lastScreenDataResource = null; } if (m_lastDataStream != null) { m_lastDataStream.Dispose(); m_lastDataStream = null; } }
internal static void Resize(int width, int height, int samplesNum, int samplesQuality) { if (m_resolvedDepth != null) { m_resolvedDepth.Release(); m_ambientOcclusionHelper.Release(); m_ambientOcclusion.Release(); m_tileIndexes.Release(); } m_resolvedDepth = new MyDepthStencil(width, height, 1, 0); m_ambientOcclusionHelper = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm, 1, 0); m_ambientOcclusion = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm, 1, 0); int tilesNum = ((width + MyLightRendering.TILE_SIZE - 1) / MyLightRendering.TILE_SIZE) * ((height + MyLightRendering.TILE_SIZE - 1) / MyLightRendering.TILE_SIZE); TilesNum = tilesNum; TilesX = (width + MyLightRendering.TILE_SIZE - 1) / MyLightRendering.TILE_SIZE; m_tileIndexes = new MyRWStructuredBuffer(tilesNum + tilesNum * MyRender11Constants.MAX_POINT_LIGHTS, sizeof(uint)); }
internal static void Resize(int width, int height, int samplesNum, int samplesQuality) { if (m_resolvedDepth != null) { m_resolvedDepth.Release(); m_ambientOcclusionHelper.Release(); m_ambientOcclusion.Release(); m_tileIndices.Release(); } m_resolvedDepth = new MyDepthStencil(width, height, 1, 0); m_ambientOcclusionHelper = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm, 1, 0); m_ambientOcclusion = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm, 1, 0); TilesX = (width + MyLightRendering.TILE_SIZE - 1) / MyLightRendering.TILE_SIZE; TilesY = ((height + MyLightRendering.TILE_SIZE - 1) / MyLightRendering.TILE_SIZE); TilesNum = TilesX * TilesY; m_tileIndices = new MyRWStructuredBuffer(TilesNum + TilesNum * MyRender11Constants.MAX_POINT_LIGHTS, sizeof(uint), MyRWStructuredBuffer.UAVType.Default, true, "MyScreenDependants::tileIndices"); }
private static void TakeCustomSizedScreenshot(Vector2 rescale) { var resCpy = m_resolution; m_resolution = new Vector2I(resCpy * rescale); CreateScreenResources(); m_finalImage = DrawGameScene(null); m_resetEyeAdaptation = true; var surface = new MyRenderTarget(m_finalImage.GetSize().X, m_finalImage.GetSize().Y, SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb, 1, 0); MyCopyToRT.Run(surface, m_finalImage); MyCopyToRT.ClearAlpha(surface); SaveScreenshotFromResource(surface.m_resource); surface.Release(); m_resolution = resCpy; CreateScreenResources(); }
internal static void CreateScreenResources() { var width = m_resolution.X; var height = m_resolution.Y; var samples = RenderSettings.AntialiasingMode.SamplesCount(); if(MyGBuffer.Main == null) { MyGBuffer.Main = new MyGBuffer(); } MyGBuffer.Main.Resize(width, height, samples, 0); MyScreenDependants.Resize(width, height, samples, 0); RemoveScreenResources(); m_resolvedLight = new MyRenderTarget(width, height, Format.R11G11B10_Float, 1, 0); m_reduce0 = new MyUnorderedAccessTexture(width, height, Format.R32_Float); m_reduce0.SetDebugName("reduce0"); m_reduce1 = new MyUnorderedAccessTexture(width, height, Format.R32_Float); m_reduce1.SetDebugName("reduce1"); m_uav3 = new MyUnorderedAccessTexture(width, height, Format.R11G11B10_Float); m_localLum = new MyUnorderedAccessTexture( (width + MyLuminanceAverage.NumThreads - 1) / MyLuminanceAverage.NumThreads, (height + MyLuminanceAverage.NumThreads - 1) / MyLuminanceAverage.NumThreads, Format.R32_Float); m_div2 = new MyUnorderedAccessTexture(width / 2, height / 2, Format.R11G11B10_Float); m_div4 = new MyUnorderedAccessTexture(width / 4, height / 4, Format.R11G11B10_Float); m_div8 = new MyUnorderedAccessTexture(width / 8, height / 8, Format.R11G11B10_Float); m_div8_1 = new MyUnorderedAccessTexture(width / 8, height / 8, Format.R11G11B10_Float); m_rgba8_linear = new MyUnorderedAccessTexture(width, height, Format.R8G8B8A8_UNorm); m_rgba8_0 = new MyCustomTexture(width, height, BindFlags.RenderTarget | BindFlags.ShaderResource, Format.R8G8B8A8_Typeless); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.SrvView }); m_rgba8_1 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); m_prevLum = new MyUnorderedAccessTexture(1, 1, Format.R32_Float); Debug.Assert(m_shadowsHelper == RwTexId.NULL); m_shadowsHelper = MyRwTextures.CreateUav2D(width, height, Format.R8_UNorm, "cascade shadows gather"); m_shadowsHelper1 = MyRwTextures.CreateUav2D(width, height, Format.R8_UNorm, "cascade shadows gather 2"); }
internal static void CreateScreenResources() { var width = m_resolution.X; var height = m_resolution.Y; var samples = RenderSettings.AntialiasingMode.SamplesCount(); MyUtils.Init(ref MyGBuffer.Main); MyGBuffer.Main.Resize(width, height, samples, 0); MyScreenDependants.Resize(width, height, samples, 0); RemoveScreenResources(); m_reduce0 = new MyUnorderedAccessTexture(width, height, Format.R32G32_Float); m_reduce0.SetDebugName("reduce0"); m_reduce1 = new MyUnorderedAccessTexture(width, height, Format.R32G32_Float); m_reduce1.SetDebugName("reduce1"); m_uav3 = new MyUnorderedAccessTexture(width, height, MyGBuffer.LBufferFormat); HalfScreenUavHDR = new MyUnorderedAccessTexture(width / 2, height / 2, MyGBuffer.LBufferFormat); QuarterScreenUavHDR = new MyUnorderedAccessTexture(width / 4, height / 4, MyGBuffer.LBufferFormat); EighthScreenUavHDR = new MyUnorderedAccessTexture(width / 8, height / 8, MyGBuffer.LBufferFormat); EighthScreenUavHDRHelper = new MyUnorderedAccessTexture(width / 8, height / 8, MyGBuffer.LBufferFormat); m_rgba8_linear = new MyUnorderedAccessTexture(width, height, Format.R8G8B8A8_UNorm); m_rgba8_0 = new MyCustomTexture(width, height, BindFlags.RenderTarget | BindFlags.ShaderResource, Format.R8G8B8A8_Typeless); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.RtvView }); m_rgba8_0.AddView(new MyViewKey { Fmt = Format.R8G8B8A8_UNorm_SRgb, View = MyViewEnum.SrvView }); m_rgba8_1 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); m_rgba8_2 = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, 1, 0); if (samples > 1) { m_rgba8_ms = new MyRenderTarget(width, height, Format.R8G8B8A8_UNorm_SRgb, samples, 0); } m_prevLum = new MyUnorderedAccessTexture(1, 1, Format.R32G32_Float); Debug.Assert(PostProcessedShadows == RwTexId.NULL); Debug.Assert(CascadesHelper == RwTexId.NULL); PostProcessedShadows = MyRwTextures.CreateUavRenderTarget(width, height, Format.R8_UNorm); CascadesHelper = MyRwTextures.CreateRenderTarget(width, height, Format.R8_UNorm); m_gbuffer1Copy = MyRwTextures.CreateScratch2D(width, height, Format.R8G8B8A8_UNorm, samples, 0, "gbuffer 1 copy"); }
private unsafe static byte[] GetScreenData(Vector2I resolution, byte[] screenData = null) { const uint headerPadding = 256; // Need to allocate some space for the bitmap headers const uint bytesPerPixel = 4; uint imageSizeInBytes = (uint)(resolution.Size() * bytesPerPixel); uint dataSizeInBytes = imageSizeInBytes + headerPadding; byte[] returnData = null; if(screenData == null) screenData = new byte[imageSizeInBytes]; else if(screenData.Length != imageSizeInBytes) { Debug.Fail("Preallocated buffer for GetScreenData incorrect size: " + imageSizeInBytes.ToString() + " expected, " + screenData.Length + " received"); return returnData; } MyBindableResource imageSource = Backbuffer; MyBindableResource imageSourceResourceView = null; if (imageSource != null && (resolution.X != imageSource.GetSize().X || resolution.Y != imageSource.GetSize().Y)) { MyViewKey viewKey = new MyViewKey { View = MyViewEnum.SrvView, Fmt = MyRender11Constants.DX11_BACKBUFFER_FORMAT }; if(m_backbufferCopyResource == null) { m_backbufferCopyResource = new MyCustomTexture(m_resolution.X, m_resolution.Y, BindFlags.ShaderResource, MyRender11Constants.DX11_BACKBUFFER_FORMAT); m_backbufferCopyResource.AddView(viewKey); } MyRenderContext.Immediate.DeviceContext.CopyResource(imageSource.m_resource, m_backbufferCopyResource.m_resource); imageSource = m_backbufferCopyResource; imageSourceResourceView = m_backbufferCopyResource.GetView(viewKey); } if (imageSource == null) return returnData; if(imageSizeInBytes > int.MaxValue) { Debug.Fail("Image size too large to read!"); return returnData; } MyBindableResource imageResource = imageSource; bool shouldResize = imageSourceResourceView != null; if (shouldResize) { imageResource = m_lastScreenDataResource; if (imageResource == null || (imageResource.GetSize().X != resolution.X || imageResource.GetSize().Y != resolution.Y)) { if (m_lastScreenDataResource != null && (m_lastScreenDataResource != m_backbufferCopyResource && m_lastScreenDataResource != Backbuffer)) { m_lastScreenDataResource.Release(); } m_lastScreenDataResource = null; imageResource = new MyRenderTarget(resolution.X, resolution.Y, MyRender11Constants.DX11_BACKBUFFER_FORMAT, 1, 0); } var RC = MyRenderContext.Immediate; var deviceContext = RC.DeviceContext; deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; deviceContext.Rasterizer.SetViewport(0, 0, resolution.X, resolution.Y); deviceContext.PixelShader.Set(MyDebugRenderer.BlitTextureShader); deviceContext.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, imageResource); RC.BindSRV(0, imageSourceResourceView); MyDebugRenderer.DrawQuad(0, 0, resolution.X, resolution.Y, MyScreenPass.QuadVS); //MyCopyToRT.Run(imageResource, imageSourceResourceView, new MyViewport(resolution)); } m_lastScreenDataResource = imageResource; Stream dataStream = m_lastDataStream; if (m_lastDataStream == null || m_lastDataStream.Length != dataSizeInBytes) { if (m_lastDataStream != null) { m_lastDataStream.Dispose(); m_lastDataStream = null; } dataStream = new DataStream((int)dataSizeInBytes, true, true); dataStream.Seek(0, SeekOrigin.Begin); } m_lastDataStream = dataStream; Resource.ToStream(MyRenderContext.Immediate.DeviceContext, imageResource.m_resource, ImageFileFormat.Bmp, dataStream); if (!(dataStream.CanRead && dataStream.CanSeek)) { Debug.Fail("Screen data stream does not support the necessary operations to get the data"); return returnData; } fixed (byte* dataPointer = screenData) { GetBmpDataFromStream(dataStream, dataPointer, imageSizeInBytes); } returnData = screenData; if (m_lastDataStream != null) m_lastDataStream.Seek(0, SeekOrigin.Begin); return returnData; }
private unsafe static byte[] GetScreenData(Vector2I resolution, byte[] screenData = null) { const uint headerPadding = 256; // Need to allocate some space for the bitmap headers const uint bytesPerPixel = 4; uint imageSizeInBytes = (uint)(resolution.Size() * bytesPerPixel); uint dataSizeInBytes = imageSizeInBytes + headerPadding; byte[] returnData = null; if (screenData == null) { screenData = new byte[imageSizeInBytes]; } else if (screenData.Length != imageSizeInBytes) { Debug.Fail("Preallocated buffer for GetScreenData incorrect size: " + imageSizeInBytes.ToString() + " expected, " + screenData.Length * bytesPerPixel + " received"); return(returnData); } MyBindableResource imageSource = Backbuffer; if (imageSource == null) { return(returnData); } if (imageSizeInBytes > int.MaxValue) { Debug.Fail("Image size too large to read!"); return(returnData); } MyBindableResource imageResource = imageSource; if (resolution.X != imageResource.GetSize().X || resolution.Y != imageResource.GetSize().Y) { imageResource = m_lastScreenDataResource; if (imageResource == null || (imageResource.GetSize().X != resolution.X || imageResource.GetSize().Y != resolution.Y)) { if (m_lastScreenDataResource != null && m_lastScreenDataResource != Backbuffer) { m_lastScreenDataResource.Release(); } m_lastScreenDataResource = null; imageResource = new MyRenderTarget(resolution.X, resolution.Y, MyRender11Constants.DX11_BACKBUFFER_FORMAT, 0, 0); } MyCopyToRT.Run(imageResource, imageSource, new MyViewport(resolution)); } m_lastScreenDataResource = imageResource; Stream dataStream = m_lastDataStream; if (m_lastDataStream == null || m_lastDataStream.Length != dataSizeInBytes) { if (m_lastDataStream != null) { m_lastDataStream.Dispose(); m_lastDataStream = null; } dataStream = new DataStream((int)dataSizeInBytes, true, true); } m_lastDataStream = dataStream; Resource.ToStream(MyRenderContext.Immediate.DeviceContext, imageResource.m_resource, ImageFileFormat.Bmp, dataStream); if (!(dataStream.CanRead && dataStream.CanSeek)) { Debug.Fail("Screen data stream does not support the necessary operations to get the data"); return(returnData); } fixed(byte *dataPointer = screenData) { GetBmpDataFromStream(dataStream, dataPointer, imageSizeInBytes); } returnData = screenData; if (m_lastDataStream != null) { m_lastDataStream.Seek(0, SeekOrigin.Begin); } return(returnData); }
private unsafe static byte[] GetScreenData(Vector2I resolution, byte[] screenData = null) { const uint headerPadding = 256; // Need to allocate some space for the bitmap headers const uint bytesPerPixel = 4; uint imageSizeInBytes = (uint)(resolution.Size() * bytesPerPixel); uint dataSizeInBytes = imageSizeInBytes + headerPadding; byte[] returnData = null; if(screenData == null) screenData = new byte[imageSizeInBytes]; else if(screenData.Length != imageSizeInBytes) { Debug.Fail("Preallocated buffer for GetScreenData incorrect size: " + imageSizeInBytes.ToString() + " expected, " + screenData.Length * bytesPerPixel + " received"); return returnData; } MyBindableResource imageSource = Backbuffer; if (imageSource == null) return returnData; if(imageSizeInBytes > int.MaxValue) { Debug.Fail("Image size too large to read!"); return returnData; } MyBindableResource imageResource = imageSource; if (resolution.X != imageResource.GetSize().X || resolution.Y != imageResource.GetSize().Y) { imageResource = m_lastScreenDataResource; if (imageResource == null || (imageResource.GetSize().X != resolution.X || imageResource.GetSize().Y != resolution.Y)) { if (m_lastScreenDataResource != null && m_lastScreenDataResource != Backbuffer) { m_lastScreenDataResource.Release(); } m_lastScreenDataResource = null; imageResource = new MyRenderTarget(resolution.X, resolution.Y, MyRender11Constants.DX11_BACKBUFFER_FORMAT, 0, 0); } MyCopyToRT.Run(imageResource, imageSource, new MyViewport(resolution)); } m_lastScreenDataResource = imageResource; Stream dataStream = m_lastDataStream; if (m_lastDataStream == null || m_lastDataStream.Length != dataSizeInBytes) { if (m_lastDataStream != null) { m_lastDataStream.Dispose(); m_lastDataStream = null; } dataStream = new DataStream((int)dataSizeInBytes, true, true); } m_lastDataStream = dataStream; Resource.ToStream(MyRenderContext.Immediate.DeviceContext, imageResource.m_resource, ImageFileFormat.Bmp, dataStream); if (!(dataStream.CanRead && dataStream.CanSeek)) { Debug.Fail("Screen data stream does not support the necessary operations to get the data"); return returnData; } fixed (byte* dataPointer = screenData) { GetBmpDataFromStream(dataStream, dataPointer, imageSizeInBytes); } returnData = screenData; if (m_lastDataStream != null) m_lastDataStream.Seek(0, SeekOrigin.Begin); return returnData; }
internal static void RenderColoredTextures(List<renderColoredTextureProperties> texturesToRender) { if (texturesToRender.Count == 0) return; if (!m_initialized) Init(); const int RENDER_TEXTURE_RESOLUTION = 512; RC.DeviceContext.OutputMerger.BlendState = null; RC.SetIL(null); RC.SetPS(m_ps); RC.SetCB(0, MyCommon.FrameConstants); RC.SetCB(1, m_cb); Dictionary<Vector2I, MyRenderTarget> createdRenderTextureTargets = new Dictionary<Vector2I, MyRenderTarget>(); foreach (var texture in texturesToRender) { TexId texId = MyTextures.GetTexture(texture.TextureName, MyTextureEnum.COLOR_METAL, true); if (texId == TexId.NULL) continue; Vector2 texSize = MyTextures.GetSize(texId); Vector2I renderTargetResolution = new Vector2I(RENDER_TEXTURE_RESOLUTION, RENDER_TEXTURE_RESOLUTION); if (texSize.Y > 0) { if (texSize.Y < RENDER_TEXTURE_RESOLUTION) { renderTargetResolution.X = (int)texSize.X; renderTargetResolution.Y = (int)texSize.Y; } else { renderTargetResolution.X *= (int)(texSize.X / texSize.Y); } } MyViewport viewport = new MyViewport(renderTargetResolution.X, renderTargetResolution.Y); MyRenderTarget renderTexture = null; if (!createdRenderTextureTargets.TryGetValue(renderTargetResolution, out renderTexture)) { renderTexture = new MyRenderTarget(renderTargetResolution.X, renderTargetResolution.Y, SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb, 1, 0); createdRenderTextureTargets[renderTargetResolution] = renderTexture; } RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, renderTexture); // Set color var mapping = MyMapping.MapDiscard(m_cb); Vector4 color = new Vector4(texture.ColorMaskHSV, 1); mapping.WriteAndPosition(ref color); mapping.Unmap(); // Set texture RC.DeviceContext.PixelShader.SetShaderResource(0, MyTextures.GetView(texId)); // Draw MyScreenPass.DrawFullscreenQuad(viewport); // Save to file MyTextureData.ToFile(renderTexture.GetHWResource(), texture.PathToSave, ImageFileFormat.Png); } texturesToRender.Clear(); foreach (var texture in createdRenderTextureTargets) { texture.Value.Release(); } createdRenderTextureTargets.Clear(); RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, null); RC.BindGBufferForRead(0, MyGBuffer.Main); }
internal static void RemoveScreenResources() { if (m_reduce0 != null) { m_reduce0.Release(); m_reduce1.Release(); m_uav3.Release(); HalfScreenUavHDR.Release(); QuarterScreenUavHDR.Release(); EighthScreenUavHDR.Release(); EighthScreenUavHDRHelper.Release(); m_rgba8_linear.Release(); m_rgba8_0.Release(); m_rgba8_1.Release(); m_rgba8_2.Release(); if (m_rgba8_ms != null) { m_rgba8_ms.Release(); m_rgba8_ms = null; } m_prevLum.Release(); MyRwTextures.Destroy(ref PostProcessedShadows); MyRwTextures.Destroy(ref CascadesHelper); MyRwTextures.Destroy(ref m_gbuffer1Copy); } if(m_lastScreenDataResource != null && m_lastScreenDataResource != Backbuffer) { m_lastScreenDataResource.Release(); m_lastScreenDataResource = null; } if(m_lastDataStream != null) { m_lastDataStream.Dispose(); m_lastDataStream = null; } }
private static void TakeCustomSizedScreenshot(Vector2 rescale) { var resCpy = m_resolution; m_resolution = new Vector2I(resCpy * rescale); CreateScreenResources(); DrawGameScene(false); m_resetEyeAdaptation = true; // uav3 stores final colors var surface = new MyRenderTarget(m_finalImage.GetSize().X, m_finalImage.GetSize().Y, SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb, 1, 0); MyCopyToRT.Run(surface, m_finalImage); MyCopyToRT.ClearAlpha(surface); SaveScreenshotFromResource(surface.m_resource); surface.Release(); m_resolution = resCpy; CreateScreenResources(); }
private unsafe static byte[] GetScreenData(Vector2I resolution, byte[] screenData = null) { const uint headerPadding = 256; // Need to allocate some space for the bitmap headers const uint bytesPerPixel = 4; uint imageSizeInBytes = (uint)(resolution.Size() * bytesPerPixel); uint dataSizeInBytes = imageSizeInBytes + headerPadding; byte[] returnData = null; if (screenData == null) { screenData = new byte[imageSizeInBytes]; } else if (screenData.Length != imageSizeInBytes) { Debug.Fail("Preallocated buffer for GetScreenData incorrect size: " + imageSizeInBytes.ToString() + " expected, " + screenData.Length + " received"); return(returnData); } MyBindableResource imageSource = Backbuffer; MyBindableResource imageSourceResourceView = null; if (imageSource != null && (resolution.X != imageSource.GetSize().X || resolution.Y != imageSource.GetSize().Y)) { MyViewKey viewKey = new MyViewKey { View = MyViewEnum.SrvView, Fmt = MyRender11Constants.DX11_BACKBUFFER_FORMAT }; if (m_backbufferCopyResource == null) { m_backbufferCopyResource = new MyCustomTexture(m_resolution.X, m_resolution.Y, BindFlags.ShaderResource, MyRender11Constants.DX11_BACKBUFFER_FORMAT); m_backbufferCopyResource.AddView(viewKey); } MyRenderContext.Immediate.DeviceContext.CopyResource(imageSource.m_resource, m_backbufferCopyResource.m_resource); imageSource = m_backbufferCopyResource; imageSourceResourceView = m_backbufferCopyResource.GetView(viewKey); } if (imageSource == null) { return(returnData); } if (imageSizeInBytes > int.MaxValue) { Debug.Fail("Image size too large to read!"); return(returnData); } MyBindableResource imageResource = imageSource; bool shouldResize = imageSourceResourceView != null; if (shouldResize) { imageResource = m_lastScreenDataResource; if (imageResource == null || (imageResource.GetSize().X != resolution.X || imageResource.GetSize().Y != resolution.Y)) { if (m_lastScreenDataResource != null && (m_lastScreenDataResource != m_backbufferCopyResource && m_lastScreenDataResource != Backbuffer)) { m_lastScreenDataResource.Release(); } m_lastScreenDataResource = null; imageResource = new MyRenderTarget(resolution.X, resolution.Y, MyRender11Constants.DX11_BACKBUFFER_FORMAT, 1, 0); } var RC = MyRenderContext.Immediate; var deviceContext = RC.DeviceContext; deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; deviceContext.Rasterizer.SetViewport(0, 0, resolution.X, resolution.Y); deviceContext.PixelShader.Set(MyDebugRenderer.BlitTextureShader); deviceContext.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.BindDepthRT(null, DepthStencilAccess.ReadWrite, imageResource); RC.BindSRV(0, imageSourceResourceView); MyDebugRenderer.DrawQuad(0, 0, resolution.X, resolution.Y, MyScreenPass.QuadVS); //MyCopyToRT.Run(imageResource, imageSourceResourceView, new MyViewport(resolution)); } m_lastScreenDataResource = imageResource; Stream dataStream = m_lastDataStream; if (m_lastDataStream == null || m_lastDataStream.Length != dataSizeInBytes) { if (m_lastDataStream != null) { m_lastDataStream.Dispose(); m_lastDataStream = null; } dataStream = new DataStream((int)dataSizeInBytes, true, true); dataStream.Seek(0, SeekOrigin.Begin); } m_lastDataStream = dataStream; Resource.ToStream(MyRenderContext.Immediate.DeviceContext, imageResource.m_resource, ImageFileFormat.Bmp, dataStream); if (!(dataStream.CanRead && dataStream.CanSeek)) { Debug.Fail("Screen data stream does not support the necessary operations to get the data"); return(returnData); } fixed(byte *dataPointer = screenData) { GetBmpDataFromStream(dataStream, dataPointer, imageSizeInBytes); } returnData = screenData; if (m_lastDataStream != null) { m_lastDataStream.Seek(0, SeekOrigin.Begin); } return(returnData); }