Example #1
0
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> elements, List <MyRender.MyRenderElement> transparentElements)
        {
            if (MyRender.Settings.SkipVoxels)
            {
                return;
            }

            Debug.Assert(lodTypeEnum == MyLodTypeEnum.LOD0 || lodTypeEnum == MyLodTypeEnum.LOD_BACKGROUND);

            double distanceMin = (MyRenderCamera.Position - m_aabb.Min).Length();
            double distanceMax = (MyRenderCamera.Position - m_aabb.Max).Length();

            distanceMin = Math.Min(distanceMin, distanceMax);
            distanceMax = Math.Max(distanceMin, distanceMax);

            if (distanceMin > MyRenderCamera.FAR_PLANE_DISTANCE && lodTypeEnum == MyLodTypeEnum.LOD0)
            {
                return;
            }
            if (distanceMax < MyRenderCamera.NEAR_PLANE_FOR_BACKGROUND && lodTypeEnum == MyLodTypeEnum.LOD_BACKGROUND)
            {
                return;
            }
            base.GetRenderElements(lodTypeEnum, elements, transparentElements);
        }
        public static void SetupVoxelEntity(MyLodTypeEnum lod, MyEffectBase shader, MyRender.MyRenderElement renderElement)
        {
            MyEffectVoxels effectVoxels = shader as MyEffectVoxels;

            {
                MatrixD worldMatrixD = renderElement.WorldMatrix;
                worldMatrixD.Translation -= MyRenderCamera.Position;
                var worldMatrix = (Matrix)worldMatrixD;
                effectVoxels.SetWorldMatrix(ref worldMatrix);
            }

            var voxelCell = renderElement.RenderObject as MyRenderVoxelCell;
            if (voxelCell != null)
            {
                MyRenderVoxelCell.EffectArgs args;
                voxelCell.GetEffectArgs(out args);
                effectVoxels.VoxelVertex.SetArgs(ref args);
            }

            effectVoxels.SetDiffuseColor(Vector3.One);
            if (MyRenderSettings.DebugClipmapLodColor && renderElement.VoxelBatch.Lod < MyRenderVoxelCell.LOD_COLORS.Length)
            {
                effectVoxels.SetDiffuseColor(MyRenderVoxelCell.LOD_COLORS[renderElement.VoxelBatch.Lod].ToVector3());
            }
            effectVoxels.EnablePerVertexAmbient(MyRender.Settings.EnablePerVertexVoxelAmbient);

            if (lod == MyLodTypeEnum.LOD_BACKGROUND && renderElement.RenderObject is MyRenderVoxelCellBackground)
            {
                SetupAtmosphere(effectVoxels, renderElement.RenderObject as MyRenderVoxelCellBackground);
            }
        }
        private void SetupRenderElement(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> elements)
        {
            if (lodTypeEnum != MyLodTypeEnum.LOD0)
            {
                return;
            }

            MyRender.MyRenderElement renderElement;
            MyRender.AllocateRenderElement(out renderElement);
            if (MyRender.IsRenderOverloaded)
            {
                return;
            }

            renderElement.VertexBuffer      = m_vertexBuffer;
            renderElement.VertexCount       = m_vertices.Length;
            renderElement.VertexStride      = MyVertexFormatPositionNormalTextureTangent.Stride;
            renderElement.VertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration;

            renderElement.IndexBuffer = m_indexBuffer;
            renderElement.IndexStart  = 0;
            renderElement.TriCount    = m_indices.Length / 3;

            renderElement.InstanceBuffer = null;
            MatrixD.CreateTranslation(ref m_worldPosition, out renderElement.WorldMatrix);
            MatrixD.Multiply(ref renderElement.WorldMatrix, ref MyRenderCamera.InversePositionTranslationMatrix, out renderElement.WorldMatrixForDraw);
            renderElement.DrawTechnique = MyMeshDrawTechnique.MESH;
            renderElement.Material      = m_ropeMaterial;
            renderElement.RenderObject  = this;
            renderElement.Color         = new Vector3(1f, 0.93f, 0.42f);

            elements.Add(renderElement);
        }
Example #4
0
 public MyVoxelPrecalcTaskItem(MyLodTypeEnum type, MyVoxelMap voxelMap, MyVoxelGeometry.CellData cache, Vector3I voxelStart)
 {
     Type       = type;
     VoxelMap   = voxelMap;
     Cache      = cache;
     VoxelStart = voxelStart;
 }
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List<MyRender.MyRenderElement> elements, List<MyRender.MyRenderElement> transparentElements)
        {
            if (MyRender.Settings.SkipVoxels)
                return;

            Debug.Assert(lodTypeEnum == MyLodTypeEnum.LOD0 || lodTypeEnum == MyLodTypeEnum.LOD_BACKGROUND);

            foreach (MyRenderVoxelBatch batch in m_batches)
            {
                if (batch.IndexBuffer == null)
                    continue;

                MyRender.MyRenderElement renderElement;
                MyRender.AllocateRenderElement(out renderElement);

                if (!MyRender.IsRenderOverloaded)
                {
                    //renderElement.DebugName = this.Name;
                    SetupRenderElement(batch, renderElement);

                    renderElement.DrawTechnique = MyMeshDrawTechnique.VOXEL_MAP;
                    renderElement.Material = m_fakeVoxelMaterial;

                    elements.Add(renderElement);
                }
                else if (renderElement == null)
                {
                    break;
                }

            }
        }
 public MyVoxelPrecalcTaskItem(MyLodTypeEnum type, MyVoxelMap voxelMap, MyVoxelCacheCellData cache, MyMwcVector3Int voxelStart)
 {
     Type       = type;
     VoxelMap   = voxelMap;
     Cache      = cache;
     VoxelStart = voxelStart;
 }
        public static void SetupVoxelEntity(MyLodTypeEnum lod,MyEffectBase shader, MyRender.MyRenderElement renderElement)
        {
            MyEffectVoxels effectVoxels = shader as MyEffectVoxels;

            {
                MatrixD worldMatrixD = renderElement.WorldMatrix;
                worldMatrixD.Translation -= MyRenderCamera.Position;
                var worldMatrix = (Matrix)worldMatrixD;
                effectVoxels.SetWorldMatrix(ref worldMatrix);
            }

            //effectVoxels.SetVoxelMapPosition((Vector3)(renderElement.WorldMatrix.Translation - MyRenderCamera.Position));
            //effectVoxels.SetPositionLocalOffset((Vector3)(renderElement.WorldMatrix.Right));
            //effectVoxels.SetPositionLocalScale((Vector3)(renderElement.WorldMatrix.Up));
            //effectVoxels.SetLodBounds(new Vector2((float)renderElement.WorldMatrix.M14, (float)renderElement.WorldMatrix.M24));
            effectVoxels.SetDiffuseColor(Vector3.One);
            if (MyRenderSettings.DebugClipmapLodColor && renderElement.VoxelBatch.Lod < MyRenderVoxelCell.LOD_COLORS.Length)
            {
                effectVoxels.SetDiffuseColor(MyRenderVoxelCell.LOD_COLORS[renderElement.VoxelBatch.Lod].ToVector3());
            }
            effectVoxels.EnablePerVertexAmbient(MyRender.Settings.EnablePerVertexVoxelAmbient);

            if (lod == MyLodTypeEnum.LOD_BACKGROUND && renderElement.RenderObject is MyRenderVoxelCellBackground)
            {
                SetupAtmosphere(effectVoxels, renderElement.RenderObject as MyRenderVoxelCellBackground);
            }
        }
