Exemplo n.º 1
0
        private void Generate(MaterialShaderStage stage, MaterialGeneratorContext context)
        {
            if (!context.HasSurfaceShaders(stage))
            {
                return;
            }

            // Blend setup for this layer
            context.SetStream(stage, BlendStream, BlendMap, MaterialKeys.BlendMap, MaterialKeys.BlendValue);

            // Generate a dynamic shader name
            // Create a mixin
            var shaderMixinSource = new ShaderMixinSource();

            shaderMixinSource.Mixins.Add(new ShaderClassSource("MaterialSurfaceStreamsBlend"));

            // Add all streams
            foreach (var stream in context.Streams[stage])
            {
                shaderMixinSource.AddCompositionToArray("blends", context.GetStreamBlendShaderSource(stream));
            }

            var materialBlendLayerMixin = context.GenerateSurfaceShader(stage);

            // Add the shader to the mixin
            shaderMixinSource.AddComposition("layer", materialBlendLayerMixin);

            context.ResetSurfaceShaders(stage);
            context.AddSurfaceShader(stage, shaderMixinSource);
        }
Exemplo n.º 2
0
            public ShaderSource GenerateStreamInilizer(MaterialShaderStage stage)
            {
                // Early exit if nothing to do
                if (StreamInitializers[stage].Count == 0 && SurfaceShaders[stage].Count == 0 && stage != MaterialShaderStage.Pixel)
                {
                    return(null);
                }

                var mixin = new ShaderMixinSource();

                // the basic streams contained by every materials
                mixin.Mixins.Add(new ShaderClassSource("MaterialStream"));

                // the streams coming from the material layers
                foreach (var streamInitializer in StreamInitializers[stage])
                {
                    mixin.Mixins.Add(streamInitializer);
                }
                StreamInitializers[stage].Clear();

                // the streams specific to a stage
                if (stage == MaterialShaderStage.Pixel)
                {
                    mixin.Mixins.Add("MaterialPixelShadingStream");
                }

                return(mixin);
            }
Exemplo n.º 3
0
            public ShaderSource GenerateSurfaceShader(MaterialShaderStage stage)
            {
                var surfaceShaders = GetSurfaceShaders(stage);

                if (surfaceShaders.Count == 0)
                {
                    return(null);
                }

                ShaderSource result;

                // If there is only a single op, don't generate a mixin
                if (surfaceShaders.Count == 1)
                {
                    result = surfaceShaders[0];
                }
                else
                {
                    var mixin = new ShaderMixinSource();
                    result = mixin;
                    mixin.Mixins.Add(new ShaderClassSource("MaterialSurfaceArray"));

                    // Squash all operations into MaterialLayerArray
                    foreach (var operation in surfaceShaders)
                    {
                        mixin.AddCompositionToArray("layers", operation);
                    }
                }

                surfaceShaders.Clear();
                Streams[stage].Clear();
                return(result);
            }
Exemplo n.º 4
0
        public ShaderSource GetStreamFinalModifier <T>(MaterialShaderStage stage)
        {
            ShaderSource shaderSource = null;

            finalInputStreamModifiers.TryGetValue(new KeyValuePair <MaterialShaderStage, Type>(stage, typeof(T)), out shaderSource);
            return(shaderSource);
        }
        public void SetStream(MaterialShaderStage stage, string stream, IComputeNode computeNode, ObjectParameterKey<Texture> defaultTexturingKey, ParameterKey defaultValueKey, Color? defaultTextureValue)
        {
            if (defaultValueKey == null) throw new ArgumentNullException(nameof(defaultValueKey));
            if (computeNode == null)
            {
                return;
            }

            var streamType = MaterialStreamType.Float;
            if (defaultValueKey.PropertyType == typeof(Vector4) || defaultValueKey.PropertyType == typeof(Color4))
            {
                streamType = MaterialStreamType.Float4;
            }
            else if (defaultValueKey.PropertyType == typeof(Vector3) || defaultValueKey.PropertyType == typeof(Color3))
            {
                streamType = MaterialStreamType.Float3;
            }
            else if (defaultValueKey.PropertyType == typeof(Vector2) || defaultValueKey.PropertyType == typeof(Half2))
            {
                streamType = MaterialStreamType.Float2;
            }
            else if (defaultValueKey.PropertyType == typeof(float))
            {
                streamType = MaterialStreamType.Float;
            }
            else
            {
                throw new NotSupportedException("ParameterKey type [{0}] is not supported by SetStream".ToFormat(defaultValueKey.PropertyType));
            }

            var classSource = computeNode.GenerateShaderSource(Context, new MaterialComputeColorKeys(defaultTexturingKey, defaultValueKey, defaultTextureValue));
            SetStream(stage, stream, streamType, classSource);
        }
