/// <summary> /// Get a shader stages data to match the passed material if one exisits in the cache otherwise create a new one /// </summary> /// <param name="shaderResources"></param> /// <param name="customDefines"></param> /// <param name="specializationContants"></param> /// <returns></returns> public static ShaderStagesInfo GetOrCreateShaderStagesInfo(ShaderResource[] shaderResources, string customDefines, int[] specializationContants) { List <ShaderStageInfo> stages = new List <ShaderStageInfo>(); foreach (ShaderResource shaderResource in shaderResources) { stages.Add(GetOrCreateShaderStageInfo(shaderResource, customDefines, specializationContants)); } ShaderStagesInfo shaderStages = new ShaderStagesInfo { stages = stages.ToArray(), stagesCount = stages.Count }; // see if we have one that matches foreach (int idkey in _shaderStagesInfoCache.Keys) { ShaderStagesInfo ssd = _shaderStagesInfoCache[idkey]; if (ssd.Equals(shaderStages)) { return(ssd); } } // must be new shaderStages.id = _shaderStagesInfoCache.Count; _shaderStagesInfoCache[shaderStages.id] = shaderStages; return(shaderStages); }
private static void ExportTerrainMesh(Terrain terrain, ExportSettings settings, List <PipelineData> storePipelines = null) { TerrainConverter.TerrainInfo terrainInfo = TerrainConverter.CreateTerrainInfo(terrain, settings); if (terrainInfo == null || ((terrainInfo.diffuseTextureDatas.Count == 0 || terrainInfo.maskTextureDatas.Count == 0) && terrainInfo.customMaterial == null)) { return; } // add stategroup and pipeline for shader GraphBuilderInterface.unity2vsg_AddStateGroupNode(); PipelineData pipelineData = new PipelineData(); pipelineData.hasNormals = 1; pipelineData.uvChannelCount = 1; pipelineData.useAlpha = 0; if (terrainInfo.customMaterial == null) { pipelineData.descriptorBindings = NativeUtils.WrapArray(terrainInfo.descriptorBindings.ToArray()); ShaderStagesInfo shaderStagesInfo = MaterialConverter.GetOrCreateShaderStagesInfo(terrainInfo.shaderMapping.shaders.ToArray(), string.Join(",", terrainInfo.shaderDefines.ToArray()), terrainInfo.shaderConsts.ToArray()); pipelineData.shaderStages = shaderStagesInfo.ToNative(); pipelineData.id = NativeUtils.ToNative(NativeUtils.GetIDForPipeline(pipelineData)); storePipelines.Add(pipelineData); if (GraphBuilderInterface.unity2vsg_AddBindGraphicsPipelineCommand(pipelineData, 1) == 1) { if (terrainInfo.diffuseTextureDatas.Count > 0) { DescriptorImageData layerDiffuseTextureArray = MaterialConverter.GetOrCreateDescriptorImageData(terrainInfo.diffuseTextureDatas.ToArray(), 0); GraphBuilderInterface.unity2vsg_AddDescriptorImage(layerDiffuseTextureArray); } if (terrainInfo.diffuseScales.Count > 0) { DescriptorVectorArrayUniformData scalesDescriptor = new DescriptorVectorArrayUniformData(); scalesDescriptor.binding = 2; scalesDescriptor.value = NativeUtils.WrapArray(terrainInfo.diffuseScales.ToArray()); GraphBuilderInterface.unity2vsg_AddDescriptorBufferVectorArray(scalesDescriptor); } DescriptorVectorUniformData sizeDescriptor = new DescriptorVectorUniformData(); sizeDescriptor.binding = 3; sizeDescriptor.value = NativeUtils.ToNative(terrainInfo.terrainSize); GraphBuilderInterface.unity2vsg_AddDescriptorBufferVector(sizeDescriptor); if (terrainInfo.maskTextureDatas.Count > 0) { DescriptorImageData layerMaskTextureArray = MaterialConverter.GetOrCreateDescriptorImageData(terrainInfo.maskTextureDatas.ToArray(), 1); GraphBuilderInterface.unity2vsg_AddDescriptorImage(layerMaskTextureArray); } GraphBuilderInterface.unity2vsg_CreateBindDescriptorSetCommand(1); GraphBuilderInterface.unity2vsg_AddVertexIndexDrawNode(MeshConverter.GetOrCreateVertexIndexDrawData(terrainInfo.terrainMesh)); GraphBuilderInterface.unity2vsg_EndNode(); // step out of vertex index draw node } } else { pipelineData.descriptorBindings = NativeUtils.WrapArray(terrainInfo.customMaterial.descriptorBindings.ToArray()); pipelineData.shaderStages = terrainInfo.customMaterial.shaderStages.ToNative(); pipelineData.id = NativeUtils.ToNative(NativeUtils.GetIDForPipeline(pipelineData)); storePipelines.Add(pipelineData); if (GraphBuilderInterface.unity2vsg_AddBindGraphicsPipelineCommand(pipelineData, 1) == 1) { BindDescriptors(terrainInfo.customMaterial, true); GraphBuilderInterface.unity2vsg_AddVertexIndexDrawNode(MeshConverter.GetOrCreateVertexIndexDrawData(terrainInfo.terrainMesh)); GraphBuilderInterface.unity2vsg_EndNode(); // step out of vertex index draw node } } GraphBuilderInterface.unity2vsg_EndNode(); // step out of stategroup node }