Example #8
0
        private static void DrawVoxels(MySortedElements sortedElements, MyLodTypeEnum lod, MyRenderVoxelBatchType batchType, ref int ibChangesStats)
        {
            int index   = sortedElements.GetVoxelIndex(lod, batchType);
            var matDict = sortedElements.Voxels[index];

            if (matDict.RenderElementCount == 0)
            {
                return;
            }

            var tech = GetTechnique(batchType == MyRenderVoxelBatchType.SINGLE_MATERIAL ? MyMeshDrawTechnique.VOXEL_MAP_SINGLE : MyMeshDrawTechnique.VOXEL_MAP_MULTI);

            var shader = (MyEffectVoxels)tech.PrepareAndBeginShader(m_currentSetup, lod);

            MyPerformanceCounter.PerCameraDrawWrite.TechniqueChanges[(int)lod]++;

            foreach (var mat in matDict.Voxels)
            {
                var firstElement = mat.Value.FirstOrDefault();
                if (firstElement == null)
                {
                    continue;
                }


                // Setup material
                tech.SetupVoxelMaterial(shader, firstElement.VoxelBatch);
                MyPerformanceCounter.PerCameraDrawWrite.MaterialChanges[(int)lod]++;

                MyRenderObject lastRenderObject = null;
                VertexBuffer   lastVertexBuffer = null;

                foreach (var renderElement in mat.Value)
                {
                    if (!object.ReferenceEquals(lastVertexBuffer, renderElement.VertexBuffer))
                    {
                        lastVertexBuffer       = renderElement.VertexBuffer;
                        GraphicsDevice.Indices = renderElement.IndexBuffer;
                        GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);
                        GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;
                        MyPerformanceCounter.PerCameraDrawWrite.VertexBufferChanges[(int)lod]++;
                        ibChangesStats++;
                    }

                    if (lastRenderObject != renderElement.RenderObject)
                    {
                        lastRenderObject = renderElement.RenderObject;
                        MyPerformanceCounter.PerCameraDrawWrite.EntityChanges[(int)lod]++;
                        tech.SetupEntity(shader, renderElement);
                        shader.D3DEffect.CommitChanges();
                    }

                    GraphicsDevice.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);
                    MyPerformanceCounter.PerCameraDrawWrite.TotalDrawCalls++;
                }
            }

            shader.End();
            // Technique End
        }
Example #9
0
 public MyVoxelPrecalcTaskItem(MyLodTypeEnum type, MyVoxelMap voxelMap, MyVoxelCacheCellData cache, MyMwcVector3Int voxelStart)
 {
     Type = type;
     VoxelMap = voxelMap;
     Cache = cache;
     VoxelStart = voxelStart;
 }
 public MyVoxelPrecalcTaskItem(MyLodTypeEnum type, MyVoxelMap voxelMap, MyVoxelGeometry.CellData cache, Vector3I voxelStart)
 {
     Type = type;
     VoxelMap = voxelMap;
     Cache = cache;
     VoxelStart = voxelStart;
 }
        public static void SetupVoxelEntity(MyLodTypeEnum lod, MyEffectBase shader, MyRender.MyRenderElement renderElement)
        {
            MyEffectVoxels effectVoxels = shader as MyEffectVoxels;

            {
                MatrixD worldMatrixD = renderElement.WorldMatrix;
                worldMatrixD.Translation -= MyRenderCamera.Position;
                var worldMatrix = (Matrix)worldMatrixD;
                effectVoxels.SetWorldMatrix(ref worldMatrix);
            }

            var voxelCell = renderElement.RenderObject as MyRenderVoxelCell;

            if (voxelCell != null)
            {
                MyRenderVoxelCell.EffectArgs args;
                voxelCell.GetEffectArgs(out args);
                effectVoxels.VoxelVertex.SetArgs(ref args);
            }

            effectVoxels.SetDiffuseColor(Vector3.One);
            if (MyRenderSettings.DebugClipmapLodColor && renderElement.VoxelBatch.Lod < MyRenderVoxelCell.LOD_COLORS.Length)
            {
                effectVoxels.SetDiffuseColor(MyRenderVoxelCell.LOD_COLORS[renderElement.VoxelBatch.Lod].ToVector3());
            }
            effectVoxels.EnablePerVertexAmbient(
                MyRenderSettings.EnableVoxelAo,
                MyRenderSettings.VoxelAoMin,
                MyRenderSettings.VoxelAoMax,
                MyRenderSettings.VoxelAoOffset);
        }
Example #12
0
        internal CellData GetCell(MyLodTypeEnum lod, ref Vector3I cellCoord)
        {
            Int64 key = MySession.Static.VoxelMaps.GetCellHashCode(m_voxelMap.VoxelMapId, ref cellCoord, lod);

            CellData cachedCell;

            if (!m_cellsByCoordinate.TryGetValue(key, out cachedCell))
            {
                if (CellAffectsTriangles(m_voxelMap.Storage, lod, ref cellCoord))
                {
                    cachedCell = CellPool.AllocateOrCreate();
                    m_cellsByCoordinate.Add(key, cachedCell);

                    Profiler.Begin("Cell precalc");
                    MyVoxelPrecalc.PrecalcImmediatelly(
                        new MyVoxelPrecalcTaskItem(
                            lod,
                            m_voxelMap,
                            cachedCell,
                            new Vector3I(
                                cellCoord.X * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS,
                                cellCoord.Y * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS,
                                cellCoord.Z * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS)));
                    Profiler.End();
                }
            }

            return(cachedCell);
        }
        public static void SetupVoxelEntity(MyLodTypeEnum lod, MyEffectBase shader, MyRender.MyRenderElement renderElement)
        {
            MyEffectVoxels effectVoxels = shader as MyEffectVoxels;

            {
                MatrixD worldMatrixD = renderElement.WorldMatrix;
                worldMatrixD.Translation -= MyRenderCamera.Position;
                var worldMatrix = (Matrix)worldMatrixD;
                effectVoxels.SetWorldMatrix(ref worldMatrix);
            }

            //effectVoxels.SetVoxelMapPosition((Vector3)(renderElement.WorldMatrix.Translation - MyRenderCamera.Position));
            //effectVoxels.SetPositionLocalOffset((Vector3)(renderElement.WorldMatrix.Right));
            //effectVoxels.SetPositionLocalScale((Vector3)(renderElement.WorldMatrix.Up));
            //effectVoxels.SetLodBounds(new Vector2((float)renderElement.WorldMatrix.M14, (float)renderElement.WorldMatrix.M24));
            effectVoxels.SetDiffuseColor(Vector3.One);
            if (MyRenderSettings.DebugClipmapLodColor && renderElement.VoxelBatch.Lod < MyRenderVoxelCell.LOD_COLORS.Length)
            {
                effectVoxels.SetDiffuseColor(MyRenderVoxelCell.LOD_COLORS[renderElement.VoxelBatch.Lod].ToVector3());
            }
            effectVoxels.EnablePerVertexAmbient(MyRender.Settings.EnablePerVertexVoxelAmbient);

            if (lod == MyLodTypeEnum.LOD_BACKGROUND && renderElement.RenderObject is MyRenderVoxelCellBackground)
            {
                SetupAtmosphere(effectVoxels, renderElement.RenderObject as MyRenderVoxelCellBackground);
            }
        }