Exemplo n.º 6
0
        public ShaderSource GenerateStreamInitializers(MaterialShaderStage stage)
        {
            // Early exit if nothing to do
            var stageContext = GetContextPerStage(stage);

            if (stageContext.StreamInitializers.Count == 0 && stageContext.ShaderSources.Count == 0 && stage != MaterialShaderStage.Pixel)
            {
                return(null);
            }

            var mixin = new ShaderMixinSource();

            // the basic streams contained by every materials
            mixin.Mixins.Add(new ShaderClassSource("MaterialStream"));

            // the streams coming from the material layers
            foreach (var streamInitializer in stageContext.StreamInitializers)
            {
                mixin.Mixins.Add(new ShaderClassSource(streamInitializer));
            }
            stageContext.StreamInitializers.Clear();

            // the streams specific to a stage
            // TODO: Use StreamInitializers instead of streams initializers hardcoded in MaterialPixelShadingStream.ResetStream
            if (stage == MaterialShaderStage.Pixel)
            {
                mixin.Mixins.Add(new ShaderClassSource("MaterialPixelShadingStream"));
            }

            return(mixin);
        }
Exemplo n.º 7
0
 public void UseStream(MaterialShaderStage stage, string stream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     currentLayerContext.GetContextPerStage(stage).Streams.Add(stream);
 }
Exemplo n.º 8
0
 public void UseStream(MaterialShaderStage stage, string stream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     Current.Streams[stage].Add(stream);
 }
Exemplo n.º 9
0
 public void AddSurfaceShader(MaterialShaderStage stage, ShaderSource shaderSource)
 {
     if (shaderSource == null)
     {
         throw new ArgumentNullException("shaderSource");
     }
     Current.GetSurfaceShaders(stage).Add(shaderSource);
 }
Exemplo n.º 10
0
 public void AddShaderSource(MaterialShaderStage stage, ShaderSource shaderSource)
 {
     if (shaderSource == null)
     {
         throw new ArgumentNullException(nameof(shaderSource));
     }
     currentLayerContext.GetContextPerStage(stage).ShaderSources.Add(shaderSource);
 }
Exemplo n.º 11
0
 public void UseStreamWithCustomBlend(MaterialShaderStage stage, string stream, ShaderSource blendStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     UseStream(stage, stream);
     registeredStreamBlend[stream] = blendStream;
 }
Exemplo n.º 12
0
        public void SetStreamFinalModifier <T>(MaterialShaderStage stage, ShaderSource shaderSource)
        {
            if (shaderSource == null)
            {
                return;
            }

            var typeT = typeof(T);

            finalInputStreamModifiers[new KeyValuePair <MaterialShaderStage, Type>(stage, typeT)] = shaderSource;
        }
Exemplo n.º 13
0
        public void AddAdjacentEdgeAverageMacros(MaterialShaderStage stage, MaterialGeneratorContext context)
        {
            var tessellationShader = context.Parameters.Get(MaterialKeys.TessellationShader) as ShaderMixinSource;

            if (tessellationShader == null)
            {
                return;
            }

            tessellationShader.Macros.Add(new ShaderMacro("InputControlPointCount", 12));
        }
        public void AddAdjacentEdgeAverageShaders(MaterialShaderStage stage, MaterialGeneratorContext context)
        {
            var tessellationShader = context.Parameters.Get(MaterialKeys.TessellationShader) as ShaderMixinSource;
            if(tessellationShader == null)
                return;

            if (context.GetStreamFinalModifier<MaterialDisplacementMapFeature>(MaterialShaderStage.Domain) != null)
            {
                tessellationShader.Mixins.Add(new ShaderClassSource("TessellationAE2", "TexCoord")); // this suppose Displacement from Texture -> TODO make it more flexible so that it works with any kind of displacement.
                tessellationShader.Mixins.Add(new ShaderClassSource("TessellationAE3", "normalWS"));
            }
        }
