Exemplo n.º 1
0
 public void BindVertexArrays(GLCmdVertexBufferObject vao)
 {
     mCache.SetVAO(vao.VBO);
 }
Exemplo n.º 2
0
        public CmdBufferInstructionSet Compose(IGLCmdBufferRepository repository, IEnumerable <GLCmdRenderPassCommand> passes)
        {
            var output = new CmdBufferInstructionSet();

            //var passes = new List<GLQueueRenderPass> ();
            var pipelines = new List <GLCmdBufferPipelineItem> ();
//			if (repository == null || repository.GraphicsPipelines.Count == 0)
//			{
//				pipelines.Add (new GLCmdBufferPipelineItem {
//					StencilState = mStencil.GetDefaultEnums(),
//					DepthState = mDepth.GetDefaultEnums(),
//				});
//			}

            var drawItems    = new List <GLCmdBufferDrawItem> ();
            var vertexArrays = SetupVBOs(repository);

            var viewports = SetupViewpoints(repository);

            var backCompareMasks  = new List <int> ();
            var frontCompareMasks = new List <int> ();
            var backReferences    = new List <int> ();
            var frontReferences   = new List <int> ();
            var backWriteMasks    = new List <int> ();
            var frontWriteMasks   = new List <int> ();
            var lineWidths        = new List <float> ();
            var scissors          = SetupScissors(repository);

            var blendConstants = new List <MgColor4f>();

            var descriptorSets = SetupDescriptorSets(repository);

            var depthBias   = new List <GLCmdDepthBiasParameter> ();
            var depthBounds = new List <GLCmdDepthBoundsParameter> ();
            var clearValues = new List <GLCmdClearValuesParameter> ();
            var colorBlends = new List <GLGraphicsPipelineBlendColorState> ();

            // Generate commands here

            // set up defaults
//			var defaultPass = new GLQueueRenderPass { Index = 0 } ;
//			passes.Add (defaultPass);

            GLCmdVertexBufferObject currentVertexArray = null;

            bool isFirst              = true;
            int  pastPipelineIndex    = 0;
            int  currentPipelineIndex = 0;
            GLCmdBufferPipelineItem pipelineItem;

            if (passes != null)
            {
                foreach (var pass in passes)
                {
//					var rp = new GLQueueRenderPass {
//						Index = (byte) passes.Count,
//						ClearValues = pass.ClearValues,
//					};
//					passes.Add (rp);

                    foreach (var command in pass.DrawCommands)
                    {
                        if (command.Pipeline.HasValue)
                        {
                            currentPipelineIndex = command.Pipeline.Value;

                            var pipeline = repository.GraphicsPipelines.At(currentPipelineIndex);

                            if (isFirst)
                            {
                                isFirst = false;
                                InsertNewPipeline(pass, pipeline, pipelines, clearValues, colorBlends);
                            }
                            else
                            {
                                if (pastPipelineIndex != currentPipelineIndex)
                                {
                                    InsertNewPipeline(pass, pipeline, pipelines, clearValues, colorBlends);
                                }
                            }

                            var item = new GLCmdBufferDrawItem {
                                Command                 = ExtractPolygonMode(pipeline),
                                ProgramID               = pipeline.ProgramID,
                                Topology                = pipeline.Topology,
                                Pipeline                = (byte)currentPipelineIndex,
                                DescriptorSet           = GetStoreViaNullableIndex <GLCmdDescriptorSetParameter>(descriptorSets, repository.DescriptorSets, command.DescriptorSet),
                                Viewport                = GetStoreViaNullableIndex <GLCmdViewportParameter>(viewports, repository.Viewports, command.Viewports),
                                BlendConstants          = GetStoreViaNullableIndex <MgColor4f>(blendConstants, repository.BlendConstants, command.BlendConstants),
                                LineWidth               = GetStoreViaNullableIndex <float>(lineWidths, repository.LineWidths, command.LineWidth),
                                DepthBias               = GetStoreViaNullableIndex <GLCmdDepthBiasParameter>(depthBias, repository.DepthBias, command.DepthBias),
                                DepthBounds             = GetStoreViaNullableIndex <GLCmdDepthBoundsParameter>(depthBounds, repository.DepthBounds, command.DepthBounds),
                                FrontStencilCompareMask = GetStoreViaNullableIndex <int>(frontCompareMasks, repository.FrontCompareMasks, command.FrontCompareMask),
                                BackStencilCompareMask  = GetStoreViaNullableIndex <int>(backCompareMasks, repository.BackCompareMasks, command.BackCompareMask),
                                FrontStencilReference   = GetStoreViaNullableIndex <int>(frontReferences, repository.FrontReferences, command.FrontReference),
                                BackStencilReference    = GetStoreViaNullableIndex <int>(backReferences, repository.BackReferences, command.BackReference),
                                FrontStencilWriteMask   = GetStoreViaNullableIndex <int>(frontWriteMasks, repository.FrontWriteMasks, command.FrontWriteMask),
                                BackStencilWriteMask    = GetStoreViaNullableIndex <int>(backWriteMasks, repository.BackWriteMasks, command.BackWriteMask),
                                Scissor                 = GetStoreViaNullableIndex <GLCmdScissorParameter>(scissors, repository.Scissors, command.Scissors),
                            };

                            pastPipelineIndex = currentPipelineIndex;

                            if (command.DrawIndexedIndirect != null)
                            {
                                item.Command |= GLCommandBufferFlagBits.CmdDrawIndexedIndirect;

                                var drawCommand = command.DrawIndexedIndirect;

                                item.Buffer = ExtractIndirectBuffer(drawCommand.buffer);

                                item.Count  = drawCommand.drawCount;
                                item.Offset = drawCommand.offset;
                                item.Stride = drawCommand.stride;
                            }
                            else if (command.DrawIndirect != null)
                            {
                                item.Command |= GLCommandBufferFlagBits.CmdDrawIndirect;

                                var drawCommand = command.DrawIndirect;

                                item.Buffer = ExtractIndirectBuffer(drawCommand.buffer);

                                item.Count  = drawCommand.drawCount;
                                item.Offset = drawCommand.offset;
                                item.Stride = drawCommand.stride;
                            }
                            else if (command.DrawIndexed != null)
                            {
                                item.Command |= GLCommandBufferFlagBits.CmdDrawIndexed;

                                var drawCommand = command.DrawIndexed;
                                item.First         = drawCommand.firstIndex;
                                item.FirstInstance = drawCommand.firstInstance;
                                item.Count         = drawCommand.indexCount;
                                item.VertexOffset  = drawCommand.vertexOffset;
                                item.InstanceCount = drawCommand.instanceCount;
                            }
                            else if (command.Draw != null)
                            {
                                var drawCommand = command.Draw;

                                item.FirstInstance = drawCommand.firstInstance;
                                item.First         = drawCommand.firstVertex;
                                item.InstanceCount = drawCommand.instanceCount;
                                item.Count         = drawCommand.vertexCount;
                            }
                            else
                            {
                                throw new InvalidOperationException();
                            }

                            if (command.VertexBuffer.HasValue)
                            {
                                // create new vbo
                                if (currentVertexArray == null)
                                {
                                    currentVertexArray = GenerateVBO(repository, command, pipeline);
                                    vertexArrays.Add(currentVertexArray);
                                }
                                else
                                {
                                    bool useSameBuffers = (command.IndexBuffer.HasValue)
                                                                                ? currentVertexArray.Matches(
                                        command.VertexBuffer.Value,
                                        command.IndexBuffer.Value)
                                                                                : currentVertexArray.Matches(
                                        command.VertexBuffer.Value);

                                    if (!useSameBuffers)
                                    {
                                        currentVertexArray = GenerateVBO(repository, command, pipeline);
                                        vertexArrays.Add(currentVertexArray);
                                    }
                                }

                                item.VBO = currentVertexArray.VBO;
                            }
                            if (command.IndexBuffer.HasValue)
                            {
                                var indexBuffer = repository.IndexBuffers.At(command.IndexBuffer.Value);
                                item.Command |= ExtractIndexType(indexBuffer, item.Command);
                            }

                            // build drawable
                            drawItems.Add(item);
                        }
                    }
                }
            }

            output.DrawItems   = drawItems.ToArray();
            output.Pipelines   = pipelines.ToArray();
            output.VBOs        = vertexArrays.ToArray();
            output.ClearValues = clearValues.ToArray();
            output.ColorBlends = colorBlends.ToArray();

            output.Viewports         = viewports.ToArray();
            output.BackCompareMasks  = backCompareMasks.ToArray();
            output.FrontCompareMasks = frontCompareMasks.ToArray();
            output.BackReferences    = backReferences.ToArray();
            output.FrontReferences   = frontReferences.ToArray();
            output.BackWriteMasks    = backWriteMasks.ToArray();
            output.FrontWriteMasks   = frontWriteMasks.ToArray();
            output.LineWidths        = lineWidths.ToArray();
            output.Scissors          = scissors.ToArray();
            output.BlendConstants    = blendConstants.ToArray();
            output.DescriptorSets    = descriptorSets.ToArray();
            output.DepthBias         = depthBias.ToArray();
            output.DepthBounds       = depthBounds.ToArray();

            return(output);
        }