private PipelineState(GraphicsDevice graphicsDevice, PipelineStateDescription pipelineStateDescription) : base(graphicsDevice)
        {
            // First time, build caches
            var pipelineStateCache = GetPipelineStateCache();

            var depthClampEmulation = !pipelineStateDescription.RasterizerState.DepthClipEnable && !graphicsDevice.HasDepthClamp;

#if SILICONSTUDIO_XENKO_GRAPHICS_API_OPENGLES
            // Depth Clamp can't be emulated on OpenGL ES 2 (TODO: warning?)
            if (graphicsDevice.IsOpenGLES2)
            {
                depthClampEmulation = false;
            }
#endif

            // Store states
            BlendState        = new BlendState(pipelineStateDescription.BlendState, pipelineStateDescription.Output.RenderTargetCount > 0);
            RasterizerState   = new RasterizerState(pipelineStateDescription.RasterizerState);
            DepthStencilState = new DepthStencilState(pipelineStateDescription.DepthStencilState, pipelineStateDescription.Output.DepthStencilFormat != PixelFormat.None);

            PrimitiveType = pipelineStateDescription.PrimitiveType.ToOpenGL();

            // Compile effect
            var effectBytecode = pipelineStateDescription.EffectBytecode;
            EffectProgram = effectBytecode != null?pipelineStateCache.EffectProgramCache.Instantiate(Tuple.Create(effectBytecode, depthClampEmulation)) : null;

            var rootSignature = pipelineStateDescription.RootSignature;
            if (rootSignature != null && effectBytecode != null)
            {
                ResourceBinder.Compile(graphicsDevice, rootSignature.EffectDescriptorSetReflection, effectBytecode);
            }

            // Vertex attributes
            if (pipelineStateDescription.InputElements != null)
            {
                var vertexAttribs = new List <VertexAttrib>();
                foreach (var inputElement in pipelineStateDescription.InputElements)
                {
                    // Query attribute name from effect
                    var attributeName = "a_" + inputElement.SemanticName + inputElement.SemanticIndex;
                    int attributeIndex;
                    if (!EffectProgram.Attributes.TryGetValue(attributeName, out attributeIndex))
                    {
                        continue;
                    }

                    var vertexElementFormat = VertexAttrib.ConvertVertexElementFormat(inputElement.Format);
                    vertexAttribs.Add(new VertexAttrib(
                                          inputElement.InputSlot,
                                          attributeIndex,
                                          vertexElementFormat.Size,
                                          vertexElementFormat.Type,
                                          vertexElementFormat.Normalized,
                                          inputElement.AlignedByteOffset));
                }

                VertexAttribs = pipelineStateCache.VertexAttribsCache.Instantiate(vertexAttribs.ToArray());
            }
        }
示例#2
0
 public EffectProgram GetOrCreateShader(GraphicsDevice graphicsDevice, EffectBytecode bytecode)
 {
     lock (ShaderLibrary)
     {
         EffectProgram effectProgram;
         if (!ShaderLibrary.TryGetValue(bytecode, out effectProgram))
         {
             effectProgram = new EffectProgram(graphicsDevice, bytecode);
             ShaderLibrary.Add(bytecode, effectProgram);
         }
         return(effectProgram);
     }
 }
示例#3
0
 public EffectProgram GetOrCreateShader(GraphicsDevice graphicsDevice, EffectBytecode bytecode)
 {
     lock (ShaderLibrary)
     {
         EffectProgram effectProgram;
         if (!ShaderLibrary.TryGetValue(bytecode, out effectProgram))
         {
             effectProgram = new EffectProgram(graphicsDevice, bytecode);
             ShaderLibrary.Add(bytecode, effectProgram);
         }
         return effectProgram;
     }
 }
示例#4
0
        private void Initialize(ParameterCollection usedParameters)
        {
            program    = EffectProgram.New(graphicsDeviceDefault, bytecode);
            reflection = program.Reflection;

            // prepare resource bindings used internally
            resourceBindings = new EffectParameterResourceBinding[reflection.ResourceBindings.Count];
            for (int i = 0; i < resourceBindings.Length; i++)
            {
                resourceBindings[i].Description = reflection.ResourceBindings[i];
            }
            defaultParameters = new ParameterCollection();
            inputSignature    = program.InputSignature;
            LoadDefaultParameters();
        }