Example #14
0
 internal void Add(MyLodTypeEnum lod, List <MyRender.MyRenderElement> m_renderElements)
 {
     foreach (var el in m_renderElements)
     {
         Add(lod, el);
     }
 }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = (MyEffectModelsDNS)MyRender.GetEffect(MyEffects.ModelDNS);
            SetupBaseEffect(shader, setup, lodType);

            MyEffectModelsDNS dnsShader = shader as MyEffectModelsDNS;

            dnsShader.SetHalfPixel(MyRenderCamera.Viewport.Width, MyRenderCamera.Viewport.Height);
            dnsShader.SetScale(MyRender.GetScaleForViewport(MyRender.GetRenderTarget(MyRenderTargets.Depth)));

            bool useDepth = lodType != MyLodTypeEnum.LOD_NEAR;

            if (useDepth)
            {
                // DepthStencilState.DepthRead;
                MyStateObjects.DepthStencil_TestFarObject_DepthReadOnly.Apply();
            }
            else
            {
                DepthStencilState.DepthRead.Apply();
            }
            MyStateObjects.Holo_BlendState.Apply();

            shader.ApplyHolo(!useDepth);
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
Example #16
0
 internal void Add(MyLodTypeEnum lod, List<MyRender.MyRenderElement> m_renderElements)
 {
     foreach (var el in m_renderElements)
     {
         Add(lod, el);
     }
 }
Example #17
0
 public void Add(MyLodTypeEnum lod, MyRender.MyRenderElement renderElement)
 {
     if (renderElement.DrawTechnique == MyMeshDrawTechnique.VOXEL_MAP)
         AddVoxel(lod, renderElement);
     else
         AddModel(lod, renderElement);
 }
Example #18
0
        public override void GetRenderElementsForShadowmap(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> renderElements, List <MyRender.MyRenderElement> transparentRenderElements)
        {
            if (MyRender.Settings.SkipVoxels)
            {
                return;
            }


            //  Get non-empty render cells visible to the frustum and sort them by distance to camera
            MyRender.GetRenderProfiler().StartProfilingBlock("GetElements from MyVoxelMap");

            foreach (MyRenderVoxelBatch batch in m_batches)
            {
                if (batch.IndexCount == 0)
                {
                    continue;
                }

                MyRender.MyRenderElement renderElement;
                MyRender.AllocateRenderElement(out renderElement);

                if (!MyRender.IsRenderOverloaded)
                {
                    SetupRenderElement(batch, renderElement);
                    renderElement.Dithering = 0;

                    renderElements.Add(renderElement);
                }
            }

            MyRender.GetRenderProfiler().EndProfilingBlock();
        }
Example #19
0
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> elements, List <MyRender.MyRenderElement> transparentElements)
        {
            if (MyRender.Settings.SkipVoxels)
            {
                return;
            }

            Debug.Assert(lodTypeEnum == MyLodTypeEnum.LOD0);

            foreach (MyRenderVoxelBatch batch in m_batches)
            {
                if (batch.IndexBuffer == null)
                {
                    continue;
                }

                MyRender.MyRenderElement renderElement;
                MyRender.AllocateRenderElement(out renderElement);

                if (!MyRender.IsRenderOverloaded)
                {
                    //renderElement.DebugName = this.Name;
                    SetupRenderElement(batch, renderElement);

                    renderElement.DrawTechnique = MyMeshDrawTechnique.VOXEL_MAP;
                    renderElement.Material      = m_fakeVoxelMaterial;

                    elements.Add(renderElement);
                }
                else if (renderElement == null)
                {
                    break;
                }
            }
        }
        private static void DrawVoxels(MyLodTypeEnum lod, MyVoxelCacheCellRenderBatchType batchType, ref int ibChangesStats)
        {
            int index = m_sortedElements.GetVoxelIndex(lod, batchType);
            var matDict = m_sortedElements.Voxels[index];

            if (matDict.RenderElementCount == 0)
                return;

            // Technique start
            var shader = GetShader(MyMeshDrawTechnique.VOXEL_MAP);
            SetupShaderPerDraw(shader, MyMeshDrawTechnique.VOXEL_MAP);
            BeginShaderAlternative(shader, MyMeshDrawTechnique.VOXEL_MAP, batchType);
            shader.Begin();

            MyPerformanceCounter.PerCameraDraw.TechniqueChanges[(int)lod]++;

            foreach (var mat in matDict.Voxels)
            {
                var firstElement = mat.Value.FirstOrDefault();
                if (firstElement == null)
                    continue;

                // Setup material
                SetupShaderForMaterialAlternative(shader, batchType, firstElement.VoxelBatch.Material0, firstElement.VoxelBatch.Material1, firstElement.VoxelBatch.Material2);
                MyPerformanceCounter.PerCameraDraw.MaterialChanges[(int)lod]++;
                
                MyEntity lastEntity = null;
                VertexBuffer lastVertexBuffer = null;

                foreach (var renderElement in mat.Value)
                {
                    if (!object.ReferenceEquals(lastVertexBuffer, renderElement.VertexBuffer))
                    {
                        lastVertexBuffer = renderElement.VertexBuffer;
                        m_device.Indices = renderElement.IndexBuffer;
                        m_device.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);
                        m_device.VertexDeclaration = renderElement.VertexDeclaration;
                        MyPerformanceCounter.PerCameraDraw.VertexBufferChanges[(int)lod]++;
                        ibChangesStats++;
                    }

                    if (lastEntity != renderElement.Entity)
                    {
                        lastEntity = renderElement.Entity;
                        MyPerformanceCounter.PerCameraDraw.EntityChanges[(int)lod]++;
                        SetupShaderForEntity(shader, renderElement);
                        shader.D3DEffect.CommitChanges();
                    }

                    m_renderProfiler.StartProfilingBlock("DrawIndexedPrimitives");
                    m_device.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);
                    MyPerformanceCounter.PerCameraDraw.TotalDrawCalls++;
                    m_renderProfiler.EndProfilingBlock();
                }
            }

            shader.End();
            // Technique End
        }
 private static void LoadCellInBackground(
     MyVoxelMap voxelMap, ref MyMwcVector3Int renderCellCoord, MyLodTypeEnum cellHashType)
 {
     System.Diagnostics.Debug.Assert(false, "Not implemented");
     MyCommonDebugUtils.AssertDebug(voxelMap != null);
     //m_queue.Enqueue(new RenderCellLoadJob(voxelMap, ref renderCellCoord, cellHashType));
     //m_event.Set();
 }
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List<MyRender.MyRenderElement> elements, List<MyRender.MyRenderElement> transparentElements)
        {
            MyPerformanceCounter.PerCameraDrawWrite.EntitiesRendered++;

            //   GetRenderProfiler().StartNextBlock("Collect render elements");
            CollectRenderElements(elements, transparentElements, m_lods[0].Model, m_lods[0].MeshMaterials, 0);
            //   GetRenderProfiler().EndProfilingBlock();
        }
