static unsafe IConstantBuffer GetObjectCB(MyRenderContext RC, MyInstanceComponent instance, float stateData) { Vector4 col0, col1, col2; instance.GetMatrixCols(0, out col0, out col1, out col2); Matrix matrix = Matrix.Identity; matrix.SetRow(0, col0); matrix.SetRow(1, col1); matrix.SetRow(2, col2); matrix = Matrix.Transpose(matrix); 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; commonData.ColorMul = Vector3.One; commonData.KeyColor = new Vector3(0, -1f, 0f); commonData.CustomAlpha = stateData; mapping.WriteAndPosition(ref commonData); mapping.Unmap(); return(cb); }
public void Unregister() { MyRenderProxy.Assert(m_aabbTreeId != UNITIALISED_AABB_TREE_ID, "The cpu culled entity is not assigned!"); MyManagers.HierarchicalCulledEntities.AabbTree.RemoveProxy(m_aabbTreeId); m_aabbTreeId = -1; Owner = null; }
void IDrawableGroupStrategy.Fill(int bufferOffset, MyInstanceComponent instance, MyLod lod, int multiTransformI, int instanceMaterialOffsetInData, MyInstanceLodState state, float stateData) { MyVbConstantElement element = new MyVbConstantElement { //WorldMatrixRow0 = new Vector4(matrix.M11, matrix.M12, matrix.M13, matrix.M14), //WorldMatrixRow1 = new Vector4(matrix.M21, matrix.M22, matrix.M23, matrix.M24), //WorldMatrixRow2 = new Vector4(matrix.M31, matrix.M32, matrix.M33, matrix.M34), }; //Much faster approach than naive: instance.GetMatrixCols(multiTransformI, out element.WorldMatrixRow0, out element.WorldMatrixRow1, out element.WorldMatrixRow2); m_vbData[bufferOffset] = element; m_validElements++; }
public void DoWork(WorkData workData = null) { var frustum = m_cpuFrustumCullPass.Frustum; if (MyRender11.Settings.DrawNonMergeInstanced) { m_renderables.OverlapAllFrustum <MyCpuCulledEntity>(ref frustum, m_cpuFrustumCullPass.List, m_cpuFrustumCullPass.IsInsideList, 0, false); } m_visibleInstances.Clear(); foreach (var cullEntity in m_cpuFrustumCullPass.List) { MyInstanceComponent instance = cullEntity.Owner; if (instance.IsVisible(m_cpuFrustumCullPass.PassId)) { m_visibleInstances.Add(cullEntity.Owner); } } }
void IDrawableGroupStrategy.Fill(int bufferOffset, MyInstanceComponent instance, MyLod lod, int multiTransformI, int instanceMaterialOffsetInData, MyInstanceLodState state, float stateData) { HalfVector4 packedColorMultEmissivity = MyInstanceMaterial.Default.PackedColorMultEmissivity; if (instanceMaterialOffsetInData != -1) // if instance material is defined { MyInstanceMaterial instanceMaterial = instance.GetInstanceMaterial(instanceMaterialOffsetInData); packedColorMultEmissivity = instanceMaterial.PackedColorMultEmissivity; } else { packedColorMultEmissivity = instance.GlobalColorMultEmissivity; } HalfVector4 packedKeyColorDithering = instance.KeyColor.ToHalfVector4(); HalfVector4 dithering = new HalfVector4(); dithering.PackedValue = (ulong)HalfUtils.Pack(stateData); packedKeyColorDithering.PackedValue = packedKeyColorDithering.PackedValue | dithering.PackedValue << 48; MyVbConstantElement element = new MyVbConstantElement { //WorldMatrixRow0 = new Vector4(matrix.M11, matrix.M12, matrix.M13, matrix.M14), //WorldMatrixRow1 = new Vector4(matrix.M21, matrix.M22, matrix.M23, matrix.M24), //WorldMatrixRow2 = new Vector4(matrix.M31, matrix.M32, matrix.M33, matrix.M34), KeyColorDithering = packedKeyColorDithering, ColorMultEmissivity = packedColorMultEmissivity, }; // much faster approach than naive: instance.GetMatrixCols(multiTransformI, out element.WorldMatrixRow0, out element.WorldMatrixRow1, out element.WorldMatrixRow2); m_vbData[bufferOffset] = element; m_validElements++; }
public void Register(BoundingBoxD boundingBox, MyInstanceComponent owner) { MyRenderProxy.Assert(m_aabbTreeId == UNITIALISED_AABB_TREE_ID, "The cpu culled entity has been initialised before!"); m_aabbTreeId = MyManagers.HierarchicalCulledEntities.AabbTree.AddProxy(ref boundingBox, this, 0); Owner = owner; }
public static void Run(IRtvBindable target, ICustomTexture fxaaTarget, IDepthStencil depthStencilCopy) { if (!HasHighlights) { return; } ProfilerShort.Begin("MyHighlight.Run"); MyGpuProfiler.IC_BeginBlock("MyHighlight.Run"); // set resolved depth/ stencil // render all with proper depth-stencil state // blur // blend to main target testing with stencil again MyHighlightPass.Instance.ViewProjection = MyRender11.Environment.Matrices.ViewProjectionAt0; MyHighlightPass.Instance.Viewport = new MyViewport(MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y); MyHighlightPass.Instance.PerFrame(); MyHighlightPass.Instance.Begin(); int samples = MyRender11.Settings.User.AntialiasingMode.SamplesCount(); IBorrowedRtvTexture rgba8_1 = MyManagers.RwTexturesPool.BorrowRtv("MyHighlight.Rgba8_1", Format.R8G8B8A8_UNorm_SRgb, samples); RC.ClearRtv(rgba8_1, new SharpDX.Color4(0, 0, 0, 0)); RC.SetRtv(depthStencilCopy, MyDepthStencilAccess.DepthReadOnly, rgba8_1); foreach (var pair in m_highlights) { MyActor actor = MyIDTracker <MyActor> .FindByID(pair.Key); if (actor == null) { MyRenderProxy.Fail("The actor cannot be found for highlight. This bug is outside of the renderer."); continue; } MyRenderableComponent renderableComponent = actor.GetRenderable(); MyInstanceComponent instanceComponent = actor.GetInstance(); if (renderableComponent != null) { DrawRenderableComponent(actor, renderableComponent, pair.Value); } else if (instanceComponent != null) { DrawInstanceComponent(instanceComponent, pair.Value); } else { // If an actor has been removed without removing outlines, just remove the outlines too m_keysToRemove.Add(pair.Key); MyRenderProxy.Fail("The actor has been removed, but the highligh is still active. This bug is caused by the issue out of the renderer."); } } MyHighlightPass.Instance.End(); RC.SetBlendState(null); foreach (var outlineKey in m_keysToRemove) { m_highlights.Remove(outlineKey); } m_keysToRemove.Clear(); ISrvBindable initialSourceView = rgba8_1; IRtvBindable renderTargetview = rgba8_1; float maxThickness = 0f; foreach (var pair in m_highlights) { foreach (MyHighlightDesc descriptor in pair.Value) { maxThickness = Math.Max(maxThickness, descriptor.Thickness); } } if (maxThickness > 0) { IBorrowedRtvTexture rgba8_2 = MyManagers.RwTexturesPool.BorrowRtv("MyHighlight.Rgba8_2", Format.R8G8B8A8_UNorm_SRgb); MyBlur.Run(renderTargetview, rgba8_2, initialSourceView, (int)Math.Round(maxThickness), MyBlur.MyBlurDensityFunctionType.Exponential, 0.25f, MyDepthStencilStateManager.IgnoreDepthStencil); rgba8_2.Release(); } MyGpuProfiler.IC_EndBlock(); ProfilerShort.End(); BlendHighlight(target, rgba8_1, fxaaTarget, depthStencilCopy); }
static void DrawInstanceComponent(MyInstanceComponent instanceComponent, List <MyHighlightDesc> highlightDescs) { MyRenderContext RC = MyRender11.RC; // common settings (combination of MyHighlightPass.cs and MyRenderingPass.cs): MyMapping mapping = MyMapping.MapDiscard(MyCommon.ProjectionConstants); Matrix matrix = MyRender11.Environment.Matrices.ViewProjectionAt0; matrix = Matrix.Transpose(matrix); mapping.WriteAndPosition(ref matrix); mapping.Unmap(); RC.VertexShader.SetConstantBuffer(MyCommon.FRAME_SLOT, MyCommon.FrameConstants); RC.VertexShader.SetConstantBuffer(MyCommon.PROJECTION_SLOT, MyCommon.ProjectionConstants); RC.PixelShader.SetSamplers(0, MySamplerStateManager.StandardSamplers); RC.PixelShader.SetSrv(MyCommon.DITHER_8X8_SLOT, MyGeneratedTextureManager.Dithering8x8Tex); //RC.AllShaderStages.SetConstantBuffer(MyCommon.ALPHAMASK_VIEWS_SLOT, MyCommon.AlphamaskViewsConstants); // not used! Maybe impostors? RC.SetDepthStencilState(MyDepthStencilStateManager.WriteHighlightStencil, MyHighlight.HIGHLIGHT_STENCIL_MASK); RC.SetBlendState(null); RC.SetPrimitiveTopology(PrimitiveTopology.TriangleList); RC.SetRasterizerState(MyRasterizerStateManager.NocullRasterizerState); RC.SetScreenViewport(); RC.PixelShader.SetConstantBuffer(4, MyCommon.HighlightConstants); MyLod lod = instanceComponent.GetHighlightLod(); MyInstanceLodState stateId = MyInstanceLodState.Solid; float stateData = 0; RC.SetIndexBuffer(lod.IB); RC.SetVertexBuffer(0, lod.VB0); IConstantBuffer objectCB = GetObjectCB(RC, instanceComponent, stateData); RC.VertexShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB); RC.PixelShader.SetConstantBuffer(MyCommon.OBJECT_SLOT, objectCB); foreach (MyHighlightDesc desc in highlightDescs) { MyHighlightDesc descRef = desc; WriteHighlightConstants(ref descRef); if (string.IsNullOrEmpty(desc.SectionName)) { foreach (var part in lod.Parts) { DrawHighlightedPart(RC, part, stateId); } } else { if (lod.HighlightSections != null && lod.HighlightSections.ContainsKey(desc.SectionName)) { foreach (var part in lod.HighlightSections[desc.SectionName].Parts) { DrawHighlightedPart(RC, part, stateId); } } } } }