예제 #1
0
        //TODO: @Investigate: What if multiple delegates are bound to the same event?
        public void InvokeVertexLayoutGeneration()
        {
            // Base vertex geometry
            VertexLayouts[0]           = CallVertexLayoutGeneration.Invoke();
            VertexPreEffectsLayouts[0] = CallVertexLayoutGeneration.Invoke();

            // Instancing data i.e. position, view matrices
            var instancingEventKeys = InstancingEventHandlerKeys.GetKeys();
            var flagIndex           = InstancingDataFlags.GetArrayIndexForFlag(InstancingDataFlag);

            if (flagIndex >= 0)
            {
                var instancingEventKey = instancingEventKeys[flagIndex];
                var instancingVertexInstanceDeletegate = (VertexInstanceLayoutGenerationList[instancingEventKey] as Func <VertexLayoutDescription>);
                VertexLayouts[flagIndex + 1] = instancingVertexInstanceDeletegate.Invoke();
            }

            // Instancing effects data
            var preEffectsEventKeys = PreEffectEventHandlerKeys.GetKeys();
            var preEffectsflagIndex = RenderFlags.GetPreEffectArrayIndexForFlag(PreEffectsInstancingFlag);

            if (preEffectsflagIndex >= 0)
            {
                var preEffectsEventKey       = preEffectsEventKeys[preEffectsflagIndex];
                var vertexInstanceDeletegate = (VertexPreEffectsInstanceLayoutGenerationList[preEffectsEventKey] as Func <VertexLayoutDescription>);
                VertexPreEffectsLayouts[preEffectsflagIndex + 1] = vertexInstanceDeletegate.Invoke();
            }
        }
예제 #2
0
        public void LoadShaders(GraphicsDevice graphicsDevice)
        {
            VertexShader   = IO.LoadShader(_vertexShaderName, ShaderStages.Vertex, graphicsDevice);
            FragmentShader = IO.LoadShader(_fragmentShaderName, ShaderStages.Fragment, graphicsDevice);

            var preEffects = RenderFlags.GetAllPreEffectFor(PreEffectsFlag);

            foreach (var flag in preEffects)
            {
                var arrayIndex = RenderFlags.GetPreEffectArrayIndexForFlag(flag);
                var name       = ShaderNames.PreEffectShaderNames[arrayIndex];
                VertexPreEffectShaders[arrayIndex]   = IO.LoadShader(name, ShaderStages.Vertex, graphicsDevice);
                GeometryPreEffectShaders[arrayIndex] = IO.LoadShader(name, ShaderStages.Geometry, graphicsDevice);
                FragmentPreEffectShaders[arrayIndex] = IO.LoadShader(name, ShaderStages.Fragment, graphicsDevice);
            }
        }
예제 #3
0
        public ResourceLayout[] FillEffectsResourceSet(DisposeCollectorResourceFactory factory, SceneRuntimeDescriptor sceneRuntimeDescriptor, List <SubRenderable> childrenPre)
        {
            var effectLayoutList = new List <ResourceLayout>();

            //TODO: just iterate over children and process them as Subrenderable !!

            if (ShadowMapEnabled)
            {
                var effectCount = RenderFlags.GetSizeOfPreEffectFlag(PreEffectsFlag) * 2; // 1 for Vertex Stage 1 for Fragment
                EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX] = new ResourceSet[effectCount];
                var shadowMapRenderable = childrenPre[0] as ShadowMap;

                var shadowMapResourceLayout = ResourceGenerator.GenerateTextureResourceLayoutForShadowMapping(factory);
                effectLayoutList.Add(sceneRuntimeDescriptor.LightProvViewResourceLayout);
                effectLayoutList.Add(shadowMapResourceLayout);
                EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.SHADOW_MAP)]     = sceneRuntimeDescriptor.LightProjViewResourceSet;
                EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.SHADOW_MAP) + 1] = ResourceGenerator.GenerateResourceSetForShadowMapping(shadowMapResourceLayout, shadowMapRenderable.ShadowMapTexView, factory);
            }

            if (OmniShadowMapEnabled)
            {
                var effectCount = 2;
                EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX] = new ResourceSet[effectCount];
                EffectResourceSets[RenderFlags.OMNI_SHADOW_MAPS]   = new ResourceSet[2];
                //var omniShadowMapRenderable =  childrenPre[RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.OMNI_SHADOW_MAPS)] as OmniShadowMap;
                var omniShadowMapRenderable = childrenPre[0] as OmniShadowMap;
                var shadowMapResourceLayout = ResourceGenerator.GenerateTextureResourceLayoutForOmniShadowMapping(factory);
                //TODO: this has to be an array
                effectLayoutList.Add(sceneRuntimeDescriptor.CameraInfoResourceLayout);
                effectLayoutList.Add(shadowMapResourceLayout);
                EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][0] = sceneRuntimeDescriptor.CameraInfoResourceSet;
                EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX][1] = ResourceGenerator.GenerateResourceSetForShadowMapping(shadowMapResourceLayout, omniShadowMapRenderable.ShadowMapTexView, factory);
                EffectResourceSets[RenderFlags.OMNI_SHADOW_MAPS][0]   = omniShadowMapRenderable.ShadowMatrices;
                EffectResourceSets[RenderFlags.OMNI_SHADOW_MAPS][1]   = omniShadowMapRenderable.CameraInfo;
            }


            return(effectLayoutList.ToArray());
        }