Exemplo n.º 15
0
        public void AddAdjacentEdgeAverageShaders(MaterialShaderStage stage, MaterialGeneratorContext context)
        {
            var tessellationShader = context.Parameters.Get(MaterialKeys.TessellationShader) as ShaderMixinSource;

            if (tessellationShader == null)
            {
                return;
            }

            if (context.GetStreamFinalModifier <MaterialDisplacementMapFeature>(MaterialShaderStage.Domain) != null)
            {
                tessellationShader.Mixins.Add(new ShaderClassSource("TessellationAE2", "TexCoord")); // this suppose Displacement from Texture -> TODO make it more flexible so that it works with any kind of displacement.
                tessellationShader.Mixins.Add(new ShaderClassSource("TessellationAE3", "normalWS"));
            }
        }
Exemplo n.º 16
0
        public void SetStream(MaterialShaderStage stage, string stream, MaterialStreamType streamType, ShaderSource classSource)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // Blend stream is not part of the stream used
            if (stream != MaterialBlendLayer.BlendStream)
            {
                GetContextPerStage(stage).Streams.Add(stream);
            }

            string channel;

            switch (streamType)
            {
            case MaterialStreamType.Float:
                channel = "r";
                break;

            case MaterialStreamType.Float2:
                channel = "rg";
                break;

            case MaterialStreamType.Float3:
                channel = "rgb";
                break;

            case MaterialStreamType.Float4:
                channel = "rgba";
                break;

            default:
                throw new NotSupportedException("StreamType [{0}] is not supported".ToFormat(streamType));
            }

            var mixin = new ShaderMixinSource();

            mixin.Mixins.Add(new ShaderClassSource("MaterialSurfaceSetStreamFromComputeColor", stream, channel));
            mixin.AddComposition("computeColorSource", classSource);

            GetContextPerStage(stage).ShaderSources.Add(mixin);
        }
Exemplo n.º 17
0
        public void SetStream(MaterialShaderStage stage, string stream, IComputeNode computeNode, ObjectParameterKey <Texture> defaultTexturingKey, ParameterKey defaultValueKey, Color?defaultTextureValue)
        {
            if (defaultValueKey == null)
            {
                throw new ArgumentNullException(nameof(defaultValueKey));
            }
            if (computeNode == null)
            {
                return;
            }

            var streamType = MaterialStreamType.Float;

            if (defaultValueKey.PropertyType == typeof(Vector4) || defaultValueKey.PropertyType == typeof(Color4))
            {
                streamType = MaterialStreamType.Float4;
            }
            else if (defaultValueKey.PropertyType == typeof(Vector3) || defaultValueKey.PropertyType == typeof(Color3))
            {
                streamType = MaterialStreamType.Float3;
            }
            else if (defaultValueKey.PropertyType == typeof(Vector2) || defaultValueKey.PropertyType == typeof(Half2))
            {
                streamType = MaterialStreamType.Float2;
            }
            else if (defaultValueKey.PropertyType == typeof(float))
            {
                streamType = MaterialStreamType.Float;
            }
            else
            {
                throw new NotSupportedException("ParameterKey type [{0}] is not supported by SetStream".ToFormat(defaultValueKey.PropertyType));
            }

            var classSource = computeNode.GenerateShaderSource(Context, new MaterialComputeColorKeys(defaultTexturingKey, defaultValueKey, defaultTextureValue));

            SetStream(stage, stream, streamType, classSource);
        }
 public void SetStreamBlend(MaterialShaderStage stage, IComputeScalar blendMap)
 {
     SetStream(stage, MaterialBlendLayer.BlendStream, blendMap, MaterialKeys.BlendMap, MaterialKeys.BlendValue, null);
 }
