public void MarkOneCascade(int numCascade, IDepthStencil depthStencil, Matrix worldToProjection, ICascadeShadowMapSlice slice) { MyRenderContext RC = MyRender11.RC; RC.SetVertexBuffer(0, m_vertexBuffer); RC.SetIndexBuffer(m_indexBuffer); RC.SetInputLayout(m_inputLayout); RC.SetViewport(0, 0, MyRender11.ResolutionI.X, MyRender11.ResolutionI.Y); RC.SetDepthStencilState(MyDepthStencilStateManager.MarkIfInsideCascadeOld[numCascade], 0xf - numCascade); RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState); RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); MyMapping mapping = MyMapping.MapDiscard(m_markerConstantBuffer); Matrix shadowToProjection = slice.MatrixShadowToWorldAt0Space * worldToProjection; shadowToProjection = Matrix.Transpose(shadowToProjection); mapping.WriteAndPosition(ref shadowToProjection); mapping.Unmap(); RC.VertexShader.SetConstantBuffer(6, m_markerConstantBuffer); RC.VertexShader.Set(m_vsMarker); RC.PixelShader.SetSrv(0, depthStencil.SrvDepth); //RC.PixelShader.SetSrv(1, depthStencil.SrvStencil); RC.PixelShader.Set(m_psMarker); RC.SetRtv(depthStencil, MyDepthStencilAccess.DepthReadOnly); RC.DrawIndexed(36, 0, 0); }
// complex implemtation because of the IPrioritizedWork, the simple way is to call Draw directly... public void InitWork(List <MyInstanceComponent> visibleInstances) { MyRenderProxy.Assert(m_visibleInstancesForDoWork == null, "It is needed to call DoWork() after InitWork()"); m_visibleInstancesForDoWork = visibleInstances; m_RC = MyManagers.DeferredRCs.AcquireRC(); Stats.Clear(); }
// the method returns the same constant buffer as the parameter protected void FillConstantBuffer <T>(MyRenderContext RC, IConstantBuffer cb, T data) where T : struct { var mapping = MyMapping.MapDiscard(RC, MyCommon.ProjectionConstants); mapping.WriteAndPosition(ref data); mapping.Unmap(); }
public void MarkAllCascades(IDepthStencil depthStencil, Matrix worldToProjection, ICascadeShadowMap csm) { MyRenderContext RC = MyRender11.RC; RC.ClearDsv(depthStencil, DepthStencilClearFlags.Stencil, 0, 0); for (int i = 0; i < csm.SlicesCount; i++) { MarkOneCascade(i, depthStencil, worldToProjection, csm.GetSlice(i)); } }
public void InitDevice(Form form) { this.form = form; this.form.FormBorderStyle = FormBorderStyle.FixedSingle; this.form.MaximizeBox = false; this.desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, this.desc, out this.device, out this.swapChain); using (var factory = this.swapChain.GetParent <Factory>()) { this.screen = factory.Adapters[0].Outputs[0]; factory.MakeWindowAssociation(this.form.Handle, WindowAssociationFlags.IgnoreAll); } var context = device.ImmediateContext; backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); depthBuffer = new Texture2D(device, new Texture2DDescription() { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = form.ClientSize.Width, Height = form.ClientSize.Height, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }); depthView = new DepthStencilView(device, depthBuffer); context.Rasterizer.SetViewport(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f)); context.OutputMerger.SetTargets(depthView, renderView); renderContext = new MyRenderContext(this); }
public void DrawCoverage(IRtvTexture outTex, IDepthStencil depthStencil) { MyRenderContext RC = MyRender11.RC; RC.SetBlendState(null); RC.SetRtv(outTex); RC.PixelShader.Set(m_psDrawCoverage); RC.PixelShader.SetSrv(1, depthStencil.SrvStencil); MyScreenPass.DrawFullscreenQuad(); RC.ResetTargets(); }
public void CopyRedToAll(IRtvBindable output, ISrvTexture source) { MyRenderContext RC = MyRender11.RC; RC.SetBlendState(null); RC.SetRtv(output); RC.PixelShader.Set(m_ps); RC.PixelShader.SetSrv(0, source); MyScreenPass.DrawFullscreenQuad(); RC.ResetTargets(); }
// this is tricky call. The method assumes that Draw() has been called in this frame public void DrawGlass(MyRenderContext RC) { RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetViewport(m_viewport.OffsetX, m_viewport.OffsetY, m_viewport.Width, m_viewport.Height); //RC.SetRtvs(m_gbuffer, MyDepthStencilAccess.ReadWrite); <- the rtv is set out of the pass... FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix)); RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex); IConstantBuffer cbObjectData = GetPlaceholderObjectCB(RC, 255); // <- the lod value does not matter in this case RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData); //RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData); foreach (var itGroup in m_drawableGroupFactory.GetRawDrawableGroups()) { MyRenderProxy.Assert(itGroup.InstancesCount != 0); MyRenderProxy.Assert(itGroup.InstancesCount == itGroup.InstancesIncrement); if (itGroup.Lod.GlassParts == null) { continue; } foreach (var part in itGroup.Lod.GlassParts) { MyGlassMaterial material = part.GlassMaterial; RC.SetVertexBuffer(0, part.Parent.VB0); RC.SetVertexBuffer(1, part.Parent.VB1); RC.SetVertexBuffer(2, m_vbInstances); RC.SetIndexBuffer(part.Parent.IB); MyShaderBundle shaderBundle = part.GetShaderBundle(itGroup.State); RC.SetInputLayout(shaderBundle.InputLayout); RC.VertexShader.Set(shaderBundle.VertexShader); RC.PixelShader.Set(shaderBundle.PixelShader); RC.PixelShader.SetSrvs(0, material.Srvs); RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, GetGlassCB(RC, material)); int numInstances = itGroup.InstancesCount; int ibOffset = itGroup.OffsetInInstanceBuffer + (part.InstanceMaterialOffsetInLod + 1) * itGroup.InstancesCount; RC.DrawIndexedInstanced(part.IndicesCount, numInstances, part.StartIndex, part.StartVertex, ibOffset); } } }
unsafe IConstantBuffer GetGlassCB(MyRenderContext RC, MyGlassMaterial material) { StaticGlassConstants glassConstants = new StaticGlassConstants(); glassConstants.Color = material.Color; glassConstants.Reflective = material.Refraction; var glassCB = MyCommon.GetMaterialCB(sizeof(StaticGlassConstants)); var mapping = MyMapping.MapDiscard(RC, glassCB); mapping.WriteAndPosition(ref glassConstants); mapping.Unmap(); return(glassCB); }
// returned value - is there anything to draw? public bool Finalize(MyRenderContext RC, out IVertexBuffer vbInstances) { vbInstances = m_vbInstances; if (m_validElements == 0) { return(false); } MyMapping mappingVb = MyMapping.MapDiscard(RC, vbInstances); mappingVb.WriteAndPosition(m_vbData, m_validElements); mappingVb.Unmap(); return(true); }
public void OnDeviceEnd() { m_isDeviceInit = false; // Initialize all RCs m_tmpList.Clear(); for (int i = 0; i < m_pool.Count; i++) { MyRenderContext rc = m_pool.Get(); m_tmpList.Add(rc); rc.Dispose(); } foreach (var RC in m_tmpList) { m_pool.Return(RC); } m_tmpList.Clear(); }
public static void Draw(IRtvBindable renderTarget) { ISrvBindable srvBind = null; if (m_selRtvTexture != null) { srvBind = m_selRtvTexture; } if (m_selUavTexture != null) { srvBind = m_selUavTexture; } if (m_selBorrowedRtvTexture != null) { srvBind = m_selBorrowedRtvTexture; } if (m_selBorrowedUavTexture != null) { srvBind = m_selBorrowedUavTexture; } if (srvBind == null) // no texture is selected { return; } if (m_ps == PixelShaderId.NULL) { m_ps = MyShaders.CreatePs("Debug/DebugRt.hlsl"); } MyRenderContext RC = MyImmediateRC.RC; RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetViewport(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y); RC.SetRtv(renderTarget); RC.SetBlendState(null); RC.PixelShader.Set(m_ps); RC.PixelShader.SetSrv(0, srvBind); RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); MyScreenPass.DrawFullscreenQuad(); Deselect(); }
public void Draw(IRtvTexture outTex, IDepthStencil stencil, ICascadeShadowMap csm, ref MyShadowsSettings settings) { MyRenderContext RC = MyRender11.RC; RC.SetBlendState(null); RC.SetRtv(outTex); RC.PixelShader.Set(m_ps); RC.PixelShader.SetSrv(0, stencil.SrvDepth); RC.PixelShader.SetSrv(1, stencil.SrvStencil); RC.PixelShader.SetSrv(2, csm.DepthArrayTexture); RC.PixelShader.SetSrv(3, MyGBuffer.Main.GBuffer1); RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.PixelShader.SetConstantBuffer(1, GetShadowConstants(csm, ref settings)); RC.PixelShader.SetSampler(6, MySamplerStateManager.Shadowmap); MyScreenPass.DrawFullscreenQuad(); RC.ResetTargets(); }
protected static unsafe IConstantBuffer GetPlaceholderObjectCB(MyRenderContext RC, uint lod) { int cbSize = sizeof(MyObjectDataCommon); cbSize += sizeof(MyObjectDataNonVoxel); IConstantBuffer cb = MyCommon.GetObjectCB(cbSize); var mapping = MyMapping.MapDiscard(RC, cb); MyObjectDataNonVoxel nonVoxelData = new MyObjectDataNonVoxel(); mapping.WriteAndPosition(ref nonVoxelData); MyObjectDataCommon commonData = new MyObjectDataCommon(); commonData.LocalMatrix = Matrix.Identity; commonData.ColorMul = Vector3.One; commonData.KeyColor = new Vector3(0, 0f, 0f); commonData.LOD = lod; mapping.WriteAndPosition(ref commonData); mapping.Unmap(); return(cb); }
public void OnDeviceInit() { m_pool = new MyConcurrentPool <MyRenderContext>(MaxDeferredRCsCount, true); // Initialize all RCs m_tmpList.Clear(); int poolSize = m_pool.Count; for (int i = 0; i < poolSize; i++) { MyRenderContext rc = m_pool.Get(); m_tmpList.Add(rc); rc.Initialize(); } foreach (var rc in m_tmpList) { m_pool.Return(rc); } m_tmpList.Clear(); m_isDeviceInit = true; }
static void UpdateStateChanges(string page) { UpdateRenderContextStats(page, "Immediate RC calls", MyRender11.RC.GetStatistics()); MyRender11.RC.ClearStatistics(); m_tmpRCStatistics.Clear(); MyRenderProxy.Assert(m_tmpListRCs.Count == 0, "Temporary data are persistently stored in list"); MyDeferredRenderContextManager rcManager = MyManagers.DeferredRCs; for (int i = 0; i < rcManager.GetRCsCount(); i++) { MyRenderContext rc = rcManager.AcquireRC(); m_tmpListRCs.Add(rc); m_tmpRCStatistics.Gather(rc.GetStatistics()); rc.ClearStatistics(); } foreach (MyRenderContext rc in m_tmpListRCs) { rcManager.FreeRC(rc); } m_tmpListRCs.Clear(); UpdateRenderContextStats(page, "Deferred RCs calls", m_tmpRCStatistics); m_tmpRCStatistics.Clear(); }
public void FreeRC(MyRenderContext rc) { MyRenderProxy.Assert(m_isDeviceInit); m_pool.Return(rc); }
protected override void Draw(MyRenderContext RC, List <MyInstanceComponent> visibleInstances) { int maxLodId = MyManagers.IDGenerator.DepthLods.GetHighestID(); if (IsProfilingDoable()) { ProfilerShort.Begin("Preparation"); } m_drawableGroupDepthStrategy.Prepare(m_vbInstances); m_drawableGroupFactory.Compute(m_drawableGroupDepthStrategy, visibleInstances, PassId, maxLodId); bool isEmpty = !m_drawableGroupDepthStrategy.Finalize(RC, out m_vbInstances); if (!isEmpty) { FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix)); RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetViewport(m_viewport.OffsetX, m_viewport.OffsetY, m_viewport.Width, m_viewport.Height); RC.SetRtv(m_dsv, null); FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix)); RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex); // Just some placeholder: IConstantBuffer cbObjectData = GetPlaceholderObjectCB(RC, 0); RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData); RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData); if (IsProfilingDoable()) { ProfilerShort.BeginNextBlock("Recording commands"); } foreach (var itGroup in m_drawableGroupFactory.GetRawDrawableGroups()) { MyRenderProxy.Assert(itGroup.InstancesCount != 0); MyRenderProxy.Assert(itGroup.InstancesCount == itGroup.InstancesIncrement); foreach (var part in itGroup.Lod.Parts) { RC.SetVertexBuffer(0, part.Parent.VB0); RC.SetVertexBuffer(2, m_vbInstances); RC.SetIndexBuffer(part.Parent.IB); RC.SetDepthStencilState(null); RC.SetRasterizerState(GetRasterizerState(m_isCascade)); RC.SetBlendState(null); MyShaderBundle shaderBundle = part.GetShaderBundle(itGroup.State); RC.SetInputLayout(shaderBundle.InputLayout); RC.VertexShader.Set(shaderBundle.VertexShader); RC.PixelShader.Set(shaderBundle.PixelShader); int numInstances = itGroup.InstancesCount; int ibOffset = itGroup.OffsetInInstanceBuffer + (part.InstanceMaterialOffsetInLod + 1) * itGroup.InstancesCount; RC.DrawIndexedInstanced(part.IndicesCount, numInstances, part.StartIndex, part.StartVertex, ibOffset); m_stats.Triangles += (part.IndicesCount / 3) * numInstances; m_stats.Instances += numInstances; m_stats.Draws++; } } } if (IsProfilingDoable()) { ProfilerShort.End(); } }
internal static void ResizeAndUpdateStaticStructuredBuffer(ref StructuredBufferId id, int capacity, int stride, IntPtr data, string debugName, MyRenderContext rc = null) { if (id == StructuredBufferId.NULL) { id = CreateStructuredBuffer(capacity, stride, false, data, debugName); } else { Debug.Assert(stride == id.Stride); Debug.Assert(false == id.Dynamic); if (id.Capacity < capacity) { SBuffersData[id.Index].Buffer.Dispose(); SBuffers.Data[id.Index].Description.SizeInBytes = stride * capacity; InitStructuredBuffer(id, data); } else { if (rc == null) { rc = MyRender11.RC; } rc.UpdateSubresource(new DataBox(data, stride * capacity, 0), id.Buffer); } } }
protected override void Draw(MyRenderContext RC, List <MyInstanceComponent> visibleInstances) { int maxLodId = MyManagers.IDGenerator.GBufferLods.GetHighestID(); ProfilerShort.Begin("Preparation"); m_drawableGroupGBufferStrategy.Prepare(m_vbInstances); m_drawableGroupFactory.Compute(m_drawableGroupGBufferStrategy, visibleInstances, 0, maxLodId); bool isEmpty = !m_drawableGroupGBufferStrategy.Finalize(RC, out m_vbInstances); if (!isEmpty) { RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetViewport(m_viewport.OffsetX, m_viewport.OffsetY, m_viewport.Width, m_viewport.Height); RC.SetRtvs(m_gbuffer, MyDepthStencilAccess.ReadWrite); FillConstantBuffer(RC, MyCommon.ProjectionConstants, Matrix.Transpose(m_viewProjMatrix)); RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); RC.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex); IConstantBuffer cbObjectData = GetPlaceholderObjectCB(RC, 255); // <- the lod value does not matter in this case RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData); RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, cbObjectData); ProfilerShort.BeginNextBlock("Recording commands"); foreach (var itGroup in m_drawableGroupFactory.GetRawDrawableGroups()) { MyRenderProxy.Assert(itGroup.InstancesCount != 0); MyRenderProxy.Assert(itGroup.InstancesCount == itGroup.InstancesIncrement); foreach (var part in itGroup.Lod.Parts) { MyStandardMaterial material = part.StandardMaterial; RC.SetVertexBuffer(0, part.Parent.VB0); RC.SetVertexBuffer(1, part.Parent.VB1); RC.SetVertexBuffer(2, m_vbInstances); RC.SetIndexBuffer(part.Parent.IB); if (MyRender11.Settings.Wireframe) { RC.SetDepthStencilState(MyDepthStencilStateManager.DepthTestWrite); RC.SetBlendState(null); RC.SetRasterizerState(MyRasterizerStateManager.NocullWireframeRasterizerState); } else { RC.SetDepthStencilState(part.StandardMaterial.DepthStencilState); RC.SetRasterizerState(material.RasterizerState); RC.SetBlendState(part.StandardMaterial.BlendState); } MyShaderBundle shaderBundle = part.GetShaderBundle(itGroup.State); RC.SetInputLayout(shaderBundle.InputLayout); RC.VertexShader.Set(shaderBundle.VertexShader); RC.PixelShader.Set(shaderBundle.PixelShader); RC.PixelShader.SetSrvs(0, SrvStrategy.GetSrvs(part)); int numInstances = itGroup.InstancesCount; int ibOffset = itGroup.OffsetInInstanceBuffer + (part.InstanceMaterialOffsetInLod + 1) * itGroup.InstancesCount; RC.DrawIndexedInstanced(part.IndicesCount, numInstances, part.StartIndex, part.StartVertex, ibOffset); m_stats.Triangles += (part.IndicesCount / 3) * numInstances; m_stats.Instances += numInstances; m_stats.Draws++; } } } ProfilerShort.End(); }
protected abstract void Draw(MyRenderContext RC, List <MyInstanceComponent> visibleInstances);