예제 #4
0
        /// <summary>
        /// Sets delegates for runtime command generation.
        /// Also updates index and vertex buffers, sets pipeline
        /// </summary>
        /// <param name="modelDescriptor">Model descriptor.</param>
        /// <param name="sceneRuntimeDescriptor">Scene runtime descriptor.</param>
        /// <param name="instancingData">Instance data enumerable.</param>
        /// <typeparam name="T">The type of Vertex sent to the GPU</typeparam>
        protected void FillRuntimeDescriptor <T>(ModelRuntimeDescriptor <T> modelDescriptor, SceneRuntimeDescriptor sceneRuntimeDescriptor, IEnumerable <InstanceData> instancingData) where T : struct, VertexRuntime, VertexLocateable
        {
            var model = modelDescriptor.Model;

            modelDescriptor.TextureResourceLayout = modelDescriptor.InvokeTextureResourceLayoutGeneration(_factory);
            modelDescriptor.TextureSampler        = modelDescriptor.InvokeSamplerGeneration(_factory);
            modelDescriptor.LoadShaders(_graphicsDevice);
            byte vertexSizeInBytes = model.GetMesh(0).Vertices[0].GetSizeInBytes();

            var meshCount = model.MeshCount;

            for (int i = 0; i < meshCount; i++)
            {
                var          mesh    = model.GetMesh(i);
                var          meshBVH = model.GetMeshBVH(i);
                DeviceBuffer vertexBuffer
                    = _factory.CreateBuffer(new BufferDescription(mesh.Vertices.LengthUnsigned() * vertexSizeInBytes, BufferUsage.VertexBuffer));

                DeviceBuffer indexBuffer
                    = _factory.CreateBuffer(new BufferDescription(mesh.Indices.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer));


                modelDescriptor.VertexBufferList.Add(vertexBuffer);
                modelDescriptor.IndexBufferList.Add(indexBuffer);
                var allInstancePreEffects = RenderFlags.GetAllPreEffectFor(modelDescriptor.PreEffectsInstancingFlag);

                foreach (var instanceData in instancingData)
                {
                    var instanceDataBuffer = ResourceGenerator.AllocateInstanceDataBuffer(instanceData, _graphicsDevice, _factory);
                    if (instanceDataBuffer != null)
                    {
                        modelDescriptor.InstanceBufferLists[RenderFlags.NORMAL_ARRAY_INDEX].Add(instanceDataBuffer);
                        foreach (var preEffect in allInstancePreEffects)
                        {
                            modelDescriptor.InstanceBufferLists[RenderFlags.GetArrayIndexForFlag(preEffect)].Add(instanceDataBuffer);
                        }
                    }
                }

                _graphicsDevice.UpdateBuffer <T>(vertexBuffer, 0, ref mesh.Vertices[0], (vertexSizeInBytes * mesh.VertexCount).ToUnsigned());
                _graphicsDevice.UpdateBuffer <ushort>(indexBuffer, 0, ref mesh.Indices[0], (sizeof(ushort) * mesh.IndexCount).ToUnsigned());

                var resourceSet = modelDescriptor.InvokeTextureResourceSetGeneration(i, _factory, _graphicsDevice);
                if (resourceSet != null)
                {
                    modelDescriptor.TextureResourceSetsList.Add(resourceSet);
                }
            }

            var rasterizerStateCullBack = new RasterizerStateDescription(
                cullMode: FaceCullMode.Back,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false
                );
            var rasterizerStateCullFront = new RasterizerStateDescription(
                cullMode: FaceCullMode.Front,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,

                depthClipEnabled: true,
                scissorTestEnabled: false
                );

            var rasterizerStateCullNone = new RasterizerStateDescription(
                cullMode: FaceCullMode.None,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,

                depthClipEnabled: true,
                scissorTestEnabled: false
                );


            modelDescriptor.InvokeVertexLayoutGeneration();

            var shadowMapIndex     = RenderFlags.GetArrayIndexForFlag(RenderFlags.SHADOW_MAP);
            var omniShadowMapIndex = RenderFlags.GetArrayIndexForFlag(RenderFlags.OMNI_SHADOW_MAPS);

            modelDescriptor.Pipelines[shadowMapIndex]     = modelDescriptor.ShadowMapEnabled ? _factory.CreateGraphicsPipeline(PipelineGenerator.GenerateShadowMappingPreEffectPipeline(modelDescriptor, _childrenPre[RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.SHADOW_MAP)].SceneRuntimeDescriptor, rasterizerStateCullFront, RenderFlags.SHADOW_MAP, _childrenPre[RenderFlags.GetPreEffectArrayIndexForFlag(RenderFlags.SHADOW_MAP)].FrameBuffer)) : null;
            modelDescriptor.Pipelines[omniShadowMapIndex] = modelDescriptor.OmniShadowMapEnabled ? _factory.CreateGraphicsPipeline(PipelineGenerator.GenerateOmniShadowMappingPreEffectPipeline(modelDescriptor, _childrenPre[0].SceneRuntimeDescriptor, rasterizerStateCullNone, RenderFlags.OMNI_SHADOW_MAPS, _childrenPre[0].FrameBuffer)) : null;


            var effectLayoutArray = modelDescriptor.FillEffectsResourceSet(_factory, sceneRuntimeDescriptor, _childrenPre);
            var normalIndex       = RenderFlags.GetArrayIndexForFlag(RenderFlags.NORMAL);

            switch (modelDescriptor.VertexRuntimeType)
            {
            // Only cube maps for now
            case VertexRuntimeTypes.VertexPosition:
                //TODO: Use proper constants from RenderFlags.cs
                modelDescriptor.Pipelines[normalIndex] = _factory.CreateGraphicsPipeline(PipelineGenerator.GeneratePipelineP(modelDescriptor, sceneRuntimeDescriptor, rasterizerStateCullFront, _graphicsDevice.SwapchainFramebuffer, effectLayoutArray));
                break;

            case VertexRuntimeTypes.VertexPositionNormal:
                modelDescriptor.Pipelines[normalIndex] = _factory.CreateGraphicsPipeline(PipelineGenerator.GeneratePipelinePN(modelDescriptor, sceneRuntimeDescriptor, rasterizerStateCullBack, _graphicsDevice.SwapchainFramebuffer, effectLayoutArray));
                break;

            case VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent:
                modelDescriptor.Pipelines[normalIndex] = _factory.CreateGraphicsPipeline(PipelineGenerator.GeneratePipelinePNTTB(modelDescriptor, sceneRuntimeDescriptor, rasterizerStateCullBack, _graphicsDevice.SwapchainFramebuffer, effectLayoutArray));
                break;

            case VertexRuntimeTypes.VertexPositionColor:
                modelDescriptor.Pipelines[normalIndex] = _factory.CreateGraphicsPipeline(PipelineGenerator.GeneratePipelinePC(modelDescriptor, sceneRuntimeDescriptor, rasterizerStateCullBack, _graphicsDevice.SwapchainFramebuffer, effectLayoutArray));
                break;

            case VertexRuntimeTypes.VertexPositionTexture:
                modelDescriptor.Pipelines[normalIndex] = _factory.CreateGraphicsPipeline(PipelineGenerator.GeneratePipelinePT(modelDescriptor, sceneRuntimeDescriptor, rasterizerStateCullBack, _graphicsDevice.SwapchainFramebuffer, effectLayoutArray));
                break;

            default:
                throw new NotImplementedException($"{modelDescriptor.VertexRuntimeType.ToString("g")} not implemented");
            }
        }
예제 #5
0
        public void AddPreEffectsVertexInstanceDelegate(uint id, Func <VertexLayoutDescription> vertexLayoutDelegate)
        {
            var flagIndex = RenderFlags.GetPreEffectArrayIndexForFlag(PreEffectsInstancingFlag);

            VertexPreEffectsInstanceLayoutGenerationList.AddHandler(PreEffectEventHandlerKeys.GetKeys()[flagIndex], vertexLayoutDelegate);
        }