Exemplo n.º 19
0
 public void AddFinalCallback(MaterialShaderStage stage, MaterialGeneratorCallback callback)
 {
     finalCallbacks[stage].Add(callback);
 }
Exemplo n.º 20
0
 public void AddStreamInitializer(MaterialShaderStage stage, string streamInitilizerSource)
 {
     Current.StreamInitializers[stage].Add(streamInitilizerSource);
 }
 private void AddDiffuseSpecularAlphaBlendColor(MaterialShaderStage stage, MaterialGeneratorContext context)
 {
     context.AddShaderSource(MaterialShaderStage.Pixel, new ShaderClassSource("MaterialSurfaceDiffuseSpecularAlphaBlendColor"));
 }
Exemplo n.º 22
0
 public MaterialBlendLayerPerStageContext GetContextPerStage(MaterialShaderStage stage)
 {
     return(ContextPerStage[stage]);
 }
 private void AddDiscardFromLuminance(MaterialShaderStage stage, MaterialGeneratorContext context)
 {
     context.AddShaderSource(MaterialShaderStage.Pixel, new ShaderClassSource("MaterialSurfaceTransparentAlphaDiscard"));
 }
 public ShaderSource ComputeShaderSource(MaterialShaderStage stage)
 {
     var stageContext = GetContextPerStage(stage);
     return stageContext.ComputeShaderSource();
 }
Exemplo n.º 25
0
 public ShaderSource GenerateStreamInitializers(MaterialShaderStage stage)
 {
     return(currentLayerContext.GenerateStreamInitializers(stage));
 }
 private void AddMaterialSurfaceTransmittanceShading(MaterialShaderStage stage, MaterialGeneratorContext context)
 {
     context.AddShaderSource(stage, new ShaderClassSource("MaterialSurfaceTransmittanceShading"));
 }
Exemplo n.º 27
0
 public bool HasShaderSources(MaterialShaderStage stage)
 {
     return(currentLayerContext.GetContextPerStage(stage).ShaderSources.Count > 0);
 }
Exemplo n.º 28
0
 public ShaderSource ComputeShaderSource(MaterialShaderStage stage)
 {
     return(currentLayerContext.ComputeShaderSource(stage));
 }
        public void SetStream(MaterialShaderStage stage, string stream, MaterialStreamType streamType, ShaderSource classSource)
        {
            if (stream == null) throw new ArgumentNullException(nameof(stream));

            // Blend stream is not part of the stream used
            if (stream != MaterialBlendLayer.BlendStream)
            {
                GetContextPerStage(stage).Streams.Add(stream);
            }

            string channel;
            switch (streamType)
            {
                case MaterialStreamType.Float:
                    channel = "r";
                    break;
                case MaterialStreamType.Float2:
                    channel = "rg";
                    break;
                case MaterialStreamType.Float3:
                    channel = "rgb";
                    break;
                case MaterialStreamType.Float4:
                    channel = "rgba";
                    break;
                default:
                    throw new NotSupportedException("StreamType [{0}] is not supported".ToFormat(streamType));
            }

            var mixin = new ShaderMixinSource();
            mixin.Mixins.Add(new ShaderClassSource("MaterialSurfaceSetStreamFromComputeColor", stream, channel));
            mixin.AddComposition("computeColorSource", classSource);

            GetContextPerStage(stage).ShaderSources.Add(mixin);
        }
Exemplo n.º 30
0
        private void Generate(MaterialShaderStage stage, MaterialGeneratorContext context)
        {
            if (!context.HasSurfaceShaders(stage))
            {
                return;
            }

            // Blend setup for this layer
            context.SetStream(stage, BlendStream, BlendMap, MaterialKeys.BlendMap, MaterialKeys.BlendValue);

            // Generate a dynamic shader name
            // Create a mixin
            var shaderMixinSource = new ShaderMixinSource();
            shaderMixinSource.Mixins.Add(new ShaderClassSource("MaterialSurfaceStreamsBlend"));

            // Add all streams
            foreach (var stream in context.Streams[stage])
            {
                shaderMixinSource.AddCompositionToArray("blends", context.GetStreamBlendShaderSource(stream));
            }

            var materialBlendLayerMixin = context.GenerateSurfaceShader(stage);

            // Add the shader to the mixin
            shaderMixinSource.AddComposition("layer", materialBlendLayerMixin);

            context.ResetSurfaceShaders(stage);
            context.AddSurfaceShader(stage, shaderMixinSource);
        }
