internal static void Run(MyBindableResource dst, MyBindableResource src, MyBindableResource avgLum, MyBindableResource bloom, bool enableTonemapping = true) { //Debug.Assert(src.GetSize() == dst.GetSize()); var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.stream.Write(MyRender11.Settings.MiddleGrey); mapping.stream.Write(MyRender11.Settings.LuminanceExposure); mapping.stream.Write(MyRender11.Settings.BloomExposure); mapping.stream.Write(MyRender11.Settings.BloomMult); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.BindUAV(0, dst); RC.BindSRV(0, src, avgLum, bloom); RC.Context.ComputeShader.SetSampler(0, MyRender11.m_defaultSamplerState); if (enableTonemapping) { RC.SetCS(m_cs); } else { RC.SetCS(m_csSkip); } var size = dst.GetSize(); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(null); }
internal static void PreparePointLights() { var activePointlights = 0; MyLights.Update(); MyLights.PointlightsBvh.OverlapAllFrustum(ref MyEnvironment.ViewFrustumClippedD, VisiblePointlights); bool visiblePointlights = VisiblePointlights.Count != 0; if (!visiblePointlights && !m_lastFrameVisiblePointlights) { return; } m_lastFrameVisiblePointlights = visiblePointlights; if (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.Sort((x, y) => x.ViewerDistanceSquared.CompareTo(y.ViewerDistanceSquared)); while (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.RemoveAtFast(VisiblePointlights.Count - 1); } } foreach (var light in VisiblePointlights) { MyLights.WritePointlightConstants(light, ref m_pointlightsCullBuffer[activePointlights]); activePointlights++; Debug.Assert(activePointlights <= MyRender11Constants.MAX_POINT_LIGHTS); } for (int lightIndex = activePointlights; lightIndex < MyRender11Constants.MAX_POINT_LIGHTS; ++lightIndex) { MyLights.WritePointlightConstants(LightId.NULL, ref m_pointlightsCullBuffer[lightIndex]); } var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.WriteAndPosition(ref activePointlights); mapping.Unmap(); mapping = MyMapping.MapDiscard(m_pointlightCullHwBuffer.Buffer); mapping.WriteAndPosition(m_pointlightsCullBuffer, 0, MyRender11Constants.MAX_POINT_LIGHTS); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); //RC.BindUAV(0, MyScreenDependants.m_test); RC.BindUAV(0, MyScreenDependants.m_tileIndices); RC.BindGBufferForRead(0, MyGBuffer.Main); RC.CSBindRawSRV(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer.Srv); RC.SetCS(m_preparePointLights); RC.DeviceContext.Dispatch(MyScreenDependants.TilesX, MyScreenDependants.TilesY, 1); RC.SetCS(null); }
internal static void PreparePointLights() { var activePointlights = 0; MyLights.Update(); MyLights.PointlightsBvh.OverlapAllFrustum(ref MyEnvironment.ViewFrustumClippedD, VisiblePointlights); if (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.Sort((x, y) => x.ViewerDistanceSquared.CompareTo(y.ViewerDistanceSquared)); while (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.RemoveAtFast(VisiblePointlights.Count - 1); } } foreach (var light in VisiblePointlights) { MyLights.WritePointlightConstants(light, ref m_pointlightsCullBuffer[activePointlights]); activePointlights++; Debug.Assert(activePointlights <= MyRender11Constants.MAX_POINT_LIGHTS); } var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.stream.Write(activePointlights); mapping.stream.Write(0f); mapping.stream.Write(0f); mapping.stream.Write(0f); mapping.Unmap(); mapping = MyMapping.MapDiscard(m_pointlightCullHwBuffer.Buffer); for (int i = 0; i < activePointlights; i++) { mapping.stream.Write(m_pointlightsCullBuffer[i]); } for (int i = activePointlights; i < MyRender11Constants.MAX_POINT_LIGHTS; i++) { mapping.stream.Write(new MyPointlightConstants()); } mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); //RC.BindUAV(0, MyScreenDependants.m_test); RC.BindUAV(0, MyScreenDependants.m_tileIndexes); RC.BindGBufferForRead(0, MyGBuffer.Main); RC.CSBindRawSRV(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer.Srv); RC.SetCS(m_preparePointLights); var size = MyRender11.ViewportResolution; RC.Context.Dispatch((size.X + TILE_SIZE - 1) / TILE_SIZE, (size.Y + TILE_SIZE - 1) / TILE_SIZE, 1); RC.SetCS(null); }
internal static MyBindableResource Run(MyBindableResource src, MyBindableResource avgLum) { RC.CSSetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.BindUAV(0, MyRender11.HalfScreenUavHDR); RC.BindSRVs(0, src, avgLum); RC.SetCS(m_bloomShader); var size = MyRender11.HalfScreenUavHDR.GetSize(); VRageMath.Vector2I threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads); RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1); RC.SetCS(m_downscaleShader); size = MyRender11.QuarterScreenUavHDR.GetSize(); threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads); RC.BindUAV(0, MyRender11.QuarterScreenUavHDR); RC.BindSRV(0, MyRender11.HalfScreenUavHDR); RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1); size = MyRender11.EighthScreenUavHDR.GetSize(); threadGroups = new VRageMath.Vector2I((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads); RC.BindUAV(0, MyRender11.EighthScreenUavHDR); RC.BindSRV(0, MyRender11.QuarterScreenUavHDR); RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1); RC.SetCS(m_blurV); RC.BindUAV(0, MyRender11.EighthScreenUavHDRHelper); RC.BindSRV(0, MyRender11.EighthScreenUavHDR); RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1); RC.SetCS(m_blurH); RC.BindUAV(0, MyRender11.EighthScreenUavHDR); RC.BindSRV(0, MyRender11.EighthScreenUavHDRHelper); int nPasses = 1; if (MyStereoRender.Enable) { threadGroups.X /= 2; nPasses = 2; } for (int nPass = 0; nPass < nPasses; nPass++) { MyStereoRegion region = MyStereoRegion.FULLSCREEN; if (MyStereoRender.Enable) { region = nPass == 0 ? MyStereoRegion.LEFT : MyStereoRegion.RIGHT; } RC.CSSetCB(1, GetCB_blurH(region, size)); RC.DeviceContext.Dispatch(threadGroups.X, threadGroups.Y, 1); } return(MyRender11.EighthScreenUavHDR); }
// Per-frame emission of particles into the GPU simulation private static void Emit(int emitterCount, MyGPUEmitterData[] emitterData) { // update emitter data var mapping = MyMapping.MapDiscard(m_emitterStructuredBuffer.Buffer); int maxParticlesToEmitThisFrame = 0; for (int i = 0; i < emitterCount; i++) { mapping.WriteAndPosition(ref emitterData[i]); if (emitterData[i].NumParticlesToEmitThisFrame > maxParticlesToEmitThisFrame) { maxParticlesToEmitThisFrame = emitterData[i].NumParticlesToEmitThisFrame; } } mapping.Unmap(); int numThreadGroupsX = align(maxParticlesToEmitThisFrame, MAX_PARTICLE_EMIT_THREADS) / MAX_PARTICLE_EMIT_THREADS; int numThreadGroupsY = align(emitterCount, MAX_EMITTERS) / MAX_EMITTERS; // update emitter count mapping = MyMapping.MapDiscard(m_emitterConstantBuffer); mapping.WriteAndPosition(ref emitterCount); mapping.WriteAndPosition(ref numThreadGroupsX); mapping.Unmap(); if (maxParticlesToEmitThisFrame > 0) { // Set resources but don't reset any atomic counters RC.BindUAV(0, m_particleBuffer); RC.BindUAV(1, m_deadListBuffer); RC.BindUAV(2, m_skippedParticleCountBuffer, 0); RC.CSSetCB(1, m_emitterConstantBuffer); RC.CSBindRawSRV(0, Resources.MyTextures.RandomTexId); RC.CSBindRawSRV(1, m_emitterStructuredBuffer); RC.SetCS(m_csEmit); RC.DeviceContext.Dispatch(numThreadGroupsX, numThreadGroupsY, 1); RC.CSSetCB(1, null); RC.SetCS(m_csEmitSkipFix); // Disaptch a set of 1d thread groups to fill out the dead list, one thread per particle RC.DeviceContext.Dispatch(1, 1, 1); } #if DEBUG //m_numDeadParticlesAfterEmit = ReadCounter(m_deadListBuffer); #endif }
internal static void Prefilter(RwTexId probe, RwTexId prefiltered) { RC.DeviceContext.ComputeShader.Set(m_prefilter); var mipLevels = prefiltered.Description2d.MipLevels; var side = prefiltered.Description2d.Width; uint probeSide = (uint)probe.Description2d.Width; ConstantsBufferId constantBuffer = MyCommon.GetObjectCB(32); RC.CSSetCB(1, constantBuffer); RC.DeviceContext.ComputeShader.SetShaderResource(0, probe.ShaderView); RC.DeviceContext.ComputeShader.SetSamplers(0, MyRender11.StandardSamplers); for (int j = 0; j < 6; ++j) { int mipSide = side; for (int i = 0; i < mipLevels; ++i) { uint samplesNum = i == 0 ? 1u : 64u; uint mipSideUint = (uint)mipSide; uint ju = (uint)j; float mipLevelFactor = 1 - (i / (float)(mipLevels - 1)); var mapping = MyMapping.MapDiscard(constantBuffer); mapping.WriteAndPosition(ref samplesNum); mapping.WriteAndPosition(ref probeSide); mapping.WriteAndPosition(ref mipSideUint); mapping.WriteAndPosition(ref ju); mapping.WriteAndPosition(ref mipLevelFactor); mapping.Unmap(); ComputeShaderId.TmpUav[0] = prefiltered.SubresourceUav(j, i); RC.DeviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); RC.DeviceContext.Dispatch((mipSide + 7) / 8, (mipSide + 7) / 8, 1); mipSide >>= 1; } } ComputeShaderId.TmpUav[0] = null; RC.DeviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); RC.DeviceContext.ComputeShader.SetShaderResource(0, null); RC.DeviceContext.ComputeShader.Set(null); }
internal static MyBindableResource Run(MyBindableResource src, MyBindableResource avgLum) { var buffer = MyCommon.GetObjectCB(16); var mapping = MyMapping.MapDiscard(buffer); mapping.WriteAndPosition(ref MyRender11.Settings.MiddleGrey); mapping.WriteAndPosition(ref MyRender11.Settings.LuminanceExposure); mapping.WriteAndPosition(ref MyRender11.Settings.BloomExposure); mapping.WriteAndPosition(ref MyRender11.Settings.BloomMult); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.BindUAV(0, MyRender11.HalfScreenUavHDR); RC.BindSRVs(0, src, avgLum); RC.SetCS(m_bloomShader); var size = MyRender11.HalfScreenUavHDR.GetSize(); RC.DeviceContext.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_downscaleShader); size = MyRender11.QuarterScreenUavHDR.GetSize(); RC.BindUAV(0, MyRender11.QuarterScreenUavHDR); RC.BindSRV(0, MyRender11.HalfScreenUavHDR); RC.DeviceContext.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); size = MyRender11.EighthScreenUavHDR.GetSize(); RC.BindUAV(0, MyRender11.EighthScreenUavHDR); RC.BindSRV(0, MyRender11.QuarterScreenUavHDR); RC.DeviceContext.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_blurH); RC.BindUAV(0, MyRender11.EighthScreenUavHDRHelper); RC.BindSRV(0, MyRender11.EighthScreenUavHDR); RC.DeviceContext.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_blurV); RC.BindUAV(0, MyRender11.EighthScreenUavHDR); RC.BindSRV(0, MyRender11.EighthScreenUavHDRHelper); RC.DeviceContext.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); return(MyRender11.EighthScreenUavHDR); }
internal static void Blend(RwTexId dst, RwTexId src0, RwTexId src1, float blendWeight) { //MyImmediateRC.RC.Context.CopyResource(src1.Resource, dst.Resource); RC.DeviceContext.ComputeShader.Set(m_blend); var mipLevels = dst.Description2d.MipLevels; var side = dst.Description2d.Width; RC.CSSetCB(1, MyCommon.GetObjectCB(32)); RC.DeviceContext.ComputeShader.SetSamplers(0, MyRender11.StandardSamplers); for (int j = 0; j < 6; ++j) { var mipSide = side; for (int i = 0; i < mipLevels; ++i) { uint samplesNum = i == 0 ? 1u : 64u; var blendConstantData = new MyBlendData { field0 = 0, field1 = 0, MipSide = (uint)mipSide, field2 = 0, W = blendWeight }; var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(32)); mapping.WriteAndPosition(ref blendConstantData); mapping.Unmap(); RC.DeviceContext.ComputeShader.SetShaderResource(0, src0.SubresourceSrv(j, i)); RC.DeviceContext.ComputeShader.SetShaderResource(1, src1.SubresourceSrv(j, i)); // The single parameter version of SetUnorderedAccessView allocates ComputeShaderId.TmpUav[0] = dst.SubresourceUav(j, i); RC.DeviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); RC.DeviceContext.Dispatch((mipSide + 7) / 8, (mipSide + 7) / 8, 1); mipSide >>= 1; } } ComputeShaderId.TmpUav[0] = null; RC.DeviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); RC.DeviceContext.ComputeShader.SetShaderResource(0, null); RC.DeviceContext.ComputeShader.SetShaderResource(1, null); RC.DeviceContext.ComputeShader.Set(null); }
internal static MyBindableResource Run(MyBindableResource src, MyBindableResource avgLum) { var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.stream.Write(MyRender11.Settings.MiddleGrey); mapping.stream.Write(MyRender11.Settings.LuminanceExposure); mapping.stream.Write(MyRender11.Settings.BloomExposure); mapping.stream.Write(MyRender11.Settings.BloomMult); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.BindUAV(0, MyRender11.m_div2); RC.BindSRV(0, src, avgLum); RC.SetCS(m_bloomShader); var size = MyRender11.m_div2.GetSize(); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_downscaleShader); size = MyRender11.m_div4.GetSize(); RC.BindUAV(0, MyRender11.m_div4); RC.BindSRV(0, MyRender11.m_div2); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); size = MyRender11.m_div8.GetSize(); RC.BindUAV(0, MyRender11.m_div8); RC.BindSRV(0, MyRender11.m_div4); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_blurH); RC.BindUAV(0, MyRender11.m_div8_1); RC.BindSRV(0, MyRender11.m_div8); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_blurV); RC.BindUAV(0, MyRender11.m_div8); RC.BindSRV(0, MyRender11.m_div8_1); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); return(MyRender11.m_div8); }
internal static void CreateHistogram(ShaderResourceView texture, Vector2I resolution, int samples) { RC.Context.ClearUnorderedAccessView(m_histogram.Uav, Int4.Zero); RC.Context.ComputeShader.Set(m_buildHistogram); RC.Context.ComputeShader.SetShaderResource(0, texture); RC.Context.ComputeShader.SetUnorderedAccessView(0, m_histogram.Uav); var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.stream.Write((uint)resolution.X); mapping.stream.Write((uint)resolution.Y); mapping.Unmap(); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.Context.Dispatch((resolution.X + m_numthreads - 1) / m_numthreads, (resolution.Y + m_numthreads - 1) / m_numthreads, 1); RC.Context.ComputeShader.Set(null); }
internal static void Blend(RwTexId dst, RwTexId src0, RwTexId src1, float w) { //MyImmediateRC.RC.Context.CopyResource(src1.Resource, dst.Resource); RC.Context.ComputeShader.Set(m_blend); var mipLevels = dst.Description2d.MipLevels; var side = dst.Description2d.Width; RC.CSSetCB(1, MyCommon.GetObjectCB(32)); RC.Context.ComputeShader.SetSamplers(0, MyRender11.StandardSamplers); for (int j = 0; j < 6; ++j) { var mipSide = side; for (int i = 0; i < mipLevels; ++i) { uint samplesNum = i == 0 ? 1u : 64u; var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(32)); mapping.stream.Write((uint)0); mapping.stream.Write((uint)0); mapping.stream.Write((uint)mipSide); mapping.stream.Write((uint)0); mapping.stream.Write(w); mapping.Unmap(); RC.Context.ComputeShader.SetShaderResources(0, src0.SubresourceSrv(j, i), src1.SubresourceSrv(j, i)); RC.Context.ComputeShader.SetUnorderedAccessView(0, dst.SubresourceUav(j, i)); RC.Context.Dispatch((mipSide + 7) / 8, (mipSide + 7) / 8, 1); mipSide >>= 1; } } RC.Context.ComputeShader.SetUnorderedAccessView(0, null); RC.Context.ComputeShader.SetShaderResources(0, null, null); RC.Context.ComputeShader.Set(null); }
internal static void Prefilter(RwTexId probe, RwTexId prefiltered) { RC.Context.ComputeShader.Set(m_prefilter); var mipLevels = prefiltered.Description2d.MipLevels; var side = prefiltered.Description2d.Width; var probeSide = probe.Description2d.Width; RC.CSSetCB(1, MyCommon.GetObjectCB(32)); RC.Context.ComputeShader.SetShaderResource(0, probe.ShaderView); RC.Context.ComputeShader.SetSamplers(0, MyRender11.StandardSamplers); for (int j = 0; j < 6; ++j) { var mipSide = side; for (int i = 0; i < mipLevels; ++i) { uint samplesNum = i == 0 ? 1u : 64u; var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(32)); mapping.stream.Write((uint)samplesNum); mapping.stream.Write((uint)probeSide); mapping.stream.Write((uint)mipSide); mapping.stream.Write((uint)j); mapping.stream.Write(1 - (i / (float)(mipLevels - 1))); mapping.Unmap(); RC.Context.ComputeShader.SetUnorderedAccessView(0, prefiltered.SubresourceUav(j, i)); RC.Context.Dispatch((mipSide + 7) / 8, (mipSide + 7) / 8, 1); mipSide >>= 1; } } RC.Context.ComputeShader.SetUnorderedAccessView(0, null); RC.Context.ComputeShader.SetShaderResource(0, null); RC.Context.ComputeShader.Set(null); }
internal static void Run(MyBindableResource dst, MyBindableResource src, MyBindableResource avgLum, MyBindableResource bloom, bool enableTonemapping = true) { //Debug.Assert(src.GetSize() == dst.GetSize()); var buffer = MyCommon.GetObjectCB(16); var mapping = MyMapping.MapDiscard(buffer); mapping.WriteAndPosition(ref MyRender11.Settings.MiddleGrey); mapping.WriteAndPosition(ref MyRender11.Settings.LuminanceExposure); mapping.WriteAndPosition(ref MyRender11.Settings.BloomExposure); mapping.WriteAndPosition(ref MyRender11.Settings.BloomMult); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.BindUAV(0, dst); RC.BindSRVs(0, src, avgLum, bloom); RC.DeviceContext.ComputeShader.SetSampler(0, SamplerStates.m_default); if (enableTonemapping) { RC.SetCS(m_cs); } else { RC.SetCS(m_csSkip); } var size = dst.GetSize(); RC.DeviceContext.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); ComputeShaderId.TmpUav[0] = null; RC.DeviceContext.ComputeShader.SetUnorderedAccessViews(0, ComputeShaderId.TmpUav, ComputeShaderId.TmpCount); RC.SetCS(null); }
internal static void PreparePointLights() { var activePointlights = 0; MyLights.Update(); BoundingFrustumD viewFrustumClippedD = MyRender11.Environment.ViewFrustumClippedD; if (MyStereoRender.Enable) { if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT) { viewFrustumClippedD = MyStereoRender.EnvMatricesLeftEye.ViewFrustumClippedD; } else if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT) { viewFrustumClippedD = MyStereoRender.EnvMatricesRightEye.ViewFrustumClippedD; } } MyLights.PointlightsBvh.OverlapAllFrustum(ref viewFrustumClippedD, VisiblePointlights); bool visiblePointlights = VisiblePointlights.Count != 0; if (!visiblePointlights && !m_lastFrameVisiblePointlights) { return; } m_lastFrameVisiblePointlights = visiblePointlights; if (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.Sort((x, y) => x.ViewerDistanceSquared.CompareTo(y.ViewerDistanceSquared)); while (VisiblePointlights.Count > MyRender11Constants.MAX_POINT_LIGHTS) { VisiblePointlights.RemoveAtFast(VisiblePointlights.Count - 1); } } foreach (var light in VisiblePointlights) { MyLights.WritePointlightConstants(light, ref m_pointlightsCullBuffer[activePointlights]); activePointlights++; Debug.Assert(activePointlights <= MyRender11Constants.MAX_POINT_LIGHTS); } for (int lightIndex = activePointlights; lightIndex < MyRender11Constants.MAX_POINT_LIGHTS; ++lightIndex) { MyLights.WritePointlightConstants(LightId.NULL, ref m_pointlightsCullBuffer[lightIndex]); } var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.WriteAndPosition(ref activePointlights); mapping.Unmap(); mapping = MyMapping.MapDiscard(m_pointlightCullHwBuffer.Buffer); mapping.WriteAndPosition(m_pointlightsCullBuffer, 0, MyRender11Constants.MAX_POINT_LIGHTS); mapping.Unmap(); if (!MyStereoRender.Enable) { RC.CSSetCB(0, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); } RC.CSSetCB(1, MyCommon.GetObjectCB(16)); //RC.BindUAV(0, MyScreenDependants.m_test); RC.BindUAV(0, MyScreenDependants.m_tileIndices); RC.BindGBufferForRead(0, MyGBuffer.Main); RC.CSBindRawSRV(MyCommon.POINTLIGHT_SLOT, m_pointlightCullHwBuffer); RC.SetCS(m_preparePointLights); Vector2I tiles = new Vector2I(MyScreenDependants.TilesX, MyScreenDependants.TilesY); if (MyStereoRender.Enable && MyStereoRender.RenderRegion != MyStereoRegion.FULLSCREEN) { tiles.X /= 2; } RC.DeviceContext.Dispatch(tiles.X, tiles.Y, 1); RC.SetCS(null); }
internal static MyBindableResource Run(MyBindableResource uav0, MyBindableResource uav1, MyBindableResource src, MyBindableResource prevLum, MyBindableResource localAvgLum) { var size = src.GetSize(); var texelsNum = size.X * size.Y; var mapping = MyMapping.MapDiscard(MyCommon.GetObjectCB(16)); mapping.stream.Write((uint)size.X); mapping.stream.Write((uint)size.Y); mapping.stream.Write(texelsNum); mapping.stream.Write(MyRender11.Postprocess.EnableEyeAdaptation ? -1.0f : MyRender11.Postprocess.ConstantLuminance); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.BindUAV(0, uav0); RC.BindSRV(0, src); RC.SetCS(m_initialShader); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(m_sumShader); int i = 0; while (true) { size.X = (size.X + m_numthreads - 1) / m_numthreads; size.Y = (size.Y + m_numthreads - 1) / m_numthreads; if (size.X <= 8 && size.Y <= 8) { break; } //mapping = MyMapping.MapDiscard(MyCommon.GetObjectBuffer(16).Buffer); //mapping.stream.Write(new Vector2I(size.X, size.Y)); //mapping.Unmap(); RC.BindUAV(0, (i % 2 == 0) ? uav1 : uav0); RC.BindSRV(0, (i % 2 == 0) ? uav0 : uav1); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); //might not be exactly correct if we skip this var dirty = (i % 2 == 0) ? uav0 : uav1; RC.Context.ClearUnorderedAccessView((dirty as IUnorderedAccessBindable).UAV, new SharpDX.Int4(0, 0, 0, 0)); i++; } RC.SetCS(m_finalShader); //mapping = MyMapping.MapDiscard(MyCommon.GetObjectBuffer(16).Buffer); //mapping.stream.Write(new Vector2I(size.X, size.Y)); //mapping.stream.Write(texelsNum); //mapping.Unmap(); RC.BindUAV(0, (i % 2 == 0) ? uav1 : uav0); RC.BindSRV(0, (i % 2 == 0) ? uav0 : uav1, prevLum); RC.Context.Dispatch((size.X + m_numthreads - 1) / m_numthreads, (size.Y + m_numthreads - 1) / m_numthreads, 1); RC.SetCS(null); var output = (i % 2 == 0) ? uav1 : uav0; RC.Context.CopySubresourceRegion(output.m_resource, 0, new ResourceRegion(0, 0, 0, 1, 1, 1), prevLum.m_resource, 0); return(output); }
internal static void Render() { MyLights.Update(); MyGpuProfiler.IC_BeginBlock("Map lights to tiles"); if (MyRender11.DebugOverrides.PointLights) { PreparePointLights(); } MyGpuProfiler.IC_EndBlock(); RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; if (!MyStereoRender.Enable) { RC.CSSetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); } else { MyStereoRender.CSBindRawCB_FrameConstants(RC); } RC.BindGBufferForRead(0, MyGBuffer.Main); RC.BindRawSRV(MyCommon.MATERIAL_BUFFER_SLOT, MySceneMaterials.m_buffer); RC.SetBS(MyRender11.BlendAdditive); if (!MyStereoRender.Enable) { RC.SetDS(MyDepthStencilState.IgnoreDepthStencil); } else { RC.SetDS(MyDepthStencilState.StereoIgnoreDepthStencil); } RC.DeviceContext.PixelShader.SetSamplers(0, SamplerStates.StandardSamplers); MyGpuProfiler.IC_BeginBlock("Apply point lights"); if (MyRender11.DebugOverrides.PointLights) { RenderPointlightsTiled(); } MyGpuProfiler.IC_EndBlock(); MyGpuProfiler.IC_BeginBlock("Apply spotlights"); if (MyRender11.DebugOverrides.SpotLights) { RenderSpotlights(); } MyGpuProfiler.IC_EndBlock(); MyGpuProfiler.IC_BeginBlock("Apply directional light"); if (MyRender11.DebugOverrides.EnvLight) { RenderDirectionalEnvironmentLight(); } MyGpuProfiler.IC_EndBlock(); // Because of BindGBufferForRead: RC.BindRawSRV(0, null); RC.BindRawSRV(1, null); RC.BindRawSRV(2, null); RC.BindRawSRV(3, null); RC.BindRawSRV(4, null); RC.CSBindRawSRV(0, null); RC.CSBindRawSRV(1, null); RC.CSBindRawSRV(2, null); RC.CSBindRawSRV(3, null); RC.CSBindRawSRV(4, null); RC.SetBS(null); }
internal static void Run(MyBindableResource depthRead) { // The maximum number of supported GPU particles SharpDX.Direct3D11.ShaderResourceView textureArraySRV; int emitterCount = MyGPUEmitters.Gather(m_emitterData, out textureArraySRV); if (emitterCount == 0) { return; } // Unbind current targets while we run the compute stages of the system //RC.DeviceContext.OutputMerger.SetTargets(null); // global GPU particles setup RC.SetDS(MyDepthStencilState.DefaultDepthState); RC.SetRS(MyRender11.m_nocullRasterizerState); RC.SetIL(null); RC.CSSetCB(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.DeviceContext.ComputeShader.SetSamplers(0, SamplerStates.StandardSamplers); RC.DeviceContext.PixelShader.SetSamplers(0, SamplerStates.StandardSamplers); RC.SetCB(4, MyRender11.DynamicShadows.ShadowCascades.CascadeConstantBuffer); RC.DeviceContext.VertexShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, SamplerStates.m_shadowmap); RC.DeviceContext.VertexShader.SetShaderResource(MyCommon.CASCADES_SM_SLOT, MyRender11.DynamicShadows.ShadowCascades.CascadeShadowmapArray.SRV); RC.DeviceContext.VertexShader.SetSamplers(0, SamplerStates.StandardSamplers); // If we are resetting the particle system, then initialize the dead list if (m_resetSystem) { ResetInternal(); m_resetSystem = false; } MyGpuProfiler.IC_BeginBlock("Emit"); // Emit particles into the system Emit(emitterCount, m_emitterData); MyGpuProfiler.IC_EndBlock(); // Run the simulation for this frame MyGpuProfiler.IC_BeginBlock("Simulate"); Simulate(depthRead); MyGpuProfiler.IC_EndBlock(); // Copy the atomic counter in the alive list UAV into a constant buffer for access by subsequent passes RC.DeviceContext.CopyStructureCount(m_activeListConstantBuffer, 0, m_aliveIndexBuffer.m_UAV); // Only read number of alive and dead particle back to the CPU in debug as we don't want to stall the GPU in release code #if DEBUG m_numDeadParticlesAfterSimulation = ReadCounter(m_deadListBuffer); m_numActiveParticlesAfterSimulation = ReadCounter(m_aliveIndexBuffer); #endif MyGpuProfiler.IC_BeginBlock("Render"); Render(textureArraySRV, depthRead); MyGpuProfiler.IC_EndBlock(); RC.DeviceContext.ComputeShader.SetSamplers(0, SamplerStates.StandardSamplers); /*Debug.WriteLine(string.Format("dead particles @ start {0} dead after emit {1} dead after sim {2} active after sim {3}", * m_numDeadParticlesOnInit, m_numDeadParticlesAfterEmit, * m_numDeadParticlesAfterSimulation, m_numActiveParticlesAfterSimulation));*/ }
internal static MyBindableResource Run(MyBindableResource uav0, MyBindableResource uav1, MyBindableResource src, MyBindableResource prevLum) { Vector3I size = src.GetSize(); int texelsNum = size.X * size.Y; uint sizeX = (uint)size.X; uint sizeY = (uint)size.Y; float adaptationFactor = MyRender11.Postprocess.EnableEyeAdaptation ? -1.0f : MyRender11.Postprocess.ConstantLuminance; var buffer = MyCommon.GetObjectCB(16); var mapping = MyMapping.MapDiscard(buffer); mapping.WriteAndPosition(ref sizeX); mapping.WriteAndPosition(ref sizeY); mapping.WriteAndPosition(ref texelsNum); mapping.WriteAndPosition(ref adaptationFactor); mapping.Unmap(); RC.CSSetCB(0, MyCommon.FrameConstants); RC.CSSetCB(1, MyCommon.GetObjectCB(16)); RC.SetCS(m_initialShader); MyBindableResource output = uav0; MyBindableResource input = src; RC.BindUAV(0, output); RC.BindSRV(0, input); int threadGroupCountX = ComputeGroupCount(size.X); int threadGroupCountY = ComputeGroupCount(size.Y); RC.DeviceContext.Dispatch(threadGroupCountX, threadGroupCountY, 1); RC.SetCS(m_sumShader); int i = 0; while (true) { size.X = threadGroupCountX; size.Y = threadGroupCountY; if (size.X <= NUM_THREADS && size.Y <= NUM_THREADS) { break; } output = (i % 2 == 0) ? uav1 : uav0; input = (i % 2 == 0) ? uav0 : uav1; RC.BindUAV(0, output); RC.BindSRV(0, input); threadGroupCountX = ComputeGroupCount(size.X); threadGroupCountY = ComputeGroupCount(size.Y); RC.DeviceContext.Dispatch(threadGroupCountX, threadGroupCountY, 1); i++; } RC.SetCS(m_finalShader); output = (i % 2 == 0) ? uav1 : uav0; input = (i % 2 == 0) ? uav0 : uav1; RC.BindUAV(0, output); RC.BindSRVs(0, input, prevLum); threadGroupCountX = ComputeGroupCount(size.X); threadGroupCountY = ComputeGroupCount(size.Y); RC.DeviceContext.Dispatch(threadGroupCountX, threadGroupCountY, 1); RC.SetCS(null); // Backup the result for later process RC.DeviceContext.CopySubresourceRegion(output.m_resource, 0, new ResourceRegion(0, 0, 0, 1, 1, 1), prevLum.m_resource, 0); return(output); }