Example #23
0
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> elements, List <MyRender.MyRenderElement> transparentElements)
        {
            MyPerformanceCounter.PerCameraDrawWrite.EntitiesRendered++;

            //   GetRenderProfiler().StartNextBlock("Collect render elements");
            CollectRenderElements(elements, transparentElements, m_lods[0].Model, m_lods[0].MeshMaterials, 0);
            //   GetRenderProfiler().EndProfilingBlock();
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = (MyEffectAtmosphere)MyRender.GetEffect(MyEffects.Atmosphere);
            SetupBaseEffect(shader, setup, lodType);

            shader.SetSurfaceTechnique();
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = (MyEffectModelsDNS)MyRender.GetEffect(MyEffects.ModelDNS);
            SetupBaseEffect(shader, setup, lodType);

            shader.SetTechnique(MyRenderConstants.RenderQualityProfile.ModelsInstancedSkinnedTechnique);
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = (MyEffectModelsDNS)MyRender.GetEffect(MyEffects.ModelDNS);
            SetupBaseEffect(shader, setup, lodType);

            shader.ApplyMasked();
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            MyEffectVoxelsDebris shader = (MyEffectVoxelsDebris)MyRender.GetEffect(MyEffects.VoxelDebrisMRT);
            SetupBaseEffect(shader, setup, lodType);

            shader.SetTechnique(MyRenderConstants.RenderQualityProfile.VoxelsRenderTechnique);
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
        //  Update cell - immediately after it was last time used. It will get higher priority and won't be flushed when AddCell() called next time.
        private static void UpdateCell(int voxelMapId, ref Vector3I cellCoord, MyLodTypeEnum cellHashType)
        {
            Int64 key = MySession.Static.VoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, cellHashType);
            LinkedListNode <MyVoxelCacheCellRender> ret = m_cellsByCoordinate[key];

            //  Move it to the end of priority linked list
            Debug.Assert(Thread.CurrentThread == MySandboxGame.Static.UpdateThread);
            m_priority.Remove(ret);
            m_priority.AddLast(ret);
        }
        private static void DrawVoxels(MySortedElements sortedElements, MyLodTypeEnum lod, MyRenderVoxelBatchType batchType, ref int ibChangesStats)
        {
            int index = sortedElements.GetVoxelIndex(lod, batchType);
            var matDict = sortedElements.Voxels[index];

            if (matDict.RenderElementCount == 0)
                return;

            var tech = GetTechnique(batchType == MyRenderVoxelBatchType.SINGLE_MATERIAL ? MyMeshDrawTechnique.VOXEL_MAP_SINGLE : MyMeshDrawTechnique.VOXEL_MAP_MULTI);
           
            var shader = (MyEffectVoxels)tech.PrepareAndBeginShader(m_currentSetup, lod);         
            MyPerformanceCounter.PerCameraDrawWrite.TechniqueChanges[(int)lod]++;
           
            foreach (var mat in matDict.Voxels)
            {
                var firstElement = mat.Value.FirstOrDefault();
                if (firstElement == null)
                    continue;

               
                // Setup material
                tech.SetupVoxelMaterial(shader, firstElement.VoxelBatch);
                MyPerformanceCounter.PerCameraDrawWrite.MaterialChanges[(int)lod]++;

                MyRenderObject lastRenderObject = null;
                VertexBuffer lastVertexBuffer = null;

                foreach (var renderElement in mat.Value)
                {
                    if (!object.ReferenceEquals(lastVertexBuffer, renderElement.VertexBuffer))
                    {
                        lastVertexBuffer = renderElement.VertexBuffer;
                        GraphicsDevice.Indices = renderElement.IndexBuffer;
                        GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);
                        GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;
                        MyPerformanceCounter.PerCameraDrawWrite.VertexBufferChanges[(int)lod]++;
                        ibChangesStats++;
                    }

                    if (lastRenderObject != renderElement.RenderObject)
                    {
                        lastRenderObject = renderElement.RenderObject;
                        MyPerformanceCounter.PerCameraDrawWrite.EntityChanges[(int)lod]++;
                        tech.SetupEntity(shader, renderElement);                     
                        shader.D3DEffect.CommitChanges();
                    }

                    GraphicsDevice.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);
                    MyPerformanceCounter.PerCameraDrawWrite.TotalDrawCalls++;
                }
            }

            shader.End();
            // Technique End
        }
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List<MyRender.MyRenderElement> elements, List<MyRender.MyRenderElement> transparentElements)
        {
            if (MyRender.CurrentRenderSetup.CallerID.HasValue && MyRender.CurrentRenderSetup.CallerID.Value == MyRenderCallerEnum.EnvironmentMap && this.EntityDithering > 0)
            {
                return;
            }

            Debug.Assert(m_isDataSet, "Data is not set, have you forgotten to send SetRenderEntityData message?");

            MyRenderModel currentModel = m_lods[0].Model;
            List<MyRenderMeshMaterial> currentMaterials = m_lods[0].MeshMaterials;

            var volume = WorldVolume;
            var lodIndex = 0;

            if (lodTypeEnum == MyLodTypeEnum.LOD_NEAR)
            {

            }
            else
                if (lodTypeEnum == MyLodTypeEnum.LOD0)
                {
                    if (m_lods.Count > 1)
                    {
                        var distance = MyUtils.GetSmallestDistanceToSphereAlwaysPositive(ref MyRenderCamera.Position, ref volume);

                        if (distance > MyRenderCamera.FAR_PLANE_DISTANCE)
                            return;

                        for (int i = 1; i < m_lods.Count; i++)
                        {
                            var lod = m_lods[i];
                            if (distance < lod.Distance)
                                break;

                            currentModel = lod.Model;
                            currentMaterials = lod.MeshMaterials;
                            lodIndex = i;
                        }
                    }
                }
                else
                    return; //nothing to render in LOD1

            if (currentModel == null)
                return;

            int instanceCount = m_instanceBuffer != null ? m_instanceCount : 1;

            MyRender.ModelTrianglesCountStats += currentModel.GetTrianglesCount() * instanceCount;

            MyPerformanceCounter.PerCameraDrawWrite.EntitiesRendered++;

            CollectRenderElements(elements, transparentElements, currentModel, currentMaterials, lodIndex);
        }
Example #31
0
 public void Add(MyLodTypeEnum lod, MyRender.MyRenderElement renderElement)
 {
     if (renderElement.DrawTechnique == MyMeshDrawTechnique.VOXEL_MAP)
     {
         AddVoxel(lod, renderElement);
     }
     else
     {
         AddModel(lod, renderElement);
     }
 }
