private static void DrawShadowsForElements(MyEffectShadowMap effect, bool relativeCamera, List <MyRender.MyRenderElement> elements, bool voxelShadows, int perfCounterIndex)
        {
            effect.Begin();

            long lastVertexBuffer = 0;

            for (int i = 0; i < elements.Count; i++)
            {
                MyRender.MyRenderElement renderElement = elements[i];

                if ((renderElement.Entity is MyVoxelMap) != voxelShadows ||
                    renderElement.VertexBuffer.IsDisposed ||
                    renderElement.IndexBuffer.IsDisposed)
                {
                    continue;
                }

                long currentVertexBuffer = renderElement.VertexBuffer.GetHashCode();
                if (lastVertexBuffer != currentVertexBuffer)
                {
                    lastVertexBuffer = currentVertexBuffer;

                    MyMinerGame.Static.GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);
                    MyMinerGame.Static.GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;
                    MyMinerGame.Static.GraphicsDevice.Indices           = renderElement.IndexBuffer;

                    System.Diagnostics.Debug.Assert(renderElement.IndexBuffer != null);
                }

                effect.SetWorldMatrix(relativeCamera ? renderElement.WorldMatrixForDraw : renderElement.WorldMatrix);

                MyPerformanceCounter.PerCameraDraw.ShadowDrawCalls[perfCounterIndex]++;

                effect.D3DEffect.CommitChanges();
                MyMinerGame.Static.GraphicsDevice.DrawIndexedPrimitive(SharpDX.Direct3D9.PrimitiveType.TriangleList, 0, 0, renderElement.VertexCount, renderElement.IndexStart, renderElement.TriCount);

                MyPerformanceCounter.PerCameraDraw.TotalDrawCalls++;
            }

            effect.End();
        }
        private static void DrawShadowsForElements(MyEffectShadowMap effect, bool relativeCamera, Vector3D cameraPos, List <MyRender.MyRenderElement> elements, bool voxelShadows, bool skinnedShadows, bool instancedShadows, bool genericInstancedShadows, int perfCounterIndex, bool ditheredShadows)
        {
            effect.Begin();

            long lastVertexBuffer = 0;

            for (int i = 0; i < elements.Count; i++)
            {
                MyRender.MyRenderElement renderElement = elements[i];

                if ((renderElement.RenderObject is MyRenderVoxelCell) != voxelShadows ||
                    renderElement.VertexBuffer.IsDisposed ||
                    renderElement.IndexBuffer.IsDisposed)
                {
                    continue;
                }

                if ((renderElement.RenderObject is MyRenderCharacter) != skinnedShadows)
                {
                    continue;
                }

                //if ((renderElement.InstanceBuffer != null) != instancedShadows)
                //    continue;

                if ((renderElement.InstanceBuffer != null && renderElement.InstanceStride == 64) != instancedShadows)
                {
                    continue;
                }

                if ((renderElement.InstanceBuffer != null && renderElement.InstanceStride == 32) != genericInstancedShadows)
                {
                    continue;
                }

                long currentVertexBuffer = renderElement.VertexBuffer.GetHashCode();
                if ((lastVertexBuffer != currentVertexBuffer) || instancedShadows || genericInstancedShadows)
                {
                    lastVertexBuffer = currentVertexBuffer;

                    MyRender.GraphicsDevice.Indices           = renderElement.IndexBuffer;
                    MyRender.GraphicsDevice.VertexDeclaration = renderElement.VertexDeclaration;

                    if (renderElement.InstanceBuffer == null)
                    {
                        MyRender.GraphicsDevice.ResetStreamSourceFrequency(0);
                        MyRender.GraphicsDevice.ResetStreamSourceFrequency(1);
                        MyRender.GraphicsDevice.SetStreamSource(1, null, 0, 0);
                    }
                    else
                    {
                        MyRender.GraphicsDevice.SetStreamSourceFrequency(0, renderElement.InstanceCount, StreamSource.IndexedData);
                        MyRender.GraphicsDevice.SetStreamSourceFrequency(1, 1, StreamSource.InstanceData);
                        MyRender.GraphicsDevice.SetStreamSource(1, renderElement.InstanceBuffer, renderElement.InstanceStart * renderElement.InstanceStride, renderElement.InstanceStride);
                    }
                    MyRender.GraphicsDevice.SetStreamSource(0, renderElement.VertexBuffer, 0, renderElement.VertexStride);

                    System.Diagnostics.Debug.Assert(renderElement.IndexBuffer != null);
                }

                if (relativeCamera)
                {
                    MatrixD m = renderElement.WorldMatrix;
                    m.Translation -= cameraPos;
                    effect.SetWorldMatrix((Matrix)m);
                }
                else
                {
                    effect.SetWorldMatrix((Matrix)renderElement.WorldMatrix);
                }

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

                var skinMatrices = skinnedShadows ? ((MyRenderCharacter)renderElement.RenderObject).SkinMatrices : null;
                if (skinMatrices != null)
                {
                    var bonesUsed = renderElement.BonesUsed;
                    if (bonesUsed == null)
                    {
                        for (int b = 0; b < Math.Min(skinMatrices.Length, MyRenderConstants.MAX_SHADER_BONES); b++)
                        {
                            m_bonesBuffer[b] = skinMatrices[b];
                        }
                    }
                    else
                    {
                        for (int b = 0; b < bonesUsed.Length; b++)
                        {
                            m_bonesBuffer[b] = skinMatrices[bonesUsed[b]];
                        }
                    }
                    effect.SetBones(m_bonesBuffer);
                }

                effect.SetDithering(ditheredShadows ? renderElement.Dithering : 0);

                MyPerformanceCounter.PerCameraDrawWrite.ShadowDrawCalls[perfCounterIndex]++;

                effect.D3DEffect.CommitChanges();

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

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

            effect.End();
        }