示例#1
0
        /// <summary>
        /// Initializes this effect.
        /// </summary>
        /// <param name="dxDevice">parent DXDevice</param>
        protected override void OnInitializeResources(DXDevice dxDevice)
        {
            if (IsInitialized)
            {
                return; // Already initialized
            }
            parentDXDevice = dxDevice;

            _perFrameCameraConstantsBufferData = new PerFrameCameraConstantBuffer();
            _perFrameCameraConstantsBuffer     = parentDXDevice.CreateConstantBuffer(PerFrameCameraConstantBuffer.SizeInBytes, "SimpleDirectionalLightEffect_perFrameCameraConstantsBuffer");

            // Do not reset the PerFrameLightsConstantBuffer struct data - in case the SetFogData was called before this method
            //_perFrameLightsConstantsBufferData = new PerFrameLightsConstantBuffer();
            _perFrameLightsConstantsBuffer = parentDXDevice.CreateConstantBuffer(PerFrameLightsConstantBuffer.SizeInBytes, "SimpleDirectionalLightEffect_perFrameLightsConstantsBuffer");

            _perObjectConstantsBufferData = new PerObjectConstantBuffer();
            _perObjectConstantsBuffer     = parentDXDevice.CreateConstantBuffer(PerObjectConstantBuffer.SizeInBytes, "SimpleDirectionalLightEffect_perObjectConstantsBuffer");

            base.OnInitializeResources(dxDevice);
        }
        /// <summary>
        /// Initializes this effect.
        /// </summary>
        /// <param name="dxDevice">parent DXDevice</param>
        protected override void OnInitializeResources(DXDevice dxDevice)
        {
            if (IsInitialized)
            {
                return; // Already initialized
            }
            parentDXDevice = dxDevice;



            // The recommended way to create shaders is to use GetShaders method on EffectsManager (or GetVertexShader, GetPixelShader or other methods - see below).
            // In order for EffectsManager to get the shader resources, we need to RegisterShaderResource before the shaders can be created.
            // In this case the code in CustomShaderMaterialSample.xaml.cs does that. The following code is used:
            //var directoryShaderBytecodeProvider = new DirectoryShaderBytecodeProvider(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\Shaders"));
            //dxDevice.EffectsManager.RegisterShaderResource(directoryShaderBytecodeProvider);

            var vertexLayoutDesc = InputElementFactory.GetInputElementsArray(InputLayoutType.Position | InputLayoutType.Normal);

            dxDevice.EffectsManager.GetShaders(
                "MeshNormalShader.vs", "MeshNormalShader.ps", vertexLayoutDesc,
                out _vertexShaderSharedResource, out _pixelShaderSharedResource, out _inputLayoutSharedResource,
                throwExceptionIfShadersNotFound: true);


            // You could also use GetVertexShader and GetPixelShader methods:
            //_vertexShaderSharedResource = dxDevice.EffectsManager.GetVertexShader("MeshNormalShader.vs", vertexLayoutDesc, out _inputLayoutSharedResource, throwExceptionIfNotFound: true);
            //_pixelShaderSharedResource = dxDevice.EffectsManager.GetPixelShader("MeshNormalShader.ps", throwExceptionIfNotFound: true);


            // You could also use simple shader constructors to create the shaders:
            //// Create VertexShader
            //byte[] vertexShaderBytecode = System.IO.File.ReadAllBytes(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\Shaders\\MeshNormalShader.vs"));
            //_vertexShader = new VertexShader(parentDXDevice.Device, vertexShaderBytecode);

            //// Create InputLayout
            //_inputLayout = new InputLayout(parentDXDevice.Device, vertexShaderBytecode, vertexLayoutDesc);

            //// Create Pixel shader
            //byte[] pixelShaderBytecode = System.IO.File.ReadAllBytes(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\Shaders\\MeshNormalShader.ps"));
            //_pixelShader = new PixelShader(parentDXDevice.Device, pixelShaderBytecode);

            //// If we are using debug device we set DebugName - so we can see the correct name of shaders in Graphic debugging
            //if (parentDXDevice.IsDebugDevice)
            //{
            //    _vertexShader.DebugName = EffectName + "_VertexShader";
            //    _inputLayout.DebugName = EffectName + "_InputLayout";
            //    _pixelShader.DebugName = EffectName + "_PixelShader";
            //}


            _perFrameCameraConstantsBufferData = new PerFrameCameraConstantBuffer();
            _perFrameCameraConstantsBuffer     = parentDXDevice.CreateConstantBuffer(PerFrameCameraConstantBuffer.SizeInBytes, EffectName + "_perFrameCameraConstantsBuffer");

            _perFrameLightsConstantsBufferData = new PerFrameLightsConstantBuffer();
            _perFrameLightsConstantsBuffer     = parentDXDevice.CreateConstantBuffer(PerFrameLightsConstantBuffer.SizeInBytes, EffectName + "_perFrameLightsConstantsBuffer");

            _perObjectConstantsBufferData = new PerObjectConstantBuffer();
            _perObjectConstantsBuffer     = parentDXDevice.CreateConstantBuffer(PerObjectConstantBuffer.SizeInBytes, EffectName + "_perObjectConstantsBuffer");

            base.OnInitializeResources(dxDevice);
        }