Example #32
0
        public static void AddToQueue(
            MyLodTypeEnum type,
            MyVoxelMap voxelMap,
            MyVoxelGeometry.CellData cache,
            int voxelStartX, int voxelStartY, int voxelStartZ)
        {
            Debug.Assert(Thread.CurrentThread == MySandboxGame.Static.UpdateThread, "Only update thread should queue voxel precalc.");
            MyVoxelPrecalcTaskItem a = new MyVoxelPrecalcTaskItem(type, voxelMap, cache, new Vector3I(voxelStartX, voxelStartY, voxelStartZ));

            Tasks.Enqueue(a);
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = (MyEffectModelsDNS)MyRender.GetEffect(MyEffects.ModelDNS);
            SetupBaseEffect(shader, setup, lodType);

            MyStateObjects.Static_Decals_BlendState.Apply();
            MyStateObjects.DepthStencil_TestFarObject_DepthReadOnly.Apply();

            shader.BeginBlended();
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
 public void AddVoxel(MyLodTypeEnum lod, MyRender.MyRenderElement renderElement)
 {
     var matDict = Voxels[GetVoxelIndex(lod, renderElement.VoxelBatch.Type)];
     List<MyRender.MyRenderElement> elements;
     if (!matDict.Voxels.TryGetValue(renderElement.VoxelBatch.MaterialId, out elements))
     {
         elements = new List<MyRender.MyRenderElement>(20);
         matDict.Voxels[renderElement.VoxelBatch.MaterialId] = elements;
     }
     matDict.RenderElementCount++;
     elements.Add(renderElement);
 }
Example #35
0
        /// <summary>
        /// Voxel cell hash computation using id, coordinates and LOD. These values are all merged into single
        /// 64 bit number in order [LOD|MapId|Coord.X|Coord.Y|Coord.Z]. There should be constaints that ensure
        /// everything fits in required number of bits. See MyVoxelConstants for number of bits used in hash
        /// computation.
        /// </summary>
        /// <param name="cellHashType">Must be 0 or 1 when cast to integer.</param>
        public Int64 GetCellHashCode(int voxelMapId, ref Vector3I cellCoord, MyLodTypeEnum cellHashType)
        {
            Debug.Assert((uint)cellHashType <= 1, "Cell hash type is only allowed to be 0 or 1 (has to fit inside single bit).");

            Int64 hash = (Int64)cellHashType;

            hash = (hash << MyVoxelConstants.HASH_VOXEL_ID_BITS) + voxelMapId;
            hash = (hash << MyVoxelConstants.HASH_VOXEL_CELL_COORD_BITS) + cellCoord.X;
            hash = (hash << MyVoxelConstants.HASH_VOXEL_CELL_COORD_BITS) + cellCoord.Y;
            hash = (hash << MyVoxelConstants.HASH_VOXEL_CELL_COORD_BITS) + cellCoord.Z;
            return(hash);
        }
Example #36
0
        private static void DrawRenderElementsAlternative(MySortedElements sortedElements, MyLodTypeEnum lod, out int ibChangesStats)
        {
            m_currentLodDrawPass = lod;

            ibChangesStats = 0;

            BlendState.Opaque.Apply(); //set by default, blend elements are at the end.

            DrawVoxels(sortedElements, lod, MyRenderVoxelBatchType.SINGLE_MATERIAL, ref ibChangesStats);
            DrawVoxels(sortedElements, lod, MyRenderVoxelBatchType.MULTI_MATERIAL, ref ibChangesStats);
            DrawModels(sortedElements, lod, ref ibChangesStats);
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = MyRender.GetEffect(MyEffects.VoxelsMRT) as MyEffectVoxels;
            SetupBaseEffect(shader, setup, lodType);

            {
                shader.Apply();
            }
            m_currentLod = lodType;
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
        private static void DrawRenderElementsAlternative(MySortedElements sortedElements, MyLodTypeEnum lod, out int ibChangesStats)
        {
            m_currentLodDrawPass = lod;

            ibChangesStats = 0;

            BlendState.Opaque.Apply(); //set by default, blend elements are at the end.

            DrawVoxels(sortedElements, lod, MyRenderVoxelBatchType.SINGLE_MATERIAL, ref ibChangesStats);
            DrawVoxels(sortedElements, lod, MyRenderVoxelBatchType.MULTI_MATERIAL, ref ibChangesStats);
            DrawModels(sortedElements, lod, ref ibChangesStats);
        }
        //  Update cell - immediately after it was last time used. It will get higher priority and won't be flushed when AddCell() called next time.
        public static void UpdateCell(int voxelMapId, ref MyMwcVector3Int cellCoord, MyLodTypeEnum cellHashType)
        {
            Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, cellHashType);
            LinkedListNode <MyVoxelCacheCellRender> ret = m_cellsByCoordinate[key];

            //  Move it to the end of priority linked list
            lock (m_priorityLocker)
            {
                m_priority.Remove(ret);
                m_priority.AddLast(ret);
            }
        }
Example #40
0
        private static void DrawRenderElementsAlternative(MyLodTypeEnum lod, bool applyStencil, out int ibChangesStats)
        {
            m_currentLodDrawPass = lod;

            ibChangesStats = 0;

            BlendState.Opaque.Apply(); //set by default, blend elements are at the end.
            DrawVoxels(lod, MyVoxelCacheCellRenderBatchType.SINGLE_MATERIAL, ref ibChangesStats);
            DrawVoxels(lod, MyVoxelCacheCellRenderBatchType.MULTI_MATERIAL, ref ibChangesStats);

            BlendState.Opaque.Apply(); //set by default, blend elements are at the end.
            DrawModels(lod, ref ibChangesStats);
        }
        private static void DrawRenderElementsAlternative(MyLodTypeEnum lod, bool applyStencil, out int ibChangesStats)
        {
            m_currentLodDrawPass = lod;

            ibChangesStats = 0;

            BlendState.Opaque.Apply(); //set by default, blend elements are at the end. 
            DrawVoxels(lod, MyVoxelCacheCellRenderBatchType.SINGLE_MATERIAL, ref ibChangesStats);
            DrawVoxels(lod, MyVoxelCacheCellRenderBatchType.MULTI_MATERIAL, ref ibChangesStats);

            BlendState.Opaque.Apply(); //set by default, blend elements are at the end. 
            DrawModels(lod, ref ibChangesStats);
        }
Example #42
0
        public void AddVoxel(MyLodTypeEnum lod, MyRender.MyRenderElement renderElement)
        {
            var matDict = Voxels[GetVoxelIndex(lod, renderElement.VoxelBatch.Type)];
            List <MyRender.MyRenderElement> elements;

            if (!matDict.Voxels.TryGetValue(renderElement.VoxelBatch.MaterialId, out elements))
            {
                elements = new List <MyRender.MyRenderElement>(20);
                matDict.Voxels[renderElement.VoxelBatch.MaterialId] = elements;
            }
            matDict.RenderElementCount++;
            elements.Add(renderElement);
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = MyRender.GetEffect(MyEffects.VoxelsMRT) as MyEffectVoxels;

            SetupBaseEffect(shader, setup, lodType);

            {
                shader.ApplyMultimaterial();
            }
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            m_currentLod = lodType;
            return(shader);
        }
Example #44
0
 public static void SetupBaseEffect(MyEffectBase effect, MyLodTypeEnum lodType, float fogMultiplierMult = 1.0f)
 {
     if (MyRender.Settings.EnableFog)
     {
         effect.SetFogDistanceFar(MyRender.FogProperties.FogFar);
         effect.SetFogDistanceNear(MyRender.FogProperties.FogNear);
         effect.SetFogColor(MyRender.FogProperties.FogColor);
         effect.SetFogMultiplier(MyRender.FogProperties.FogMultiplier * fogMultiplierMult);
         effect.SetFogBacklightMultiplier(MyRender.FogProperties.FogBacklightMultiplier);
     }
     else
     {
         effect.SetFogMultiplier(0);
     }
 }
Example #45
0
        public static void AddToQueue(
            MyLodTypeEnum type,
            MyVoxelMap voxelMap,
            MyVoxelCacheCellData cache,
            int voxelStartX, int voxelStartY, int voxelStartZ)
        {
            MyVoxelPrecalcTaskItem a = new MyVoxelPrecalcTaskItem();

            a.Type       = type;
            a.VoxelMap   = voxelMap;
            a.Cache      = cache;
            a.VoxelStart = new MyMwcVector3Int(voxelStartX, voxelStartY, voxelStartZ);

            //  IMPORTANT: Don't need to lock Tasks, because at this point no other thread should access it.
            Tasks.Enqueue(a);
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = MyRender.GetEffect(MyEffects.VoxelsMRT) as MyEffectVoxels;
            SetupBaseEffect(shader, setup, lodType);

            if (lodType == MyLodTypeEnum.LOD_BACKGROUND)
            {
                shader.ApplyFar(MyRenderConstants.RenderQualityProfile.VoxelsRenderTechnique);
            }
            else
            {
                shader.Apply();
            }
            m_currentLod = lodType;
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return shader;
        }
Example #47
0
        public override void GetRenderElementsForShadowmap(MyLodTypeEnum lodTypeEnum, List <MyRender.MyRenderElement> renderElements, List <MyRender.MyRenderElement> transparentRenderElements)
        {
            if (MyRender.Settings.SkipVoxels || lodTypeEnum == MyLodTypeEnum.LOD_BACKGROUND)
            {
                return;
            }


            double distance = (MyRenderCamera.Position - m_aabb.Center).Length();

            if (distance > MyRenderCamera.FAR_PLANE_DISTANCE)
            {
                return;
            }

            base.GetRenderElementsForShadowmap(lodTypeEnum, renderElements, transparentRenderElements);
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = MyRender.GetEffect(MyEffects.VoxelsMRT) as MyEffectVoxels;
            SetupBaseEffect(shader, setup, lodType);

            if (lodType == MyLodTypeEnum.LOD_BACKGROUND)
            {
                shader.ApplyMultimaterialFar();
            }
            else
            {
                shader.ApplyMultimaterial();
            }
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            m_currentLod = lodType;
            return shader;
        }
Example #49
0
 private static bool CellAffectsTriangles(IMyStorage storage, MyLodTypeEnum lod, ref Vector3I cellCoord)
 {
     Profiler.Begin("CellAffectsTriangles");
     try
     {
         //  Fix max cell coordinates so they don't fall from voxelmap
         var rangeMin = cellCoord << MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS_BITS;
         rangeMin += MyVoxelPrecalc.AffectedRangeOffset;
         var rangeMax = Vector3I.Min(rangeMin + (MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS + MyVoxelPrecalc.AffectedRangeSizeChange - 1), storage.Size - 1);
         var type     = storage.GetRangeType(GetLodIndex(lod), ref rangeMin, ref rangeMax);
         return(type == MyVoxelRangeType.MIXED);
     }
     finally
     {
         Profiler.End();
     }
 }
        protected void SetupBaseEffect(MyEffectBase shader, MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            MyRenderCamera.SetupBaseEffect(shader, lodType,  setup.FogMultiplierMult);

            if (lodType == MyLodTypeEnum.LOD_NEAR)
            {
                shader.SetProjectionMatrix(ref MyRenderCamera.ProjectionMatrixForNearObjects);
            }
            else
            {
                shader.SetProjectionMatrix(ref MyRenderCamera.ProjectionMatrix);
            }

            shader.SetViewMatrix(ref MyRenderCamera.ViewMatrixAtZero);

            var rasterizerState = MyRender.Settings.Wireframe ? WireframeRasterizerState : SolidRasterizerState;
            rasterizerState.Apply();
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = MyRender.GetEffect(MyEffects.VoxelsMRT) as MyEffectVoxels;

            SetupBaseEffect(shader, setup, lodType);

            if (lodType == MyLodTypeEnum.LOD_BACKGROUND)
            {
                shader.ApplyMultimaterialFar(MyRenderConstants.RenderQualityProfile.VoxelsRenderTechnique);
            }
            else
            {
                shader.ApplyMultimaterial();
            }
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            m_currentLod = lodType;
            return(shader);
        }
        public override MyEffectBase PrepareAndBeginShader(MyRender.MyRenderSetup setup, MyLodTypeEnum lodType)
        {
            var shader = MyRender.GetEffect(MyEffects.VoxelsMRT) as MyEffectVoxels;

            SetupBaseEffect(shader, setup, lodType);

            if (lodType == MyLodTypeEnum.LOD_BACKGROUND)
            {
                shader.ApplyFar();
            }
            else
            {
                shader.Apply();
            }
            m_currentLod = lodType;
            shader.Begin(0, SharpDX.Direct3D9.FX.None);
            return(shader);
        }
        //  Remove cell - after voxels were changed, etc.
        public static void RemoveCell(MyVoxelMap voxelMap, ref Vector3I cellCoord, MyLodTypeEnum cellHashType)
        {
            Int64 key = MySession.Static.VoxelMaps.GetCellHashCode(voxelMap.VoxelMapId, ref cellCoord, cellHashType);

            //  If cell is in cache, we remove it from dictionary and move it to the beginning of priority linked list
            LinkedListNode <MyVoxelCacheCellRender> ret;

            if (m_cellsByCoordinate.TryGetValue(key, out ret) == true)
            {
                m_cellsByCoordinate.Remove(key);

                ret.Value.Reset();

                //  Move it to the beginning of priority linked list
                Debug.Assert(Thread.CurrentThread == MySandboxGame.Static.UpdateThread);
                m_priority.Remove(ret);
                m_priority.AddFirst(ret);

                VRageRender.MyRenderProxy.InvalidateRenderVoxelCell(voxelMap.GetRenderObjectID(ref cellCoord), (VRageRender.MyLodTypeEnum)(int) cellHashType);
            }
        }
Example #54
0
        internal CellData GetCellLater(MyLodTypeEnum lod, ref Vector3I cellCoord)
        {
            Int64    key = MySession.Static.VoxelMaps.GetCellHashCode(m_voxelMap.VoxelMapId, ref cellCoord, lod);
            CellData cachedCell;

            if (!m_cellsByCoordinate.TryGetValue(key, out cachedCell))
            {
                if (CellAffectsTriangles(m_voxelMap.Storage, lod, ref cellCoord))
                {
                    cachedCell = CellPool.AllocateOrCreate();
                    m_cellsByCoordinate.Add(key, cachedCell);

                    MyVoxelPrecalc.AddToQueue(MyLodTypeEnum.LOD0, m_voxelMap, cachedCell,
                                              cellCoord.X * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS,
                                              cellCoord.Y * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS,
                                              cellCoord.Z * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS);
                }
            }

            return(cachedCell);
        }
Example #55
0
        public void AddModel(MyLodTypeEnum lod, MyRender.MyRenderElement renderElement)
        {
            var matDict = Models[GetModelIndex(lod, renderElement.DrawTechnique)];
            ModelMaterialSet vbDict;
            if (!matDict.Models.TryGetValue(renderElement.Material, out vbDict))
            {
                vbDict = new ModelMaterialSet();
                matDict.Models[renderElement.Material] = vbDict;
            }

            List<MyRender.MyRenderElement> elements;
            if (!vbDict.Models.TryGetValue(renderElement.VertexBuffer, out elements))
            {
                elements = new List<MyRender.MyRenderElement>(20);
                vbDict.Models[renderElement.VertexBuffer] = elements;
            }

            matDict.RenderElementCount++;
            vbDict.RenderElementCount++;

            elements.Add(renderElement);
        }
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List<MyRender.MyRenderElement> elements, List<MyRender.MyRenderElement> transparentElements)
        {
            foreach (var mesh in m_meshes)
            {
                mesh.Material.PreloadTexture(LoadingMode.Background);

                MyRender.MyRenderElement renderElement;
                MyRender.AllocateRenderElement(out renderElement);

                if (!MyRender.IsRenderOverloaded)
                {
                    //renderElement.DebugName = entity.Name;
                    renderElement.RenderObject = this;

                    renderElement.VertexBuffer = m_vertexBuffer;
                    renderElement.IndexBuffer = m_indexBuffer;
                    renderElement.VertexCount = m_vertexCount;
                    renderElement.VertexDeclaration = m_vertexDeclaration;
                    renderElement.VertexStride = m_vertexStride;
                    renderElement.InstanceBuffer = null;

                    renderElement.IndexStart = mesh.IndexStart;
                    renderElement.TriCount = mesh.TriangleCount;

                    renderElement.WorldMatrixForDraw = GetWorldMatrixForDraw();
                    renderElement.WorldMatrix = WorldMatrix;

                    renderElement.Material = mesh.Material;
                    renderElement.DrawTechnique = mesh.Material.DrawTechnique;
                    renderElement.Color = new Vector3(1, 1, 1);
                    renderElement.Dithering = 0;

                    Debug.Assert(renderElement.VertexBuffer != null, "Vertex buffer cannot be null!");
                    Debug.Assert(renderElement.IndexBuffer != null, "Index buffer cannot be null!");

                    elements.Add(renderElement);
                }
            }
        }
        public override void GetRenderElements(MyLodTypeEnum lodTypeEnum, List<MyRender.MyRenderElement> elements, List<MyRender.MyRenderElement> transparentElements)
        {
            if (MyRender.Settings.SkipVoxels)
                return;

            Debug.Assert(lodTypeEnum == MyLodTypeEnum.LOD0 || lodTypeEnum == MyLodTypeEnum.LOD_BACKGROUND);

            double distanceMin = (MyRenderCamera.Position - m_aabb.Min).Length();
            double distanceMax = (MyRenderCamera.Position - m_aabb.Max).Length();

            distanceMin = Math.Min(distanceMin, distanceMax);
            distanceMax = Math.Max(distanceMin, distanceMax);

            if (distanceMin > MyRenderCamera.FAR_PLANE_DISTANCE && lodTypeEnum == MyLodTypeEnum.LOD0)
            {
                return;
            }
            if (distanceMax < MyRenderCamera.NEAR_PLANE_FOR_BACKGROUND && lodTypeEnum == MyLodTypeEnum.LOD_BACKGROUND)
            {
                return;
            }
            base.GetRenderElements(lodTypeEnum, elements, transparentElements);
        }
        private static void DrawModels(MySortedElements sortedElements, MyLodTypeEnum lod, ref int ibChangesStats)
        {          
            for (int i = 0; i < MySortedElements.DrawTechniqueCount; i++)
            {
                var technique = (MyMeshDrawTechnique)i;
                int index = sortedElements.GetModelIndex(lod, technique);
                var matDict = sortedElements.Models[index];

                if (matDict.RenderElementCount == 0)
                    continue;

                // Technique start
                var tech = GetTechnique(technique);
                var shader = tech.PrepareAndBeginShader(m_currentSetup, m_currentLodDrawPass);
               
                RasterizerState currentRasterizer = RasterizerState.Current;
                bool doubleSided = tech.GetType() == typeof(MyDrawTechniqueAlphaMasked) || tech.GetType() == typeof(MyDrawTechniqueMeshInstancedGenericMasked);              
                if (doubleSided)
                {
                    RasterizerState alphaMaskedRasterizer = new RasterizerState { CullMode = Cull.None };
                    alphaMaskedRasterizer.Apply();
                }

                MyPerformanceCounter.PerCameraDrawWrite.TechniqueChanges[(int)lod]++;

                foreach (var mat in matDict.Models)
                {
                    if (mat.Value.RenderElementCount == 0)
                        continue;

                    // Setup material
                    tech.SetupMaterial(shader, mat.Key);
                    MyPerformanceCounter.PerCameraDrawWrite.MaterialChanges[(int)lod]++;

#if !ATI_INSTANCES

                    foreach (var vb in mat.Value.Models)
                    {
                        // Set vb
                        var firstElement = vb.Value.FirstOrDefault();
                        if (firstElement == null)
                            continue;

                        GraphicsDevice.Indices = firstElement.IndexBuffer;
                        GraphicsDevice.SetStreamSource(0, firstElement.VertexBuffer, 0, firstElement.VertexStride);
                        GraphicsDevice.VertexDeclaration = firstElement.VertexDeclaration;
                        MyPerformanceCounter.PerCameraDrawWrite.VertexBufferChanges[(int)lod]++;
                        ibChangesStats++;

                        MyRenderObject lastRenderObject = null;
                        int[] lastBonesSet = null;

                        foreach (var renderElement in vb.Value)
                        {
                            if (renderElement.InstanceBuffer == null)
                            {
                                MyRender.GraphicsDevice.ResetStreamSourceFrequency(0);
                                MyRender.GraphicsDevice.ResetStreamSourceFrequency(1);
                            }
                            else
                            {
                                GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;
                                GraphicsDevice.SetStreamSourceFrequency(0, renderElement.InstanceCount, StreamSource.IndexedData);
                                GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);

                                GraphicsDevice.SetStreamSourceFrequency(1, 1, StreamSource.InstanceData);
                                GraphicsDevice.SetStreamSource(1, renderElement.InstanceBuffer, renderElement.InstanceStride * renderElement.InstanceStart, renderElement.InstanceStride);
                            }
                            
                            if (lastRenderObject != renderElement.RenderObject || lastBonesSet != renderElement.BonesUsed)
                            {
                                lastRenderObject = renderElement.RenderObject;
                                lastBonesSet = renderElement.BonesUsed;
                                MyPerformanceCounter.PerCameraDrawWrite.EntityChanges[(int)lod]++;
                                tech.SetupEntity(shader, renderElement);
                            
                                if(doubleSided == false)
                                {
                                    currentRasterizer.Apply();
                                    BlendState.Opaque.Apply();
                                }

                                shader.D3DEffect.CommitChanges();
                            }
                     
                            GraphicsDevice.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);
                            MyPerformanceCounter.PerCameraDrawWrite.TotalDrawCalls++;
                        }
#else

                    foreach (var vb in mat.Value.Models)
                    {
                        // Set vb
                        var firstElement = vb.Value.FirstOrDefault();
                        if (firstElement == null)
                            continue;

                        GraphicsDevice.Indices = firstElement.IndexBuffer;
                        GraphicsDevice.SetStreamSource(0, firstElement.VertexBuffer, 0, firstElement.VertexStride);
                        GraphicsDevice.VertexDeclaration = firstElement.VertexDeclaration;
                        MyPerformanceCounter.PerCameraDrawWrite.VertexBufferChanges[(int)lod]++;
                        ibChangesStats++;

                        MyRenderObject lastRenderObject = null;

                        foreach (var renderElement in vb.Value)
                        {
                            if (renderElement.InstanceBuffer == null)
                            {
                                MyRender.GraphicsDevice.ResetStreamSourceFrequency(0);
                                MyRender.GraphicsDevice.ResetStreamSourceFrequency(1);
                            }
                            else
                            {
                                          GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;

                                int totalInstCount = renderElement.InstanceCount;
                                int maxbuff = 1024;
                                int offset = 0;

                                while (totalInstCount > 0)
                                {
                                    int count = totalInstCount >= maxbuff ? maxbuff : totalInstCount;
                                    totalInstCount -= count;

                                    GraphicsDevice.SetStreamSourceFrequency(0, count, StreamSource.IndexedData);
                                    GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);

                                    GraphicsDevice.SetStreamSourceFrequency(1, 1, StreamSource.InstanceData);
                                    GraphicsDevice.SetStreamSource(1, renderElement.InstanceBuffer, renderElement.InstanceStride * (renderElement.InstanceStart + offset), renderElement.InstanceStride);

                                    offset += count;

                                    if (lastRenderObject != renderElement.RenderObject)
                                    {
                                        lastRenderObject = renderElement.RenderObject;
                                        MyPerformanceCounter.PerCameraDrawWrite.EntityChanges[(int)lod]++;
                                        tech.SetupEntity(shader, renderElement);
                                        shader.D3DEffect.CommitChanges();
                                    }

                                    GraphicsDevice.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);

                                    MyPerformanceCounter.PerCameraDrawWrite.TotalDrawCalls++;
                                }
                            }
                        
                        }
#endif
                    }
                }

                MyRender.GraphicsDevice.ResetStreamSourceFrequency(0);
                MyRender.GraphicsDevice.ResetStreamSourceFrequency(1);

                shader.End();
                // Technique End

                if (doubleSided)
                {
                    currentRasterizer.Apply();
                }
            }
        }
    }
        public override void GetRenderElementsForShadowmap(MyLodTypeEnum lodTypeEnum, List<MyRender.MyRenderElement> renderElements, List<MyRender.MyRenderElement> transparentRenderElements)
        {
            if (MyRender.Settings.SkipVoxels)
                return;


            //  Get non-empty render cells visible to the frustum and sort them by distance to camera
            MyRender.GetRenderProfiler().StartProfilingBlock("GetElements from MyVoxelMap");

            foreach (MyRenderVoxelBatch batch in m_batches)
            {
                if (batch.IndexCount == 0)
                    continue;

                MyRender.MyRenderElement renderElement;
                MyRender.AllocateRenderElement(out renderElement);

                if (!MyRender.IsRenderOverloaded)
                {
                    SetupRenderElement(batch, renderElement);
                    renderElement.Dithering = 0;

                    renderElements.Add(renderElement);
                }
            }

            MyRender.GetRenderProfiler().EndProfilingBlock();
        }
        private static void DrawVoxels(MySortedElements sortedElements, MyLodTypeEnum lod, MyRenderVoxelBatchType batchType, ref int ibChangesStats)
        {
            int index = sortedElements.GetVoxelIndex(lod, batchType);
            var matDict = sortedElements.Voxels[index];

            if (matDict.RenderElementCount == 0)
                return;

            var tech = GetTechnique(batchType == MyRenderVoxelBatchType.SINGLE_MATERIAL ? MyMeshDrawTechnique.VOXEL_MAP_SINGLE : MyMeshDrawTechnique.VOXEL_MAP_MULTI);
           
            var shader = (MyEffectVoxels)tech.PrepareAndBeginShader(m_currentSetup, lod);         
            MyPerformanceCounter.PerCameraDrawWrite.TechniqueChanges[(int)lod]++;
           
            if (lod == MyLodTypeEnum.LOD_BACKGROUND)
            {
                shader.SetAmbientMinimumAndIntensity(new Vector4(AmbientColor * AmbientMultiplier, EnvAmbientIntensity));
                shader.SetSunDirection(m_sun.Direction);
                shader.SetSunColorAndIntensity(new Vector3(m_sun.Color.X, m_sun.Color.Y, m_sun.Color.Z), m_sun.Intensity);
                shader.SetBacklightColorAndIntensity(new Vector3(m_sun.BackColor.X, m_sun.BackColor.Y, m_sun.BackColor.Z), m_sun.BackIntensity);
                var postProcess = MyRender.GetPostProcess(MyPostProcessEnum.VolumetricFog) as MyPostProcessVolumetricFog;
                shader.EnableFog(postProcess.Enabled);
                shader.SetSunSpecularColor(m_sun.SpecularColor);
            }

            foreach (var mat in matDict.Voxels)
            {
                var firstElement = mat.Value.FirstOrDefault();
                if (firstElement == null)
                    continue;

               
                // Setup material
                tech.SetupVoxelMaterial(shader, firstElement.VoxelBatch);
                MyPerformanceCounter.PerCameraDrawWrite.MaterialChanges[(int)lod]++;

                MyRenderObject lastRenderObject = null;
                VertexBuffer lastVertexBuffer = null;

                foreach (var renderElement in mat.Value)
                {
                    if (!object.ReferenceEquals(lastVertexBuffer, renderElement.VertexBuffer))
                    {
                        lastVertexBuffer = renderElement.VertexBuffer;
                        GraphicsDevice.Indices = renderElement.IndexBuffer;
                        GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);
                        GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;
                        MyPerformanceCounter.PerCameraDrawWrite.VertexBufferChanges[(int)lod]++;
                        ibChangesStats++;
                    }

                    if (lastRenderObject != renderElement.RenderObject)
                    {
                        lastRenderObject = renderElement.RenderObject;
                        MyPerformanceCounter.PerCameraDrawWrite.EntityChanges[(int)lod]++;
                        tech.SetupEntity(shader, renderElement);                     
                        shader.D3DEffect.CommitChanges();
                    }

                    GraphicsDevice.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);
                    MyPerformanceCounter.PerCameraDrawWrite.TotalDrawCalls++;
                }
            }

            shader.End();
            // Technique End
        }