// 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.ComputeShader.SetRawUav(0, m_particleBuffer.m_uav); RC.ComputeShader.SetRawUav(1, m_deadListBuffer.m_uav); RC.ComputeShader.SetRawUav(2, m_skippedParticleCountBuffer.m_uav, 0); RC.ComputeShader.SetConstantBuffer(1, m_emitterConstantBuffer); RC.ComputeShader.SetRawSrv(1, m_emitterStructuredBuffer.Srv); RC.ComputeShader.Set(m_csEmit); RC.Dispatch(numThreadGroupsX, numThreadGroupsY, 1); RC.ComputeShader.SetConstantBuffer(1, null); RC.ComputeShader.Set(m_csEmitSkipFix); // Disaptch a set of 1d thread groups to fill out the dead list, one thread per particle RC.Dispatch(1, 1, 1); } }
public static void InitializeConstantBuffer(int?randomSeed = null) { MyRandom random; if (randomSeed.HasValue) { random = new MyRandom(randomSeed.Value); } else { random = new MyRandom(); } const int JITTERSIZE = 4 * 4; var jitters = new Vector4[JITTERSIZE]; for (int i = 0; i < JITTERSIZE; i++) { float angle = 2.0f * (float)Math.PI * random.NextFloat() / NUM_DIRECTIONS; jitters[i].X = (float)Math.Cos(angle); jitters[i].Y = (float)Math.Sin(angle); jitters[i].Z = random.NextFloat(); jitters[i].W = random.NextFloat(); } PerPassConstantBuffer data; for (uint sliceIndex = 0; sliceIndex < NUM_SLICES; ++sliceIndex) { data.Offset.X = (float)(sliceIndex % 4) + 0.5f; data.Offset.Y = (float)(sliceIndex / 4) + 0.5f; data.Jitter = jitters[sliceIndex]; data.SliceIndexFloat = (float)sliceIndex; data.SliceIndexInt = sliceIndex; var buffer = m_perPassCBs[sliceIndex]; if (buffer == ConstantsBufferId.NULL) { buffer = MyHwBuffers.CreateConstantsBuffer(PERPASSCONSTANTBUFFERSIZE, "MyHBAO::passCB " + sliceIndex); m_perPassCBs[sliceIndex] = buffer; } var mapping = MyMapping.MapDiscard(buffer); mapping.WriteAndPosition(ref data); mapping.Unmap(); } }
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); }
static unsafe void TransferData() { var mapping = MyMapping.MapDiscard(RC.Context, MyCommon.GetObjectCB(sizeof(Matrix) * MaxCustomProjections)); for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++) { var view = MyRenderProxy.BillboardsViewProjectionRead[i].View; var projection = MyRenderProxy.BillboardsViewProjectionRead[i].Projection; var scaleX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X; var scaleY = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y; var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X; var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height) / (float)MyRender11.ViewportResolution.Y; var viewportTransformation = new Matrix( scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, 1, 0, offsetX, offsetY, 0, 1 ); mapping.stream.Write(Matrix.Transpose(view * projection * viewportTransformation)); } for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MaxCustomProjections; i++) { mapping.stream.Write(Matrix.Identity); } mapping.Unmap(); mapping = MyMapping.MapDiscard(RC.Context, m_VB.Buffer); fixed(void *ptr = m_vertexData) { mapping.stream.Write(new IntPtr(ptr), 0, (sizeof(MyVertexFormatPosition) * MaxBillboards * 4)); } mapping.Unmap(); mapping = MyMapping.MapDiscard(RC.Context, m_SB.Buffer); fixed(void *ptr = m_billboardData) { mapping.stream.Write(new IntPtr(ptr), 0, (sizeof(MyBillboardData) * MaxBillboards)); } mapping.Unmap(); }
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 Combine(RwTexId targetArray, MyShadowCascades firstCascades, MyShadowCascades secondCascades) { if (!MyRender11.Settings.EnableShadows) { return; } ProfilerShort.Begin("MyShadowCascadesPostProcess.Combine"); MyGpuProfiler.IC_BeginBlock("MyShadowCascadesPostProcess.Combine"); firstCascades.FillConstantBuffer(firstCascades.CascadeConstantBuffer); secondCascades.FillConstantBuffer(secondCascades.CascadeConstantBuffer); secondCascades.PostProcessor.MarkCascadesInStencil(secondCascades.CascadeInfo); MyRenderContext renderContext = MyRenderContext.Immediate; DeviceContext deviceContext = renderContext.DeviceContext; deviceContext.PixelShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); deviceContext.PixelShader.SetConstantBuffer(10, firstCascades.CascadeConstantBuffer); deviceContext.PixelShader.SetConstantBuffer(11, secondCascades.CascadeConstantBuffer); deviceContext.PixelShader.SetConstantBuffer(12, m_inverseConstants); for (int subresourceIndex = 0; subresourceIndex < targetArray.Description2d.ArraySize; ++subresourceIndex) { renderContext.BindGBufferForRead(0, MyGBuffer.Main); deviceContext.OutputMerger.SetTargets((DepthStencilView)null, (RenderTargetView)targetArray.SubresourceRtv(subresourceIndex)); deviceContext.PixelShader.SetShaderResource(0, firstCascades.CascadeShadowmapArray.SubresourceSrv(subresourceIndex)); deviceContext.PixelShader.SetShaderResource(1, secondCascades.CascadeShadowmapArray.ShaderView); //deviceContext.PixelShader.SetShaderResource(4, MyGBuffer.Main.DepthStencil.m_SRV_stencil); renderContext.SetPS(m_combinePS); Matrix inverseCascadeMatrix = MatrixD.Transpose(MatrixD.Invert(firstCascades.CascadeInfo[subresourceIndex].CurrentLocalToProjection * MyMatrixHelpers.ClipspaceToTexture)); var mapping = MyMapping.MapDiscard(m_inverseConstants); mapping.WriteAndPosition(ref inverseCascadeMatrix); mapping.Unmap(); MyScreenPass.DrawFullscreenQuad(new MyViewport(0, 0, targetArray.Description2d.Width, targetArray.Description2d.Height)); } deviceContext.OutputMerger.SetTargets(null as DepthStencilView, null as RenderTargetView); deviceContext.PixelShader.SetShaderResource(0, null); deviceContext.PixelShader.SetShaderResource(1, null); deviceContext.PixelShader.SetShaderResource(2, null); MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); }
static void RenderDirectionalEnvironmentLight() { MySunlightConstantsLayout constants; constants.Direction = MyEnvironment.DirectionalLightDir; constants.Color = MyEnvironment.DirectionalLightIntensity; var mapping = MyMapping.MapDiscard(m_sunlightConstants); mapping.stream.Write(constants); mapping.Unmap(); //context.VertexShader.Set(MyCommon.FullscreenShader.VertexShader); RC.SetPS(DirectionalEnvironmentLight_Pixel); RC.SetCB(1, m_sunlightConstants); RC.SetCB(4, MyShadows.m_csmConstants); RC.Context.PixelShader.SetSamplers(0, MyRender11.StandardSamplers); RC.Context.PixelShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MyRender11.m_shadowmapSamplerState); RC.Context.PixelShader.SetShaderResource(MyCommon.CASCADES_SM_SLOT, MyShadows.m_cascadeShadowmapArray.ShaderView); var z = Vector4.Transform(new Vector4(0.5f, 0.5f, -MyEnvironment.FarClipping, 1), MyEnvironment.Projection); RC.Context.PixelShader.SetShaderResource(MyCommon.SKYBOX_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.DaySkybox, MyTextureEnum.CUBEMAP, true))); //RC.Context.PixelShader.SetShaderResource(MyCommon.SKYBOX_IBL_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.DaySkyboxPrefiltered, MyTextureEnum.CUBEMAP, true))); RC.Context.PixelShader.SetShaderResource(MyCommon.SKYBOX_IBL_SLOT, MyRender11.IsIntelBrokenCubemapsWorkaround ? MyTextures.GetView(MyTextures.IntelFallbackCubeTexId) : MyGeometryRenderer.m_envProbe.cubemapPrefiltered.ShaderView); RC.Context.PixelShader.SetShaderResource(MyCommon.SKYBOX2_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.NightSkybox, MyTextureEnum.CUBEMAP, true))); RC.Context.PixelShader.SetShaderResource(MyCommon.SKYBOX2_IBL_SLOT, MyTextures.GetView(MyTextures.GetTexture(MyEnvironment.NightSkyboxPrefiltered, MyTextureEnum.CUBEMAP, true))); RC.Context.PixelShader.SetShaderResource(MyCommon.SHADOW_SLOT, MyRender11.m_shadowsHelper.ShaderView); RC.Context.PixelShader.SetShaderResource(MyCommon.AMBIENT_BRDF_LUT_SLOT, MyCommon.GetAmbientBrdfLut()); RC.BindSRV(MyCommon.AO_SLOT, MyScreenDependants.m_ambientOcclusion); MyScreenPass.RunFullscreenPixelFreq(MyGBuffer.Main.Get(MyGbufferSlot.LBuffer)); if (MyRender11.MultisamplingEnabled) { RC.SetPS(DirectionalEnvironmentLight_Sample); MyScreenPass.RunFullscreenSampleFreq(MyGBuffer.Main.Get(MyGbufferSlot.LBuffer)); } }
internal unsafe static void Precompute1(MyAtmosphere atmosphere, ref AtmosphereLuts luts) { var RC = MyImmediateRC.RC; float radiusGround = atmosphere.PlanetRadius; float RadiusAtmosphere = atmosphere.AtmosphereRadius; var cb = MyCommon.GetObjectCB(sizeof(AtmospherePrecomputeConstants)); RC.Context.ComputeShader.SetConstantBuffer(1, cb); AtmospherePrecomputeConstants constants = new AtmospherePrecomputeConstants(); constants.RadiusGround = radiusGround; constants.RadiusAtmosphere = RadiusAtmosphere; constants.RadiusLimit = RadiusAtmosphere + 1; constants.HeightScaleRayleighMie = atmosphere.HeightScaleRayleighMie; constants.BetaRayleighScattering = atmosphere.BetaRayleighScattering; constants.BetaMieScattering = atmosphere.BetaMieScattering; var mapping = MyMapping.MapDiscard(cb); mapping.stream.Write(constants); mapping.Unmap(); // transmittance RC.Context.ComputeShader.SetUnorderedAccessView(0, luts.TransmittanceLut.Uav); RC.SetCS(m_precomputeDensity); RC.Context.Dispatch(256 / 8, 64 / 8, 1); RC.Context.ComputeShader.SetUnorderedAccessView(0, null); // inscatter 1 RC.Context.ComputeShader.SetShaderResource(0, luts.TransmittanceLut.ShaderView); RC.SetCS(m_precomputeInscatter1); RC.Context.ComputeShader.SetUnorderedAccessViews(0, luts.InscatterLut.Uav); RC.Context.Dispatch(32 / 8, 128 / 8, 32 * 8); RC.Context.ComputeShader.SetUnorderedAccessViews(0, null as UnorderedAccessView, null as UnorderedAccessView); }
protected unsafe sealed override void RecordCommandsInternal(MyRenderableProxy proxy) { if (proxy.Material.Info.GeometryTextureRef.IsUsed) { MyRenderProxy.Fail(String.Format("Ensure all glass materials for model '{0}' are dynamic materials inside Environment.sbc", proxy.Mesh.Info.Name)); return; } MyTransparentMaterial material; if (!MyTransparentMaterials.TryGetMaterial(proxy.Material.Info.Name.String, out material)) { MyRenderProxy.Fail(String.Format("Missing transparent material '{0}'", proxy.Material.Info.Name)); return; } Stats.Draws++; MyRenderUtils.BindShaderBundle(RC, proxy.Shaders); SetProxyConstants(proxy); BindProxyGeometry(proxy, RC); ISrvBindable texture = MyManagers.FileTextures.GetTexture(material.Texture, MyFileTextureEnum.GUI, true); RC.PixelShader.SetSrv(0, texture); StaticGlassConstants glassConstants = new StaticGlassConstants(); var glassCB = MyCommon.GetObjectCB(sizeof(StaticGlassConstants)); RC.PixelShader.SetConstantBuffer(2, glassCB); var mapping = MyMapping.MapDiscard(glassCB); glassConstants.Color = material.Color; glassConstants.Reflective = material.Reflectivity; mapping.WriteAndPosition(ref glassConstants); mapping.Unmap(); var submesh = proxy.DrawSubmesh; RC.DrawIndexed(submesh.IndexCount, submesh.StartIndex, submesh.BaseVertex); ++Stats.Instances; }
internal static void DrawQuad(float x, float y, float w, float h, VertexShaderId?customVertexShader = null) { //RC.Context.PixelShader.Set(m_blitTextureShader); VertexShaderId usedVertexShader; if (!customVertexShader.HasValue) { usedVertexShader = m_screenVertexShader; } else { usedVertexShader = customVertexShader.Value; } RC.DeviceContext.VertexShader.Set(usedVertexShader); RC.DeviceContext.InputAssembler.InputLayout = m_inputLayout; var mapping = MyMapping.MapDiscard(m_quadBuffer.Buffer); var tmpFormat = new MyVertexFormatPosition2Texcoord(new Vector2(x, y), new Vector2(0, 0)); mapping.WriteAndPosition(ref tmpFormat); tmpFormat = new MyVertexFormatPosition2Texcoord(new Vector2(x + w, y + h), new Vector2(1, 1)); mapping.WriteAndPosition(ref tmpFormat); tmpFormat = new MyVertexFormatPosition2Texcoord(new Vector2(x, y + h), new Vector2(0, 1)); mapping.WriteAndPosition(ref tmpFormat); tmpFormat = new MyVertexFormatPosition2Texcoord(new Vector2(x, y), new Vector2(0, 0)); mapping.WriteAndPosition(ref tmpFormat); tmpFormat = new MyVertexFormatPosition2Texcoord(new Vector2(x + w, y), new Vector2(1, 0)); mapping.WriteAndPosition(ref tmpFormat); tmpFormat = new MyVertexFormatPosition2Texcoord(new Vector2(x + w, y + h), new Vector2(1, 1)); mapping.WriteAndPosition(ref tmpFormat); mapping.Unmap(); RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; RC.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_quadBuffer.Buffer, m_quadBuffer.Stride, 0)); RC.DeviceContext.Draw(6, 0); }
internal static void Prefilter(IUavArrayTexture probe, IUavArrayTexture prefiltered) { RC.ComputeShader.Set(m_prefilter); var mipLevels = prefiltered.MipmapLevels; var side = prefiltered.Size.X; uint probeSide = (uint)probe.Size.X; IConstantBuffer constantBuffer = MyCommon.GetObjectCB(32); RC.ComputeShader.SetConstantBuffer(1, constantBuffer); RC.ComputeShader.SetSrv(0, probe); RC.ComputeShader.SetSamplers(0, MySamplerStateManager.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(); RC.ComputeShader.SetUav(0, prefiltered.SubresourceUav(j, i)); RC.Dispatch((mipSide + 7) / 8, (mipSide + 7) / 8, 1); mipSide >>= 1; } } RC.ComputeShader.SetUav(0, null); RC.ComputeShader.SetSrv(0, null); RC.ComputeShader.Set(null); }
internal void FillConstantBuffer(ConstantsBufferId constantBuffer) { var mapping = MyMapping.MapDiscard(constantBuffer); for (int cascadeIndex = 0; cascadeIndex < m_initializedShadowCascadesCount; ++cascadeIndex) { var matrix = Matrix.Transpose(m_cascadeInfo[cascadeIndex].CurrentLocalToProjection * MyMatrixHelpers.ClipspaceToTexture); mapping.WriteAndPosition(ref matrix); } for (int cascadeIndex = m_initializedShadowCascadesCount; cascadeIndex < MaxShadowCascades; ++cascadeIndex) { mapping.WriteAndPosition(ref Matrix.Zero); } mapping.WriteAndPosition(ShadowCascadeSplitDepths, 0, ShadowCascadeSplitDepths.Length); float zero = 0; for (int splitIndex = ShadowCascadeSplitDepths.Length; splitIndex < MaxShadowCascades; ++splitIndex) { mapping.WriteAndPosition(ref zero); } for (int scaleIndex = 0; scaleIndex < ShadowCascadeScales.Length; ++scaleIndex) { Vector4 cascadeScale = ShadowCascadeScales[scaleIndex] / ShadowCascadeScales[0]; mapping.WriteAndPosition(ref cascadeScale); } for (int scaleIndex = ShadowCascadeScales.Length; scaleIndex < MaxShadowCascades; ++scaleIndex) { mapping.WriteAndPosition(ref Vector4.Zero); } float resolution = MyRender11.RenderSettings.ShadowQuality.ShadowCascadeResolution(); mapping.WriteAndPosition(ref resolution); for (int paddingIndex = 1; paddingIndex < 4; ++paddingIndex) { mapping.WriteAndPosition(ref zero); } mapping.Unmap(); }
internal static void DrawCascades() { RC.Context.PixelShader.Set(m_blitTextureArrayShader); RC.Context.PixelShader.SetShaderResource(0, MyShadows.m_cascadeShadowmapArray.ShaderView); var cb = MyCommon.GetMaterialCB(sizeof(uint)); RC.Context.PixelShader.SetConstantBuffer(5, cb); for (uint i = 0; i < 4; i++) { var mapping = MyMapping.MapDiscard(cb); mapping.stream.Write((float)i); mapping.Unmap(); DrawQuad(0, 256 * i, 410, 256); } RC.Context.PixelShader.SetShaderResource(0, null); }
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(IUavArrayTexture dst, IUavArrayTexture src0, IUavArrayTexture src1, float blendWeight) { //MyImmediateRC.RC.Context.CopyResource(src1.Resource, dst.Resource); RC.ComputeShader.Set(m_blend); var mipLevels = dst.MipmapLevels; var side = dst.Size.X; RC.ComputeShader.SetConstantBuffer(1, MyCommon.GetObjectCB(32)); RC.ComputeShader.SetSamplers(0, MySamplerStateManager.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.ComputeShader.SetSrv(0, src0.SubresourceSrv(j, i)); RC.ComputeShader.SetSrv(1, src1.SubresourceSrv(j, i)); // The single parameter version of SetUnorderedAccessView allocates RC.ComputeShader.SetUav(0, dst.SubresourceUav(j, i)); RC.Dispatch((mipSide + 7) / 8, (mipSide + 7) / 8, 1); mipSide >>= 1; } } RC.ComputeShader.SetUav(0, null); RC.ComputeShader.SetSrv(0, null); RC.ComputeShader.SetSrv(1, null); RC.ComputeShader.Set(null); }
/// <returns>True if the section was found</returns> private static void RecordMeshSectionCommands(MeshId model, LodMeshId lodModelId, MyRenderableComponent rendercomp, MyRenderLod renderLod, MyOutlineDesc desc, ref float maxThickness) { MeshSectionId sectionId; bool found = MyMeshes.TryGetMeshSection(model, rendercomp.CurrentLod, desc.SectionIndex, out sectionId); if (!found) { return; } OutlineConstantsLayout constants = new OutlineConstantsLayout(); MyMeshSectionInfo1 section = sectionId.Info; MyMeshSectionPartInfo1[] meshes = section.Meshes; for (int idx = 0; idx < meshes.Length; idx++) { MyMeshSectionPartInfo1 sectionInfo = meshes[idx]; if (renderLod.RenderableProxies.Length <= sectionInfo.PartIndex) { DebugRecordMeshPartCommands(model, desc.SectionIndex, rendercomp, renderLod, meshes, idx); return; } maxThickness = Math.Max(desc.Thickness, maxThickness); constants.Color = desc.Color.ToVector4(); if (desc.PulseTimeInFrames > 0) { constants.Color.W *= (float)Math.Pow((float)Math.Cos(2.0 * Math.PI * (float)MyRender11.Settings.GameplayFrame / (float)desc.PulseTimeInFrames), 2.0); } var mapping = MyMapping.MapDiscard(MyCommon.OutlineConstants); mapping.WriteAndPosition(ref constants); mapping.Unmap(); RC.BindShaders(renderLod.HighlightShaders[sectionInfo.PartIndex]); MyRenderableProxy proxy = renderLod.RenderableProxies[sectionInfo.PartIndex]; MyOutlinePass.Instance.RecordCommands(proxy, sectionInfo.PartSubmeshIndex, desc.InstanceId); } return; }
protected override unsafe void OnFrame(byte[] frameData) { var mapping = MyMapping.MapDiscard(m_texture.Resource); var lineSize = (uint)(SharpDX.DXGI.FormatHelper.SizeOfInBytes(VideoFormat) * VideoWidth); var rowPitch = mapping.dataBox.RowPitch; fixed(byte *ptr = frameData) { for (int y = 0; y < VideoHeight; y++) { var dst = new IntPtr((byte *)mapping.dataBox.DataPointer.ToPointer() + rowPitch * y); var src = new IntPtr((byte *)ptr + lineSize * y); MyMemory.CopyMemory(dst, src, lineSize); } } mapping.Unmap(); }
private static void DrawSrvArrayTexture(ISrvBindable textureArray, int texCount, int quadStartX, int quadStartY, int quadSize) { RC.PixelShader.Set(m_blitTextureArrayShader); RC.PixelShader.SetSrv(0, textureArray); var cb = MyCommon.GetMaterialCB(sizeof(uint)); RC.PixelShader.SetConstantBuffer(5, cb); for (uint cascadeIndex = 0; cascadeIndex < texCount; cascadeIndex++) { float index = (float)cascadeIndex; var mapping = MyMapping.MapDiscard(cb); mapping.WriteAndPosition(ref index); mapping.Unmap(); DrawQuad(quadStartX + (quadSize + quadStartX / 2) * cascadeIndex, quadStartY, quadSize, quadSize * MyRender11.ViewportResolution.Y / MyRender11.ViewportResolution.X); } RC.PixelShader.SetSrv(0, null); }
internal unsafe void RecordCommands(MyRenderableProxy proxy, MyFoliageStream stream, int voxelMatId, VertexShader vertexShader, InputLayout inputLayout, int materialIndex, int indexCount, int startIndex, int baseVertex) { MyObjectData objectData = proxy.ObjectData; objectData.LocalMatrix = proxy.WorldMatrix; MyMapping mapping; mapping = MyMapping.MapDiscard(RC.Context, proxy.objectBuffer); void *ptr = &objectData; mapping.stream.Write(new IntPtr(ptr), 0, sizeof(MyObjectData)); mapping.Unmap(); RC.SetCB(MyCommon.OBJECT_SLOT, proxy.objectBuffer); BindProxyGeometry(proxy); RC.SetVS(vertexShader); RC.SetIL(inputLayout); if (!stream.m_append) { Context.StreamOutput.SetTarget(stream.m_stream.Buffer, 0); stream.m_append = true; } else { Context.StreamOutput.SetTarget(stream.m_stream.Buffer, -1); } RC.SetCB(MyCommon.FOLIAGE_SLOT, MyCommon.FoliageConstants); mapping = MyMapping.MapDiscard(Context, MyCommon.FoliageConstants); mapping.stream.Write(MyVoxelMaterials1.Table[voxelMatId].FoliageDensity); mapping.stream.Write((uint)materialIndex); mapping.stream.Write((uint)voxelMatId); mapping.Unmap(); Context.DrawIndexed(indexCount, startIndex, baseVertex); }
private static void DrawCascadeArray(RwTexId textureArray, int quadStartX, int quadStartY, int quadSize) { RC.DeviceContext.PixelShader.Set(m_blitTextureArrayShader); RC.DeviceContext.PixelShader.SetShaderResource(0, textureArray.ShaderView); var cb = MyCommon.GetMaterialCB(sizeof(uint)); RC.DeviceContext.PixelShader.SetConstantBuffer(5, cb); for (uint cascadeIndex = 0; cascadeIndex < MyRender11.Settings.ShadowCascadeCount; cascadeIndex++) { float index = (float)cascadeIndex; var mapping = MyMapping.MapDiscard(cb); mapping.WriteAndPosition(ref index); mapping.Unmap(); DrawQuad(quadStartX + (quadSize + quadStartX / 2) * cascadeIndex, quadStartY, quadSize, quadSize * MyRender11.ViewportResolution.Y / MyRender11.ViewportResolution.X); } RC.DeviceContext.PixelShader.SetShaderResource(0, null); }
public static unsafe void MoveConstants(MyRenderContext rc, ref MyConstantsPack desc) { if (desc.CB == null) { return; } // IMPORTANT: It is optimisation but that has not been very well implemented, it is not considered multithread approach, usage of buffers in multiple passes, // reset device and and refillage of the buffers. More complex task to do it properly //if (m_staticData.m_constantsVersion.Get(desc.CB) != desc.Version) //{ // m_staticData.m_constantsVersion[desc.CB] = desc.Version; var mapping = MyMapping.MapDiscard(rc, desc.CB); mapping.WriteAndPosition(desc.Data, 0, desc.Data.Length); mapping.Unmap(); //} //MyRender11.ProcessDebugOutput(); }
internal static unsafe void RunForwardPostprocess(IRtvBindable rt, IDsvBindable depthDsv, ISrvBindable depthSrv, ref Matrix viewMatrix, ref Matrix projMatrix) { MyGpuProfiler.IC_BeginBlock("Postprocess"); var viewMatrixT = Matrix.Transpose(viewMatrix); var projMatrixT = Matrix.Transpose(projMatrix); var mapping = MyMapping.MapDiscard(RC, TransformConstants); mapping.WriteAndPosition(ref viewMatrixT); mapping.WriteAndPosition(ref projMatrixT); mapping.WriteAndPosition(ref MyRender11.Environment.Data.EnvironmentLight.SunLightDirection); mapping.Unmap(); RC.AllShaderStages.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.AllShaderStages.SetConstantBuffer(MyCommon.OBJECT_SLOT, TransformConstants); RC.SetDepthStencilState(MyDepthStencilStateManager.IgnoreDepthStencil); RC.SetRtv(rt); RC.PixelShader.SetSrv(0, depthSrv); MyFileTextureManager texManager = MyManagers.FileTextures; RC.PixelShader.SetSrv(MyCommon.SKYBOX_SLOT, texManager.GetTexture(MyRender11.Environment.Data.Skybox, MyFileTextureEnum.CUBEMAP, true)); RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); RC.PixelShader.Set(m_ps); MyScreenPass.DrawFullscreenQuad(new MyViewport(m_viewportSize, m_viewportSize)); MyGpuProfiler.IC_EndBlock(); var nearestAtmosphereId = MyAtmosphereRenderer.GetNearestAtmosphereId(); if (nearestAtmosphereId != null) { MyGpuProfiler.IC_BeginBlock("Atmosphere"); RC.PixelShader.SetSrv(0, depthSrv); var viewProj = viewMatrix * projMatrix; MyAtmosphereRenderer.RenderEnvProbe(MyRender11.Environment.Matrices.CameraPosition, ref viewProj, nearestAtmosphereId.Value); MyGpuProfiler.IC_EndBlock(); } RC.SetRtv(null); }
internal unsafe static void RenderSpotlights() { var coneMesh = MyMeshes.GetMeshId(X.TEXT("Models/Debug/Cone.mwm")); var buffers = MyMeshes.GetLodMesh(coneMesh, 0).Buffers; RC.SetVB(0, buffers.VB0.Buffer, buffers.VB0.Stride); RC.SetIB(buffers.IB.Buffer, buffers.IB.Format); RC.SetVS(SpotlightProxyVs); RC.SetIL(SpotlightProxyIL); RC.SetPS(SpotlightPs); RC.SetRS(MyRender11.m_nocullRasterizerState); var cb = MyCommon.GetObjectCB(sizeof(SpotlightConstants)); RC.SetCB(1, cb); RC.Context.PixelShader.SetSampler(MyCommon.SHADOW_SAMPLER_SLOT, MyRender11.m_shadowmapSamplerState); int index = 0; int casterIndex = 0; foreach (var id in VisibleSpotlights) { var mapping = MyMapping.MapDiscard(cb); mapping.stream.Write(MyLightRendering.Spotlights[index]); mapping.Unmap(); RC.Context.PixelShader.SetShaderResource(13, MyTextures.GetView(MyLights.Spotlights[id.Index].ReflectorTexture)); if (id.CastsShadows) { RC.Context.PixelShader.SetShaderResource(14, MyShadows.ShadowmapsPool[casterIndex].ShaderView); casterIndex++; } RC.Context.DrawIndexed(MyMeshes.GetLodMesh(coneMesh, 0).Info.IndicesNum, 0, 0); index++; } RC.SetRS(null); }
internal unsafe static void Precompute1(MyAtmosphere atmosphere, ref AtmosphereLuts luts) { var RC = MyImmediateRC.RC; float radiusGround = atmosphere.PlanetRadius; float RadiusAtmosphere = atmosphere.AtmosphereRadius; var cb = MyCommon.GetObjectCB(sizeof(AtmospherePrecomputeConstants)); RC.DeviceContext.ComputeShader.SetConstantBuffer(1, cb); var worldMatrix = atmosphere.WorldMatrix; worldMatrix.Translation -= MyEnvironment.CameraPosition; AtmospherePrecomputeConstants constants = new AtmospherePrecomputeConstants(); // Raise the ground a bit for better sunsets constants.RadiusGround = radiusGround * 1.01f * atmosphere.Settings.SeaLevelModifier; constants.RadiusAtmosphere = RadiusAtmosphere * atmosphere.Settings.AtmosphereTopModifier; constants.HeightScaleRayleighMie = atmosphere.HeightScaleRayleighMie * new Vector2(atmosphere.Settings.RayleighHeight, atmosphere.Settings.MieHeight); constants.BetaRayleighScattering = atmosphere.BetaRayleighScattering / atmosphere.Settings.RayleighScattering; constants.BetaMieScattering = atmosphere.BetaMieScattering / atmosphere.Settings.MieColorScattering; constants.MieG = atmosphere.Settings.MieG; constants.PlanetScaleFactor = atmosphere.PlanetScaleFactor; constants.AtmosphereScaleFactor = atmosphere.AtmosphereScaleFactor; constants.PlanetCentre = (Vector3)worldMatrix.Translation; constants.Intensity = atmosphere.Settings.Intensity; var mapping = MyMapping.MapDiscard(cb); mapping.WriteAndPosition(ref constants); mapping.Unmap(); // transmittance RC.DeviceContext.ComputeShader.SetUnorderedAccessView(0, luts.TransmittanceLut.Uav); RC.SetCS(m_precomputeDensity); RC.DeviceContext.Dispatch(512 / 8, 128 / 8, 1); RC.DeviceContext.ComputeShader.SetUnorderedAccessView(0, null); }
internal static void RebuildMaterialFoliageTable() { var array = new MaterialFoliageConstantsElem[256]; int N = Table.Length; for (int i = 0; i < N; i++) { var arrayTexId = MyTextures.GetTexture(Table[i].FoliageArray_Texture, MyTextureEnum.COLOR_METAL, true); array[i] = new MaterialFoliageConstantsElem { Scale = Table[i].FoliageScale, ScaleVar = Table[i].FoliageScaleVariation, TexturesNum = (uint)((Texture2D)MyTextures.Textures.Data[arrayTexId.Index].Resource).Description.ArraySize }; } var mapping = MyMapping.MapDiscard(MyCommon.MaterialFoliageTableConstants); mapping.stream.WriteRange(array, 0, N); mapping.Unmap(); }
static unsafe void TransferData() { var mapping = MyMapping.MapDiscard(RC.DeviceContext, MyCommon.GetObjectCB(sizeof(Matrix) * MaxCustomProjections)); for (int i = 0; i < MyRenderProxy.BillboardsViewProjectionRead.Count; i++) { var view = MyRenderProxy.BillboardsViewProjectionRead[i].View; var projection = MyRenderProxy.BillboardsViewProjectionRead[i].Projection; var scaleX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X; var scaleY = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y; var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X; var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height) / (float)MyRender11.ViewportResolution.Y; var viewportTransformation = new Matrix( scaleX, 0, 0, 0, 0, scaleY, 0, 0, 0, 0, 1, 0, offsetX, offsetY, 0, 1 ); var transpose = Matrix.Transpose(view * projection * viewportTransformation); mapping.WriteAndPosition(ref transpose); } for (int i = MyRenderProxy.BillboardsViewProjectionRead.Count; i < MaxCustomProjections; i++) { mapping.WriteAndPosition(ref Matrix.Identity); } mapping.Unmap(); int billboardCount = m_sorted + m_unsorted; mapping = MyMapping.MapDiscard(RC.DeviceContext, m_VB.Buffer); mapping.WriteAndPosition(m_vertexData, 0, billboardCount * 4); mapping.Unmap(); mapping = MyMapping.MapDiscard(RC.DeviceContext, m_SB.Buffer); mapping.WriteAndPosition(m_billboardData, 0, billboardCount); mapping.Unmap(); }
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); }
void CreateResizeOrFill <TDataElement>(string name, ref ISrvBuffer buffer, int size, TDataElement[] data, IntPtr rawData) where TDataElement : struct { if (buffer != null && buffer.ElementCount < size) { MyManagers.Buffers.Resize(buffer, size, newData: rawData); } if (buffer == null) { // We can't create ptr to a generic array here, we have to get it through param :'( buffer = MyManagers.Buffers.CreateSrv( name, size, Marshal.SizeOf <TDataElement>(), rawData, ResourceUsage.Dynamic); } else { var mapping = MyMapping.MapDiscard(MyImmediateRC.RC, buffer); mapping.WriteAndPosition(data, data.Length * Marshal.SizeOf <TDataElement>()); mapping.Unmap(); } }
internal static void RebuildMaterialFoliageTable() { var array = new MaterialFoliageConstantsElem[256]; for (int i = 0; i < 256; i++) { var mat = m_materials.Get(i); if (mat == null) { array[i] = new MaterialFoliageConstantsElem(); } else { array[i] = new MaterialFoliageConstantsElem(mat.FoliageScale, mat.FoliageRandomRescaleMult, mat.FoliageArraySize); } } var mapping = MyMapping.MapDiscard(MyCommon.MaterialFoliageTableConstants.Buffer); mapping.stream.WriteRange(array, 0, 256); mapping.Unmap(); }
internal unsafe void Update(MyRenderMessageDebugDrawMesh message) { edges = !message.Shaded; depth = message.DepthRead; if (vbuffer.ElementCount < message.VertexCount) { MyManagers.Buffers.Resize(vbuffer, message.VertexCount); } var mapping = MyMapping.MapDiscard(MyPrimitivesRenderer.RC, vbuffer); for (int i = 0; i < message.VertexCount; i++) { MyVertexFormatPositionColor vert = new MyVertexFormatPositionColor(Vector3.Transform(message.Vertices[i].Position, message.WorldMatrix), message.Vertices[i].Color); mapping.WriteAndPosition(ref vert); } mapping.Unmap(); message.Vertices.Clear(); }