Exemplo n.º 31
0
 public void SetStreamBlend(MaterialShaderStage stage, IComputeScalar blendMap)
 {
     SetStream(stage, MaterialBlendLayer.BlendStream, blendMap, MaterialKeys.BlendMap, MaterialKeys.BlendValue, null);
 }
 public MaterialBlendLayerPerStageContext GetContextPerStage(MaterialShaderStage stage)
 {
     return ContextPerStage[stage];
 }
Exemplo n.º 33
0
 public List <ShaderSource> GetSurfaceShaders(MaterialShaderStage stage)
 {
     return(SurfaceShaders[stage]);
 }
        public void AddAdjacentEdgeAverageMacros(MaterialShaderStage stage, MaterialGeneratorContext context)
        {
            var tessellationShader = context.Parameters.Get(MaterialKeys.TessellationShader) as ShaderMixinSource;
            if(tessellationShader == null)
                return;

            tessellationShader.Macros.Add(new ShaderMacro("InputControlPointCount", 12));
        }
Exemplo n.º 35
0
 public void AddStreamInitializer(MaterialShaderStage stage, string streamInitilizerSource)
 {
     currentLayerContext.GetContextPerStage(stage).StreamInitializers.Add(streamInitilizerSource);
 }
 private void AddDiffuseSpecularAlphaBlendColor(MaterialShaderStage stage, MaterialGeneratorContext context)
 {
     context.AddSurfaceShader(MaterialShaderStage.Pixel, new ShaderClassSource("MaterialSurfaceDiffuseSpecularAlphaBlendColor"));
 }
Exemplo n.º 37
0
 public void SetStream(MaterialShaderStage stage, string stream, IComputeNode computeNode, ObjectParameterKey <Texture> defaultTexturingKey, ParameterKey defaultValueKey, Color?defaultTextureValue = null)
 {
     currentLayerContext.SetStream(stage, stream, computeNode, defaultTexturingKey, defaultValueKey, defaultTextureValue);
 }
        public ShaderSource GenerateStreamInitializers(MaterialShaderStage stage)
        {
            // Early exit if nothing to do
            var stageContext = GetContextPerStage(stage);
            if (stageContext.StreamInitializers.Count == 0 && stageContext.ShaderSources.Count == 0 && stage != MaterialShaderStage.Pixel)
            {
                return null;
            }

            var mixin = new ShaderMixinSource();

            // the basic streams contained by every materials
            mixin.Mixins.Add(new ShaderClassSource("MaterialStream"));

            // the streams coming from the material layers
            foreach (var streamInitializer in stageContext.StreamInitializers)
            {
                mixin.Mixins.Add(streamInitializer);
            }
            stageContext.StreamInitializers.Clear();

            // the streams specific to a stage
            // TODO: Use StreamInitializers instead of streams initializers hardcoded in MaterialPixelShadingStream.ResetStream
            if (stage == MaterialShaderStage.Pixel)
                mixin.Mixins.Add("MaterialPixelShadingStream");

            return mixin;
        }
Exemplo n.º 39
0
 public void SetStream(MaterialShaderStage stage, string stream, MaterialStreamType streamType, ShaderSource shaderSource)
 {
     currentLayerContext.SetStream(stage, stream, streamType, shaderSource);
 }
 private void AddDiscardFromLuminance(MaterialShaderStage stage, MaterialGeneratorContext context)
 {
     context.AddSurfaceShader(MaterialShaderStage.Pixel, new ShaderClassSource("MaterialSurfaceTransparentAlphaDiscard"));
 }
Exemplo n.º 41
0
        public ShaderSource ComputeShaderSource(MaterialShaderStage stage)
        {
            var stageContext = GetContextPerStage(stage);

            return(stageContext.ComputeShaderSource());
        }