private static void GenerateShaders(List <GeneratedCodeData> outGeneratedCodeData, VFXExpressionGraph graph, IEnumerable <VFXContext> contexts, Dictionary <VFXContext, VFXContextCompiledData> contextToCompiledData, VFXCompilationMode compilationMode) { Profiler.BeginSample("VFXEditor.GenerateShaders"); try { foreach (var context in contexts) { var gpuMapper = graph.BuildGPUMapper(context); var uniformMapper = new VFXUniformMapper(gpuMapper, context.doesGenerateShader); // Add gpu and uniform mapper var contextData = contextToCompiledData[context]; contextData.gpuMapper = gpuMapper; contextData.uniformMapper = uniformMapper; contextToCompiledData[context] = contextData; if (context.doesGenerateShader) { var generatedContent = VFXCodeGenerator.Build(context, compilationMode, contextData); outGeneratedCodeData.Add(new GeneratedCodeData() { context = context, computeShader = context.codeGeneratorCompute, compilMode = compilationMode, content = generatedContent }); } } } finally { Profiler.EndSample(); } }
public void WriteBuffer(VFXUniformMapper mapper) { foreach (var buffer in mapper.buffers) { var name = mapper.GetName(buffer); WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(buffer.valueType), name); } }
public void WriteTexture(VFXUniformMapper mapper) { foreach (var texture in mapper.textures) { WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(texture.valueType), mapper.GetName(texture)); WriteLineFormat("SamplerState sampler{0};", mapper.GetName(texture)); } }
public void WriteTexture(VFXUniformMapper mapper) { foreach (var texture in mapper.textures) { string name = mapper.GetName(texture); WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(texture.valueType), name); WriteLineFormat("SamplerState sampler{0};", name); WriteLineFormat("float4 {0}_TexelSize;", name); } }
public void WriteCBuffer(VFXUniformMapper mapper, string bufferName) { var uniformValues = mapper.uniforms .Where(e => !e.IsAny(VFXExpression.Flags.Constant | VFXExpression.Flags.InvalidOnCPU)) // Filter out constant expressions .OrderByDescending(e => VFXValue.TypeToSize(e.valueType)); var uniformBlocks = new List <List <VFXExpression> >(); foreach (var value in uniformValues) { var block = uniformBlocks.FirstOrDefault(b => b.Sum(e => VFXValue.TypeToSize(e.valueType)) + VFXValue.TypeToSize(value.valueType) <= 4); if (block != null) { block.Add(value); } else { uniformBlocks.Add(new List <VFXExpression>() { value }); } } if (uniformBlocks.Count > 0) { WriteLineFormat("CBUFFER_START({0})", bufferName); Indent(); int paddingIndex = 0; foreach (var block in uniformBlocks) { int currentSize = 0; foreach (var value in block) { string type = VFXExpression.TypeToUniformCode(value.valueType); string name = mapper.GetName(value); if (name.StartsWith("unity_")) //Reserved unity variable name (could be filled manually see : VFXMotionVector) { continue; } currentSize += VFXExpression.TypeToSize(value.valueType); WriteLineFormat("{0} {1};", type, name); } WritePadding(4, currentSize, ref paddingIndex); } Deindent(); WriteLine("CBUFFER_END"); } }
public void WriteTexture(VFXUniformMapper mapper) { foreach (var texture in mapper.textures) { var names = mapper.GetNames(texture); // TODO At the moment issue all names sharing the same texture as different texture slots. This is not optimized as it required more texture binding than necessary for (int i = 0; i < names.Count; ++i) { WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(texture.valueType), names[i]); WriteLineFormat("SamplerState sampler{0};", names[i]); WriteLineFormat("float4 {0}_TexelSize;", names[i]); // TODO This is not very good to add a uniform for each texture that is hardly ever used WriteLine(); } } }
public void WriteBuffer(VFXUniformMapper mapper, ReadOnlyDictionary <VFXExpression, Type> usageGraphicsBuffer) { foreach (var buffer in mapper.buffers) { var name = mapper.GetName(buffer); if (buffer.valueType == VFXValueType.Buffer && usageGraphicsBuffer.TryGetValue(buffer, out var type)) { if (type == null) { throw new NullReferenceException("Unexpected null type in graphicsBuffer usage"); } var structureName = GetStructureName(type); WriteLineFormat("StructuredBuffer<{0}> {1};", structureName, name); } else { WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(buffer.valueType), name); } } }
public void WriteTexture(VFXUniformMapper mapper, IEnumerable <string> skipNames = null) { foreach (var texture in mapper.textures) { var names = mapper.GetNames(texture); // TODO At the moment issue all names sharing the same texture as different texture slots. This is not optimized as it required more texture binding than necessary for (int i = 0; i < names.Count; ++i) { if (skipNames != null && skipNames.Contains(names[i])) { continue; } WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(texture.valueType), names[i]); if (VFXExpression.IsTexture(texture.valueType)) //Mesh doesn't require a sampler or texel helper { WriteLineFormat("SamplerState sampler{0};", names[i]); WriteLineFormat("float4 {0}_TexelSize;", names[i]); // TODO This is not very good to add a uniform for each texture that is hardly ever used } WriteLine(); } } }