//TODO: Refactor renderFlag preEffectsInstancingFlag and instancingFlag into an class/struct public ModelRuntimeDescriptor(Model <T, RealtimeMaterial> modelIn, string vShaderName, string fShaderName, VertexRuntimeTypes vertexRuntimeType, PrimitiveTopology primitiveTopology, RenderDescription renderDescription, InstancingRenderDescription instancingRenderDescription) { if (!Verifier.VerifyVertexStruct <T>(vertexRuntimeType)) { throw new ArgumentException($"Type Mismatch ModelRuntimeDescriptor"); } Model = modelIn; Length = modelIn.MeshCount; TotalInstanceCount = 1; _vertexShaderName = vShaderName; _fragmentShaderName = fShaderName; VertexRuntimeType = vertexRuntimeType; PrimitiveTopology = primitiveTopology; RenderDescription = renderDescription; PreEffectsFlag = RenderFlags.PRE_EFFECTS_MASK & renderDescription.RenderModeFlag; PreEffectsInstancingFlag = instancingRenderDescription.PreEffectsFlag; InstancingDataFlag = instancingRenderDescription.RenderModeFlag; var preEffectsInstancing = RenderFlags.GetAllPreEffectFor(PreEffectsInstancingFlag); VertexBufferList = new List <DeviceBuffer>(); IndexBufferList = new List <DeviceBuffer>(); Pipelines = new Pipeline[RenderFlags.EFFCTS_TOTAL_COUNT]; InstanceBufferLists = new List <DeviceBuffer> [RenderFlags.EFFCTS_TOTAL_COUNT]; InstanceBufferLists[RenderFlags.NORMAL_ARRAY_INDEX] = new List <DeviceBuffer>(); foreach (var preEffect in preEffectsInstancing) { InstanceBufferLists[RenderFlags.GetArrayIndexForFlag(preEffect)] = new List <DeviceBuffer>(); } InstanceBuffers = new DeviceBuffer[RenderFlags.EFFCTS_TOTAL_COUNT][]; TextureResourceSetsList = new List <ResourceSet>(); VertexInstanceLayoutGenerationList = new EventHandlerList(); VertexPreEffectsInstanceLayoutGenerationList = new EventHandlerList(); VertexPreEffectShaders = new Shader[RenderFlags.PRE_EFFCTS_TOTAL_COUNT]; GeometryPreEffectShaders = new Shader[RenderFlags.PRE_EFFCTS_TOTAL_COUNT]; FragmentPreEffectShaders = new Shader[RenderFlags.PRE_EFFCTS_TOTAL_COUNT]; // Reserve first spot for base vertex geometry //TODO: Make on list VertexLayouts = new VertexLayoutDescription[InstancingDataFlags.GetSizeOfPreEffectFlag(InstancingDataFlag) + 1]; VertexPreEffectsLayouts = new VertexLayoutDescription[RenderFlags.GetSizeOfPreEffectFlag(PreEffectsInstancingFlag) + 1]; EffectResourceSets = new ResourceSet[RenderFlags.EFFCTS_TOTAL_COUNT][]; for (int i = 0; i < RenderFlags.EFFCTS_TOTAL_COUNT; i++) { EffectResourceSets[i] = new ResourceSet[0]; } }
//TODO: Data driven -> should refactor so that all GenerateRenderCommandsForModelDescriptor use this structure public static void GenerateRenderCommandsForModelDescriptor(CommandList commandList, ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>[] descriptorArray, SceneRuntimeDescriptor sceneRuntimeDescriptor, MeshBVH <VertexPositionNormalTextureTangentBitangent>[] meshBVHArray, RenderDescription renderDescription) { var meshCount = meshBVHArray.Length; for (int j = 0; j < meshCount; j++) { var meshBVH = meshBVHArray[j]; if (!meshBVH.AABBIsValid) { continue; } var modelStateIndex = meshBVH.ModelRuntimeIndex; var meshIndex = meshBVH.MeshRuntimeIndex; var modelState = descriptorArray[modelStateIndex]; var model = modelState.Model; var modelRenderFlag = modelState.RenderDescription.RenderModeFlag; var currentRenderState = modelRenderFlag & renderDescription.RenderModeFlag; if (currentRenderState == RenderFlags.NONE) { continue; } var renderStateArrayIndex = RenderFlags.GetArrayIndexForFlag(currentRenderState); commandList.SetPipeline(modelState.Pipelines[renderStateArrayIndex]); var effectsInstanceBuffer = modelState.InstanceBuffers[renderStateArrayIndex]; // We assume one other vertex buffer has been bound or will be bound. for (int i = 0; i < effectsInstanceBuffer.Length; i++) { commandList.SetVertexBuffer(i.ToUnsigned() + 1, effectsInstanceBuffer[i]); } var effectSets = modelState.EffectResourceSets[renderStateArrayIndex]; var mesh = model.GetMesh(j); var material = model.GetMaterial(meshIndex); RenderCommandGenerator.GenerateCommandsForPNTTB_Inline( commandList, modelState, meshIndex, sceneRuntimeDescriptor, effectSets, material, mesh, modelState.TotalInstanceCount ); } }
public static void GenerateRenderCommandsForModelDescriptor <T>(CommandList commandList, ModelRuntimeDescriptor <T>[] descriptorArray, SceneRuntimeDescriptor sceneRuntimeDescriptor, RenderDescription renderDescription, VertexRuntimeTypes vertexRuntimeType) where T : struct, VertexLocateable { for (int j = 0; j < descriptorArray.Length; j++) { var modelState = descriptorArray[j]; var modelRenderFlag = modelState.RenderDescription.RenderModeFlag; var currentRenderState = modelRenderFlag & renderDescription.RenderModeFlag; if (currentRenderState == RenderFlags.NONE) { continue; } var renderStateArrayIndex = RenderFlags.GetArrayIndexForFlag(currentRenderState); commandList.SetPipeline(modelState.Pipelines[renderStateArrayIndex]); var effectsInstanceBuffer = modelState.InstanceBuffers[renderStateArrayIndex]; // We assume one other vertex buffer has been bound or will be bound. for (int i = 0; i < effectsInstanceBuffer.Length; i++) { commandList.SetVertexBuffer(i.ToUnsigned() + 1, effectsInstanceBuffer[i]); } var effectSets = modelState.EffectResourceSets[renderStateArrayIndex]; var model = modelState.Model; for (int i = 0; i < model.MeshCount; i++) { if (!model.GetMeshBVH(i).AABBIsValid) { continue; } var mesh = model.GetMesh(i); var material = model.GetMaterial(i); //TODO: @Investiagte this if ((renderDescription.RenderModeFlag & RenderFlags.SHADOW_MAP) == RenderFlags.SHADOW_MAP || (renderDescription.RenderModeFlag & RenderFlags.OMNI_SHADOW_MAPS) == RenderFlags.OMNI_SHADOW_MAPS || vertexRuntimeType == VertexRuntimeTypes.VertexPositionColor) { RenderCommandGenerator.GenerateCommandsForMesh_Inline( commandList, modelState, i, sceneRuntimeDescriptor, effectSets, mesh, modelState.TotalInstanceCount ); } else { switch (vertexRuntimeType) { case VertexRuntimeTypes.VertexPosition: RenderCommandGenerator.GenerateCommandsForP_Inline( commandList, modelState as ModelRuntimeDescriptor <VertexPosition>, i, sceneRuntimeDescriptor, effectSets, mesh as Mesh <VertexPosition>, modelState.TotalInstanceCount ); break; case VertexRuntimeTypes.VertexPositionTexture: RenderCommandGenerator.GenerateCommandsForPT_Inline( commandList, modelState as ModelRuntimeDescriptor <VertexPositionTexture>, i, sceneRuntimeDescriptor, effectSets, mesh as Mesh <VertexPositionTexture>, modelState.TotalInstanceCount ); break; case VertexRuntimeTypes.VertexPositionNormal: RenderCommandGenerator.GenerateCommandsForPN_Inline( commandList, modelState as ModelRuntimeDescriptor <VertexPositionNormal>, i, sceneRuntimeDescriptor, effectSets, material, mesh as Mesh <VertexPositionNormal>, modelState.TotalInstanceCount ); break; case VertexRuntimeTypes.VertexPositionNormalTextureTangentBitangent: RenderCommandGenerator.GenerateCommandsForPNTTB_Inline( commandList, modelState as ModelRuntimeDescriptor <VertexPositionNormalTextureTangentBitangent>, i, sceneRuntimeDescriptor, effectSets, material, mesh as Mesh <VertexPositionNormalTextureTangentBitangent>, modelState.TotalInstanceCount ); break; default: var errorStr = "Type: " + typeof(T).Name + " not implemented"; throw new System.NotImplementedException(errorStr); } } } } }