/// <summary> /// Render Commands for Mesh of Type: /// <see cref="Henzai.Geometry.VertexPositionColor"/> /// Also used in ShadowMap PrePass ///</summary> private static void GenerateCommandsForMesh_Inline <T>( CommandList commandList, ModelRuntimeDescriptor <T> modelState, int meshIndex, SceneRuntimeDescriptor sceneRuntimeDescriptor, ResourceSet[] effectResourceSets, Mesh <T> mesh, uint modelInstanceCount) where T : struct, VertexLocateable { var effectIndex = 1; var vertexBuffer = modelState.VertexBuffers[meshIndex]; var indexBuffer = modelState.IndexBuffers[meshIndex]; var cameraProjViewBuffer = sceneRuntimeDescriptor.CameraProjViewBuffer; var cameraResourceSet = sceneRuntimeDescriptor.CameraResourceSet; commandList.SetVertexBuffer(0, vertexBuffer); commandList.SetIndexBuffer(indexBuffer, IndexFormat.UInt16); commandList.UpdateBuffer(cameraProjViewBuffer, 128, mesh.World); commandList.SetGraphicsResourceSet(0, cameraResourceSet); for (int i = 0; i < effectResourceSets.Length; i++) { var resourceSet = effectResourceSets[i]; var resourceSetIndex = effectIndex + i; commandList.SetGraphicsResourceSet((uint)resourceSetIndex, resourceSet); } commandList.DrawIndexed( indexCount: mesh.Indices.Length.ToUnsigned(), instanceCount: modelInstanceCount, indexStart: 0, vertexOffset: 0, instanceStart: 0 ); }
public void AddModel(ModelRuntimeDescriptor <T> modelRuntimeDescriptor) { var meshCount = modelRuntimeDescriptor.Length; for (int i = 0; i < meshCount; i++) { var meshBVH = modelRuntimeDescriptor.Model.GetMeshBVH(i); MeshBVHList.Add(meshBVH); } }
/// <summary> /// Render Commands for Mesh of Type: /// <see cref="Henzai.Geometry.VertexPositionNormalTextureTangentBitangent"/> ///</summary> private static void GenerateCommandsForPNTTB_Inline( CommandList commandList, ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent> modelState, int meshIndex, SceneRuntimeDescriptor sceneRuntimeDescriptor, ResourceSet[] effectResourceSets, RealtimeMaterial material, Mesh <VertexPositionNormalTextureTangentBitangent> mesh, uint modelInstanceCount) { var effectIndex = 5; var vertexBuffer = modelState.VertexBuffers[meshIndex]; var indexBuffer = modelState.IndexBuffers[meshIndex]; var textureResourceSet = modelState.TextureResourceSets[meshIndex]; var cameraProjViewBuffer = sceneRuntimeDescriptor.CameraProjViewBuffer; var materialBuffer = sceneRuntimeDescriptor.MaterialBuffer; var cameraResourceSet = sceneRuntimeDescriptor.CameraResourceSet; var lightResourceSet = sceneRuntimeDescriptor.LightResourceSet; var pointlightResourceSet = sceneRuntimeDescriptor.SpotLightResourceSet; var materialResourceSet = sceneRuntimeDescriptor.MaterialResourceSet; commandList.SetVertexBuffer(0, vertexBuffer); commandList.SetIndexBuffer(indexBuffer, IndexFormat.UInt16); commandList.UpdateBuffer(cameraProjViewBuffer, 128, mesh.World); commandList.SetGraphicsResourceSet(0, cameraResourceSet); // Always after SetPipeline commandList.SetGraphicsResourceSet(1, lightResourceSet); commandList.SetGraphicsResourceSet(2, pointlightResourceSet); commandList.UpdateBuffer(materialBuffer, 0, material.diffuse); commandList.UpdateBuffer(materialBuffer, 16, material.specular); commandList.UpdateBuffer(materialBuffer, 32, material.ambient); commandList.UpdateBuffer(materialBuffer, 48, material.coefficients); commandList.SetGraphicsResourceSet(3, materialResourceSet); commandList.SetGraphicsResourceSet(4, textureResourceSet); for (int i = 0; i < effectResourceSets.Length; i++) { var resourceSet = effectResourceSets[i]; var resourceSetIndex = effectIndex + i; commandList.SetGraphicsResourceSet((uint)resourceSetIndex, resourceSet); } commandList.DrawIndexed( indexCount: mesh.Indices.Length.ToUnsigned(), instanceCount: modelInstanceCount, indexStart: 0, vertexOffset: 0, instanceStart: 0 ); }
public static void GenerateRenderCommandsForCubeMapModelDescriptor(CommandList commandList, ModelRuntimeDescriptor <VertexPosition> cubeMapRuntimeDescriptor, SceneRuntimeDescriptor sceneRuntimeDescriptor) { var model = cubeMapRuntimeDescriptor.Model; commandList.SetPipeline(cubeMapRuntimeDescriptor.Pipelines[RenderFlags.NORMAL_ARRAY_INDEX]); for (int i = 0; i < model.MeshCount; i++) { var mesh = model.GetMesh(i); RenderCommandGenerator.GenerateCommandsForP_Inline( commandList, cubeMapRuntimeDescriptor, i, sceneRuntimeDescriptor, cubeMapRuntimeDescriptor.EffectResourceSets[RenderFlags.NORMAL_ARRAY_INDEX], mesh, cubeMapRuntimeDescriptor.TotalInstanceCount ); } }
/// <summary> // Only used for cube maps for now! /// Render Commands for Mesh of Type: /// <see cref="Henzai.Geometry.VertexPosition"/> ///</summary> private static void GenerateCommandsForP_Inline( CommandList commandList, ModelRuntimeDescriptor <VertexPosition> modelState, int meshIndex, SceneRuntimeDescriptor sceneRuntimeDescriptor, ResourceSet[] effectResourceSets, Mesh <VertexPosition> mesh, uint modelInstanceCount) { var effectIndex = 2; var vertexBuffer = modelState.VertexBuffers[meshIndex]; var indexBuffer = modelState.IndexBuffers[meshIndex]; var textureResourceSet = modelState.TextureResourceSets[meshIndex]; var cameraProjViewBuffer = sceneRuntimeDescriptor.CameraProjViewBuffer; var cameraResourceSet = sceneRuntimeDescriptor.CameraResourceSet; commandList.SetVertexBuffer(0, vertexBuffer); commandList.SetIndexBuffer(indexBuffer, IndexFormat.UInt16); commandList.UpdateBuffer(cameraProjViewBuffer, 128, mesh.World); commandList.SetGraphicsResourceSet(0, cameraResourceSet); // Always after SetPipeline commandList.SetGraphicsResourceSet(1, textureResourceSet); for (int i = 0; i < effectResourceSets.Length; i++) { var resourceSet = effectResourceSets[i]; var resourceSetIndex = effectIndex + i; commandList.SetGraphicsResourceSet((uint)resourceSetIndex, resourceSet); } commandList.DrawIndexed( indexCount: mesh.Indices.Length.ToUnsigned(), instanceCount: modelInstanceCount, indexStart: 0, vertexOffset: 0, instanceStart: 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"); } }