<commit_before>/*
The MIT License (MIT)
Copyright (c) 2018 Helix Toolkit contributors
*/
using System;
using SharpDX.Direct3D11;
using System.Runtime.CompilerServices;
#if !NETFX_CORE
namespace HelixToolkit.Wpf.SharpDX
#else
#if CORE
namespace HelixToolkit.SharpDX.Core
#else
namespace HelixToolkit.UWP
#endif
#endif
{
    namespace Core
    {
        using Utilities;
        using Render;
        using Shaders;


        /// <summary>
        /// 
        /// </summary>
        public abstract class GeometryRenderCore : RenderCore, IGeometryRenderCore
        {
            private RasterizerStateProxy rasterState = null;
            /// <summary>
            /// 
            /// </summary>
            public RasterizerStateProxy RasterState
            {
                get
                {
                    return rasterState;
                }
            }

            private RasterizerStateProxy invertCullModeState = null;
            public RasterizerStateProxy InvertCullModeState
            {
                get
                {
                    return invertCullModeState;
                }
            }

            private IElementsBufferModel instanceBuffer = MatrixInstanceBufferModel.Empty;
            /// <summary>
            /// 
            /// </summary>
            public IElementsBufferModel InstanceBuffer
            {
                set
                {
                    var old = instanceBuffer;
                    if (SetAffectsCanRenderFlag(ref instanceBuffer, value))
                    {
                        if (old != null)
                        {
                            old.ElementChanged -= OnElementChanged;
                        }
                        if (instanceBuffer != null)
                        {
                            instanceBuffer.ElementChanged += OnElementChanged;
                        }
                        else
                        {
                            instanceBuffer = MatrixInstanceBufferModel.Empty;
                        }
                    }
                }
                get
                {
                    return instanceBuffer;
                }
            }

            private IAttachableBufferModel geometryBuffer;
            /// <summary>
            /// 
            /// </summary>
            public IAttachableBufferModel GeometryBuffer
            {
                set
                {
                    if (SetAffectsCanRenderFlag(ref geometryBuffer, value))
                    {
                        OnGeometryBufferChanged(value);
                    }
                }
                get
                {
                    return geometryBuffer;
                }
            }

            private RasterizerStateDescription rasterDescription = new RasterizerStateDescription()
            {
                FillMode = FillMode.Solid,
                CullMode = CullMode.None,
            };
            /// <summary>
            /// 
            /// </summary>
            public RasterizerStateDescription RasterDescription
            {
                set
                {
                    if (SetAffectsRender(ref rasterDescription, value) && IsAttached)
                    {
                        CreateRasterState(value, false);
                    }
                }
                get
                {
                    return rasterDescription;
                }
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="GeometryRenderCore"/> class.
            /// </summary>
            public GeometryRenderCore() : base(RenderType.Opaque) { }
            /// <summary>
            /// Initializes a new instance of the <see cref="GeometryRenderCore"/> class.
            /// </summary>
            /// <param name="renderType">Type of the render.</param>
            public GeometryRenderCore(RenderType renderType) : base(renderType) { }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="description"></param>
            /// <param name="force"></param>
            /// <returns></returns>
            protected virtual bool CreateRasterState(RasterizerStateDescription description, bool force)
            {
                var newRasterState = EffectTechnique.EffectsManager.StateManager.Register(description);
                var invCull = description;
                if (description.CullMode != CullMode.None)
                {
                    invCull.CullMode = description.CullMode == CullMode.Back ? CullMode.Front : CullMode.Back;
                }
                var newInvertCullModeState = EffectTechnique.EffectsManager.StateManager.Register(invCull);
                RemoveAndDispose(ref rasterState);
                RemoveAndDispose(ref invertCullModeState);
                rasterState = newRasterState;
                invertCullModeState = newInvertCullModeState;
                return true;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="technique"></param>
            /// <returns></returns>
            protected override bool OnAttach(IRenderTechnique technique)
            {
                CreateRasterState(rasterDescription, true);
                return true;
            }

            protected override void OnDetach()
            {
                RemoveAndDispose(ref rasterState);
                RemoveAndDispose(ref invertCullModeState);
            }
            /// <summary>
            /// Called when [geometry buffer changed].
            /// </summary>
            /// <param name="buffer">The buffer.</param>
            protected virtual void OnGeometryBufferChanged(IAttachableBufferModel buffer)
            {
            }

            /// <summary>
            /// Set all necessary states and buffers
            /// </summary>
            /// <param name="context"></param>
            /// <param name="isInvertCullMode"></param>
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            protected void OnBindRasterState(DeviceContextProxy context, bool isInvertCullMode)
            {
                context.SetRasterState(!isInvertCullMode ? rasterState : invertCullModeState);
            }

            /// <summary>
            /// Attach vertex buffer routine
            /// </summary>
            /// <param name="context"></param>
            /// <param name="vertStartSlot"></param>
            protected virtual bool OnAttachBuffers(DeviceContextProxy context, ref int vertStartSlot)
            {
                if (GeometryBuffer.AttachBuffers(context, ref vertStartSlot, EffectTechnique.EffectsManager))
                {
                    InstanceBuffer.AttachBuffer(context, ref vertStartSlot);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            /// <summary>
            /// Called when [update can render flag].
            /// </summary>
            /// <returns></returns>
            protected override bool OnUpdateCanRenderFlag()
            {<commit_msg>Fix null reference exception in GeometryRenderCore.OnAttachBuffers()<commit_after>/*
The MIT License (MIT)
Copyright (c) 2018 Helix Toolkit contributors
*/
using System;
using SharpDX.Direct3D11;
using System.Runtime.CompilerServices;
#if !NETFX_CORE
namespace HelixToolkit.Wpf.SharpDX
#else
#if CORE
namespace HelixToolkit.SharpDX.Core
#else
namespace HelixToolkit.UWP
#endif
#endif
{
    namespace Core
    {
        using Utilities;
        using Render;
        using Shaders;


        /// <summary>
        /// 
        /// </summary>
        public abstract class GeometryRenderCore : RenderCore, IGeometryRenderCore
        {
            private RasterizerStateProxy rasterState = null;
            /// <summary>
            /// 
            /// </summary>
            public RasterizerStateProxy RasterState
            {
                get
                {
                    return rasterState;
                }
            }

            private RasterizerStateProxy invertCullModeState = null;
            public RasterizerStateProxy InvertCullModeState
            {
                get
                {
                    return invertCullModeState;
                }
            }

            private IElementsBufferModel instanceBuffer = MatrixInstanceBufferModel.Empty;
            /// <summary>
            /// 
            /// </summary>
            public IElementsBufferModel InstanceBuffer
            {
                set
                {
                    var old = instanceBuffer;
                    if (SetAffectsCanRenderFlag(ref instanceBuffer, value))
                    {
                        if (old != null)
                        {
                            old.ElementChanged -= OnElementChanged;
                        }
                        if (instanceBuffer != null)
                        {
                            instanceBuffer.ElementChanged += OnElementChanged;
                        }
                        else
                        {
                            instanceBuffer = MatrixInstanceBufferModel.Empty;
                        }
                    }
                }
                get
                {
                    return instanceBuffer;
                }
            }

            private IAttachableBufferModel geometryBuffer;
            /// <summary>
            /// 
            /// </summary>
            public IAttachableBufferModel GeometryBuffer
            {
                set
                {
                    if (SetAffectsCanRenderFlag(ref geometryBuffer, value))
                    {
                        OnGeometryBufferChanged(value);
                    }
                }
                get
                {
                    return geometryBuffer;
                }
            }

            private RasterizerStateDescription rasterDescription = new RasterizerStateDescription()
            {
                FillMode = FillMode.Solid,
                CullMode = CullMode.None,
            };
            /// <summary>
            /// 
            /// </summary>
            public RasterizerStateDescription RasterDescription
            {
                set
                {
                    if (SetAffectsRender(ref rasterDescription, value) && IsAttached)
                    {
                        CreateRasterState(value, false);
                    }
                }
                get
                {
                    return rasterDescription;
                }
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="GeometryRenderCore"/> class.
            /// </summary>
            public GeometryRenderCore() : base(RenderType.Opaque) { }
            /// <summary>
            /// Initializes a new instance of the <see cref="GeometryRenderCore"/> class.
            /// </summary>
            /// <param name="renderType">Type of the render.</param>
            public GeometryRenderCore(RenderType renderType) : base(renderType) { }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="description"></param>
            /// <param name="force"></param>
            /// <returns></returns>
            protected virtual bool CreateRasterState(RasterizerStateDescription description, bool force)
            {
                var newRasterState = EffectTechnique.EffectsManager.StateManager.Register(description);
                var invCull = description;
                if (description.CullMode != CullMode.None)
                {
                    invCull.CullMode = description.CullMode == CullMode.Back ? CullMode.Front : CullMode.Back;
                }
                var newInvertCullModeState = EffectTechnique.EffectsManager.StateManager.Register(invCull);
                RemoveAndDispose(ref rasterState);
                RemoveAndDispose(ref invertCullModeState);
                rasterState = newRasterState;
                invertCullModeState = newInvertCullModeState;
                return true;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="technique"></param>
            /// <returns></returns>
            protected override bool OnAttach(IRenderTechnique technique)
            {
                CreateRasterState(rasterDescription, true);
                return true;
            }

            protected override void OnDetach()
            {
                RemoveAndDispose(ref rasterState);
                RemoveAndDispose(ref invertCullModeState);
            }
            /// <summary>
            /// Called when [geometry buffer changed].
            /// </summary>
            /// <param name="buffer">The buffer.</param>
            protected virtual void OnGeometryBufferChanged(IAttachableBufferModel buffer)
            {
            }

            /// <summary>
            /// Set all necessary states and buffers
            /// </summary>
            /// <param name="context"></param>
            /// <param name="isInvertCullMode"></param>
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            protected void OnBindRasterState(DeviceContextProxy context, bool isInvertCullMode)
            {
                context.SetRasterState(!isInvertCullMode ? rasterState : invertCullModeState);
            }

            /// <summary>
            /// Attach vertex buffer routine
            /// </summary>
            /// <param name="context"></param>
            /// <param name="vertStartSlot"></param>
            protected virtual bool OnAttachBuffers(DeviceContextProxy context, ref int vertStartSlot)
            {
                if (GeometryBuffer != null && EffectTechnique != null &&
                    GeometryBuffer.AttachBuffers(context, ref vertStartSlot, EffectTechnique.EffectsManager))
                {
                    InstanceBuffer.AttachBuffer(context, ref vertStartSlot);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            /// <summary>
            /// Called when [update can render flag].
            /// </summary>
            /// <returns></returns>
            protected override bool OnUpdateCanRenderFlag()
            {
{'fraction_non_alphanumeric': 0.08363013698630137, 'fraction_numerical': 0.0009589041095890411, 'mean_word_length': 1.1424798239178284, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1919528', 'n_tokens_mistral': 3380, 'n_tokens_neox': 3131, 'n_words': 653}