// TODO: move this method to an extension method public ParameterKey <Texture> GetTextureKey(ComputeTextureBase computeTexture, MaterialComputeColorKeys baseKeys) { var keyResolved = (ParameterKey <Texture>)(computeTexture.Key ?? baseKeys.TextureBaseKey ?? MaterialKeys.GenericTexture); return(GetTextureKey(computeTexture.Texture, keyResolved, baseKeys.DefaultTextureValue)); }
public override ShaderSource GenerateShaderSource(MaterialGeneratorContext context, MaterialComputeColorKeys baseKeys) { // TODO: Use a generated UsedTexcoordIndex when backing textures var usedTexcoord = "TEXCOORD" + MaterialUtility.GetTextureIndex(TexcoordIndex); var textureKey = context.GetTextureKey(this, baseKeys); var samplerKey = context.GetSamplerKey(Sampler); UsedKey = textureKey; var scale = Scale; var scaleFactor = context.CurrentOverrides.UVScale; if (scaleFactor != Vector2.One) { scale *= scaleFactor; } var channelStr = GetTextureChannelAsString(); // "TTEXTURE", "TStream" ShaderClassSource shaderSource; // TODO: Workaround bad to have to copy all the new ShaderClassSource(). Check how to improve this if (context.OptimizeMaterials) { var scaleStr = MaterialUtility.GetAsShaderString(scale); var offsetStr = MaterialUtility.GetAsShaderString(Offset); // If materials are optimized, we precompute best shader combination (note: will generate shader permutations!) if (context.IsNotPixelStage) { if (Offset != Vector2.Zero) shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledOffsetSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, offsetStr, 0.0f); else if (scale != Vector2.One) shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, 0.0f); else shaderSource = new ShaderClassSource("ComputeColorTextureLodSampler", textureKey, usedTexcoord, samplerKey, channelStr, 0.0f); } else { if (Offset != Vector2.Zero) shaderSource = new ShaderClassSource("ComputeColorTextureScaledOffsetSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, offsetStr); else if (scale != Vector2.One) shaderSource = new ShaderClassSource("ComputeColorTextureScaledSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr); else shaderSource = new ShaderClassSource("ComputeColorTextureSampler", textureKey, usedTexcoord, samplerKey, channelStr); } } else { // Try to avoid shader permutations, by putting UV scaling/offset in shader parameters var textureScale = (ParameterKey<Vector2>)context.GetParameterKey(MaterialKeys.TextureScale); var textureOffset = (ParameterKey<Vector2>)context.GetParameterKey(MaterialKeys.TextureOffset); context.Parameters.Set(textureScale, scale); context.Parameters.Set(textureOffset, Offset); if (context.IsNotPixelStage) { shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledOffsetDynamicSampler", textureKey, usedTexcoord, samplerKey, channelStr, textureScale, textureOffset, 0.0f); } else { shaderSource = new ShaderClassSource("ComputeColorTextureScaledOffsetDynamicSampler", textureKey, usedTexcoord, samplerKey, channelStr, textureScale, textureOffset); } } return shaderSource; }