public override void SetupInitialization(ShaderInitializer initializer) { var services = initializer.Services; var technique = initializer.Technique; var scene = services.GetService <IEntityProvider>(); var data = from e in scene.Entities where e.ContainsComponent <MaterialComponent>() let techniqueComponents = e.Components.OfType <ITechniqueComponent>() from techniqueRange in techniqueComponents from t in techniqueRange.Techniques where t.Name == technique.Name group e.Id by e.GetComponent <MaterialComponent>() into materialEntities select new { Material = materialEntities.Key, Entities = materialEntities.ToArray() }; foreach (var kvp in data) { foreach (var entityId in kvp.Entities) { InitializerParameters parameters = new InitializerParameters(entityId, technique, services, InstanceSelector); initializer.Initialize(this, (IMaterial)kvp.Material, parameters); } } }
public override void Initialize(DirectXDevice device, Entity source, InitializerParameters parameters) { base.Initialize(device, source, parameters); if (!parameters.Technique.UsesProceduralTextures) { InitializeTextures(source, parameters); } }
public override void SetupInitialization(ShaderInitializer initializer) { var services = initializer.Services; var settings = services.GetService <IGraphicsDeviceService>(); InitializerParameters parameters = new InitializerParameters(-1, initializer.Technique, services, StaticSelector); initializer.Initialize(this, settings, parameters); }
public override void SetupInitialization(ShaderInitializer initializer) { var services = initializer.Services; var technique = initializer.Technique; InitializerParameters parameters = new InitializerParameters(-1, technique, services, StaticSelector); var data = from metaData in technique.MetaData where metaData.Key == Param.Properties.LightId select Int32.Parse(metaData.Value); ILightService lightService = services.GetService <ILightService>(); foreach (int lightId in data) { initializer.Initialize(this, lightService[lightId], parameters); } }
private void InitializeConstantBuffers(TSource source, InitializerParameters parameters) { var technique = parameters.Technique; var referenceTable = from shaderObject in technique.Mapping.Shaders from cb in shaderObject.ConstantBuffers where parameters.Selector(cb) && ValidateConstantBuffer(cb) select cb; string effectName = technique.Name; foreach (var cbDesc in referenceTable) { if (!technique[cbDesc.ShaderType].HasConstantBuffer(cbDesc.Index, effectName, parameters.EntityId)) { technique[cbDesc.ShaderType].AddConstantBuffer(parameters.EntityId, new ConstantBuffer(device, cbDesc, effectName)); } ConstantBuffer cb = technique[cbDesc.ShaderType].GetConstantBuffer(cbDesc.Index, effectName, parameters.EntityId); var validReferences = from kvp in cbDesc.References let reference = kvp.Value where AcceptedReferences.Any(r => string.Equals(r.Value, reference.Value)) select kvp; foreach (var kvp in validReferences) { var reference = kvp.Value; if (cbDesc.UpdateFrequency != UpdateType.InstanceFrame && cbDesc.IsParsed(effectName, reference)) { continue; } var effectParameters = CreateParameter(cbDesc, source, kvp.Key, reference.Value, parameters); foreach (IParameter parameter in effectParameters) { cb.AddParameter(kvp.Key, parameter); } cbDesc.MarkAsParsed(effectName, reference); } } }
public override void SetupInitialization(ShaderInitializer initializer) { var services = initializer.Services; var technique = initializer.Technique; var scene = services.GetService <IEntityProvider>(); var data = from e in scene.Entities let techniqueComponents = e.Components.OfType <ITechniqueComponent>() from cTechnique in techniqueComponents from t in cTechnique.Techniques where t.Name == technique.Name select e.Id; foreach (long entityId in data) { InitializerParameters parameters = new InitializerParameters(entityId, technique, services, InstanceSelector); initializer.Initialize(this, scene.SelectEntity(entityId), parameters); } }
public override void SetupInitialization(ShaderInitializer initializer) { var services = initializer.Services; var scene = services.GetService <IEntityProvider>(); var technique = initializer.Technique; InitializerParameters parameters = new InitializerParameters(-1, technique, services, StaticSelector); var data = from metaData in technique.MetaData where metaData.Key == Param.Properties.CameraId select Int32.Parse(metaData.Value); var cameras = (from e in scene.Entities where e.ContainsComponent <CameraComponent>() let cameraComponent = e.GetComponent <CameraComponent>() select new { Entity = e, CameraComponent = cameraComponent }).ToDictionary(kvp => kvp.CameraComponent.Index, kvp => kvp.Entity); foreach (int cameraId in data) { initializer.Initialize(this, cameras[cameraId], parameters); } }
private void InitializeTextures(IEntity source, InitializerParameters parameters) { var technique = parameters.Technique; var referenceTable = from shaderObject in technique.Mapping.Shaders from textures in shaderObject.TextureReferences select new { Shader = shaderObject, TextureDesc = textures }; foreach (var row in referenceTable) { var textureDesc = row.TextureDesc; if (!technique[textureDesc.ShaderType].HasTexture(textureDesc.Index)) { Texture texture = FindResource(source.Components, row.Shader.Name, row.TextureDesc.Texture); if (texture == null) { LogEvent.Engine.Error("[{0}] is missing a component containing a [{1}] texture and tagged with [{2}].", source.Name, textureDesc.Texture, row.Shader.Name); throw new InvalidOperationException("No suitable textures found."); } technique[textureDesc.ShaderType].AddTexture(new TextureMapping(texture, textureDesc)); } } }
protected abstract IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, TSource source, int parameterIndex, string reference, InitializerParameters initializerParameters);
public virtual void Initialize(DirectXDevice device, TSource source, InitializerParameters parameters) { LogEvent.Engine.Info("[{0}, {1}] in {2}", GetType().Name, parameters.EntityId, parameters.Technique.Name); this.device = device; InitializeConstantBuffers(source, parameters); }
protected override IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, Entity entity, int parameterIndex, string reference, InitializerParameters initializerParameters) { if (!ReferenceActions.ContainsKey(reference)) { throw new InvalidOperationException(string.Format("[{0}]: Entity parameter not valid", reference)); } return(ReferenceActions[reference](parameterIndex, entity, initializerParameters)); }
public void Initialize <TInitializer, TSource>(TInitializer initializer, TSource source, InitializerParameters parameters) where TInitializer : Initializer <TSource> { var initializerStatus = InitializerMap[technique.Name]; Type initializerType = typeof(TInitializer); bool isInstanceInitializer = InstanceInitializers.Contains(initializerType); if (!isInstanceInitializer && initializerStatus.ContainsKey(initializerType) && initializerStatus[initializerType]) { LogEvent.Engine.Warning("Attempted to reinitialize {0}", typeof(TInitializer).Name); return; } initializer.Initialize(Device, source, parameters); if (!isInstanceInitializer && initializerStatus.ContainsKey(initializerType)) { initializerStatus[typeof(TInitializer)] = true; } }
protected override IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, IGraphicsDeviceService source, int parameterIndex, string reference, InitializerParameters initializerParameters) { if (!ReferenceActions.ContainsKey(reference)) { throw new ArgumentException(string.Format("{0}: {1} not valid.", GetType().Name, reference)); } return(ReferenceActions[reference](parameterIndex, source, initializerParameters)); }
private static IEnumerable <IParameter> ComputeBlurOffsetsAndWeights(int index, Entity entity, InitializerParameters parameters) { bool isHorizontal = parameters.Technique.Name == "PostProcess.GaussianBlurH"; var cPostProcess = entity.GetComponent <PostProcessComponent>(); float texelWidth; float texelHeight; var cBlur = entity.GetComponent <BlurComponent>(); if (isHorizontal) { texelWidth = 1.0f / (cPostProcess.OutputWidth * cBlur.DownScale); texelHeight = 0; } else { texelWidth = 0; texelHeight = 1.0f / (cPostProcess.OutputHeight * cBlur.DownScale); } Vector2[] offsets; float[] weights; Blur.ComputeParameters(texelWidth, texelHeight, out weights, out offsets); Vector4[] data = new Vector4[Blur.SampleCount]; for (int i = 0; i < data.Length; i++) { data[i] = new Vector4(offsets[i].X, offsets[i].Y, weights[i], 0); } return(new[] { new Float4ArrayParameter(index, data.Length, Param.Floats.BlurOffsetsAndWeights, () => data) }); }
static IEnumerable <IParameter> Convert(int parameterIndex, IMaterial material, InitializerParameters parameters) { List <IParameter> parameterCollection = new List <IParameter> { new Float4Parameter(parameterIndex, Param.Material.Coefficients, () => new Vector4(material.AmbientCoefficient, material.DiffuseCoefficient, material.SpecularCoefficient, material.SpecularPower)), new Float4Parameter(parameterIndex, Param.Material.Ambient, () => material.Ambient), new Float4Parameter(parameterIndex, Param.Material.Diffuse, () => material.Diffuse), new Float4Parameter(parameterIndex, Param.Material.Specular, () => material.Specular) }; return(parameterCollection); }
protected override IEnumerable <IParameter> CreateParameter(ConstantBufferDescription cbParent, Entity entity, int parameterIndex, string reference, InitializerParameters initializerParameters) { string cameraIdValue = cbParent.Get(Param.Properties.CameraId); int cameraId = Int32.Parse(cameraIdValue); if (!ReferenceActions.ContainsKey(reference)) { throw new InvalidOperationException(string.Format("[{0}]: Camera parameter not valid", reference)); } return(ReferenceActions[reference](parameterIndex, entity, initializerParameters)); }