private static EffectVariable RetrieveVariableByName(EffectConstantBuffer constantBuffer, string name, bool throwIfNotFound) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (constantBuffer == null) { throw new ArgumentNullException("constantBuffer"); } EffectVariable variable = constantBuffer.GetMemberByName(name); if (!variable.IsValid && throwIfNotFound) { throw new ArgumentException(string.Format("Can't retrieve variable with given name {0}.", name), "name"); } return(variable); }
public void Attach(Device hostDevice, PointD2D hostSize) { if (hostDevice == null) throw new ArgumentNullException(nameof(hostDevice)); _hostDevice = hostDevice; _hostSize = hostSize; Device device = _hostDevice; using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Altaxo.CompiledShaders.Effects.Lighting.cso")) { if (null == stream) throw new InvalidOperationException(string.Format("Compiled shader resource not found: {0}", "Altaxo.CompiledShaders.Effects.Lighting.cso")); using (var shaderBytes = ShaderBytecode.FromStream(stream)) { _lightingEffect = new Effect(device, shaderBytes); } } int i; for (i = 0; i < _layoutNames.Length; ++i) { string techniqueName = "Shade_" + _layoutNames[i]; _renderLayouts[i].technique = this._lightingEffect.GetTechniqueByName(techniqueName); _renderLayouts[i].pass = _renderLayouts[i].technique.GetPassByIndex(0); if (null == _renderLayouts[i].technique || !_renderLayouts[i].technique.IsValid) throw new InvalidProgramException(string.Format("Technique {0} was not found or is invalid", techniqueName)); if (null == _renderLayouts[i].pass || !_renderLayouts[i].pass.IsValid) throw new InvalidProgramException(string.Format("Pass[0] of technique {0} was not found or is invalid", techniqueName)); } i = 0; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0) }); i = 1; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); i = 2; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0) }); i = 3; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0) }); i = 4; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 32, 0) }); i = 5; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0) }); i = 6; _renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0) }); // Create Constant Buffers //_constantBuffer = new Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); //_constantBufferForColor = new Buffer(device, Utilities.SizeOf<Vector4>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); //_constantBufferForSixPlanes = new Buffer(device, Utilities.SizeOf<Vector4>() * 6, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); // View transformation variables _cbViewTransformation = this._lightingEffect.GetConstantBufferByName("cbViewTransformation"); _evWorldViewProj = _cbViewTransformation.GetMemberByName("WorldViewProj").AsMatrix(); _evEyePosition = _cbViewTransformation.GetMemberByName("EyePosition").AsVector(); _cbMaterial = this._lightingEffect.GetConstantBufferByName("cbMaterial"); _evMaterialDiffuseColor = _cbMaterial.GetMemberByName("MaterialDiffuseColor").AsVector(); _evMaterialSpecularExponent = _cbMaterial.GetMemberByName("MaterialSpecularExponent").AsScalar(); _evMaterialSpecularExponent.Set(4.0f); _evMaterialSpecularIntensity = _cbMaterial.GetMemberByName("MaterialSpecularIntensity").AsScalar(); _evMaterialSpecularIntensity.Set(1.0f); _evMaterialDiffuseIntensity = _cbMaterial.GetMemberByName("MaterialDiffuseIntensity").AsScalar(); _evMaterialMetalnessValue = _cbMaterial.GetMemberByName("MaterialMetalnessValue").AsScalar(); _evMaterialMetalnessValue.Set(0.75f); // Color providers BindTextureFor1DColorProviders(); // Clip plane variables _cbClipPlanes = this._lightingEffect.GetConstantBufferByName("cbClipPlanes"); for (i = 0; i < 6; ++i) { _evClipPlanes[i] = _cbClipPlanes.GetMemberByName("ClipPlane" + i.ToString(System.Globalization.CultureInfo.InvariantCulture)).AsVector(); } // Lighting variables _lighting.Initialize(_lightingEffect); _lighting.SetDefaultLighting(); // -------------------- if (_drawing != null) { BringDrawingIntoBuffers(_drawing); } if (null != _markerGeometry) { BringMarkerGeometryIntoDeviceBuffers(_markerGeometry); } if (null != _overlayGeometry) { BringOverlayGeometryIntoDeviceBuffers(_overlayGeometry); } }
private static EffectVariable RetrieveVariableByName(EffectConstantBuffer constantBuffer, string name, bool throwIfNotFound) { if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); if (constantBuffer == null) throw new ArgumentNullException("constantBuffer"); EffectVariable variable = constantBuffer.GetMemberByName(name); if (!variable.IsValid && throwIfNotFound) throw new ArgumentException(string.Format("Can't retrieve variable with given name {0}.", name), "name"); return variable; }
/// <summary> /// Attaches the scene to the device specified in <paramref name="device"/>, and allocate resources specific to that device. /// </summary> /// <param name="device">The device to attach to.</param> /// <param name="hostSize">The size of the device.</param> public void Attach(Device device, PointD2D hostSize) { if (null != _cachedDevice) { throw new InvalidOperationException("Try to attach to device without deattach former device!"); } _cachedDevice = device ?? throw new ArgumentNullException(nameof(device)); _hostSize = hostSize; using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Altaxo.CompiledShaders.Effects.Lighting.cso")) { if (null == stream) { throw new InvalidOperationException(string.Format("Compiled shader resource not found: {0}", "Altaxo.CompiledShaders.Effects.Lighting.cso")); } using (var shaderBytes = ShaderBytecode.FromStream(stream)) { _lightingEffect = new Effect(device, shaderBytes); } } for (int i = 0; i < _layoutNames.Length; ++i) { string techniqueName = "Shade_" + _layoutNames[i]; var technique = _lightingEffect.GetTechniqueByName(techniqueName); if (null == technique || !technique.IsValid) { throw new InvalidProgramException(string.Format("Technique {0} was not found or is invalid", techniqueName)); } var pass = technique.GetPassByIndex(0); if (null == pass || !pass.IsValid) { throw new InvalidProgramException(string.Format("Pass[0] of technique {0} was not found or is invalid", techniqueName)); } InputLayout inputLayout; switch (i) { case 0: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0) }); break; case 1: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); break; case 2: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0) }); break; case 3: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0) }); break; case 4: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 32, 0) }); break; case 5: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0) }); break; case 6: inputLayout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0) }); break; default: throw new NotImplementedException(); } _renderLayouts[i] = new RenderLayout(technique, pass, inputLayout); } // Create Constant Buffers //_constantBuffer = new Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); //_constantBufferForColor = new Buffer(device, Utilities.SizeOf<Vector4>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); //_constantBufferForSixPlanes = new Buffer(device, Utilities.SizeOf<Vector4>() * 6, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); // View transformation variables _cbViewTransformation = _lightingEffect.GetConstantBufferByName("cbViewTransformation"); _evWorldViewProj = _cbViewTransformation.GetMemberByName("WorldViewProj").AsMatrix(); _evEyePosition = _cbViewTransformation.GetMemberByName("EyePosition").AsVector(); _cbMaterial = _lightingEffect.GetConstantBufferByName("cbMaterial"); _evMaterialDiffuseColor = _cbMaterial.GetMemberByName("MaterialDiffuseColor").AsVector(); _evMaterialSpecularExponent = _cbMaterial.GetMemberByName("MaterialSpecularExponent").AsScalar(); _evMaterialSpecularExponent.Set(4.0f); _evMaterialSpecularIntensity = _cbMaterial.GetMemberByName("MaterialSpecularIntensity").AsScalar(); _evMaterialSpecularIntensity.Set(1.0f); _evMaterialDiffuseIntensity = _cbMaterial.GetMemberByName("MaterialDiffuseIntensity").AsScalar(); _evMaterialMetalnessValue = _cbMaterial.GetMemberByName("MaterialMetalnessValue").AsScalar(); _evMaterialMetalnessValue.Set(0.75f); // Color providers BindTextureFor1DColorProviders(); // Clip plane variables _cbClipPlanes = _lightingEffect.GetConstantBufferByName("cbClipPlanes"); for (int i = 0; i < 6; ++i) { _evClipPlanes[i] = _cbClipPlanes.GetMemberByName("ClipPlane" + i.ToString(System.Globalization.CultureInfo.InvariantCulture)).AsVector(); } // Lighting variables _lighting = new Lighting(_lightingEffect); _lighting.SetDefaultLighting(); // -------------------- now draw the scene again with the new attached device -------------- if (null != _altaxoCamera && null != _altaxoLightSettings) { if (_altaxoDrawingGeometry != null) { BringDrawingIntoBuffers(_altaxoDrawingGeometry); } if (null != _altaxoMarkerGeometry) { BringMarkerGeometryIntoDeviceBuffers(_altaxoMarkerGeometry); } if (null != _altaxoOverlayGeometry) { BringOverlayGeometryIntoDeviceBuffers(_altaxoOverlayGeometry); } } }
public Lighting(Effect effect) { _singleLights = new SingleLight[4]; _cbLighting = effect.GetConstantBufferByName("cbLights"); HemisphericLightColorBelow = _cbLighting.GetMemberByName("HemisphericLightColorBelow").AsVector(); HemisphericLightColorAbove = _cbLighting.GetMemberByName("HemisphericLightColorAbove").AsVector(); HemisphericLightBelowToAboveVector = _cbLighting.GetMemberByName("HemisphericLightBelowToAboveVector").AsVector(); LightPosX = _cbLighting.GetMemberByName("LightPosX").AsVector(); LightPosY = _cbLighting.GetMemberByName("LightPosY").AsVector(); LightPosZ = _cbLighting.GetMemberByName("LightPosZ").AsVector(); LightDirX = _cbLighting.GetMemberByName("LightDirX").AsVector(); LightDirY = _cbLighting.GetMemberByName("LightDirY").AsVector(); LightDirZ = _cbLighting.GetMemberByName("LightDirZ").AsVector(); LightColorR = _cbLighting.GetMemberByName("LightColorR").AsVector(); LightColorG = _cbLighting.GetMemberByName("LightColorG").AsVector(); LightColorB = _cbLighting.GetMemberByName("LightColorB").AsVector(); LightRangeRcp = _cbLighting.GetMemberByName("LightRangeRcp").AsVector(); CapsuleLen = _cbLighting.GetMemberByName("CapsuleLen").AsVector(); SpotCosOuterCone = _cbLighting.GetMemberByName("SpotCosOuterCone").AsVector(); SpotCosInnerConeRcp = _cbLighting.GetMemberByName("SpotCosInnerConeRcp").AsVector(); }
public void Initialize(Effect effect) { _singleLights = new SingleLight[4]; _cbLighting = effect.GetConstantBufferByName("cbLights"); HemisphericLightColorBelow = _cbLighting.GetMemberByName("HemisphericLightColorBelow").AsVector(); HemisphericLightColorAbove = _cbLighting.GetMemberByName("HemisphericLightColorAbove").AsVector(); HemisphericLightBelowToAboveVector = _cbLighting.GetMemberByName("HemisphericLightBelowToAboveVector").AsVector(); LightPosX = _cbLighting.GetMemberByName("LightPosX").AsVector(); LightPosY = _cbLighting.GetMemberByName("LightPosY").AsVector(); LightPosZ = _cbLighting.GetMemberByName("LightPosZ").AsVector(); LightDirX = _cbLighting.GetMemberByName("LightDirX").AsVector(); LightDirY = _cbLighting.GetMemberByName("LightDirY").AsVector(); LightDirZ = _cbLighting.GetMemberByName("LightDirZ").AsVector(); LightColorR = _cbLighting.GetMemberByName("LightColorR").AsVector(); LightColorG = _cbLighting.GetMemberByName("LightColorG").AsVector(); LightColorB = _cbLighting.GetMemberByName("LightColorB").AsVector(); LightRangeRcp = _cbLighting.GetMemberByName("LightRangeRcp").AsVector(); CapsuleLen = _cbLighting.GetMemberByName("CapsuleLen").AsVector(); SpotCosOuterCone = _cbLighting.GetMemberByName("SpotCosOuterCone").AsVector(); SpotCosInnerConeRcp = _cbLighting.GetMemberByName("SpotCosInnerConeRcp").AsVector(); }