public LitShader(int lightsCount, bool useEmissionColor, bool useVertexColor, bool useTexture, bool useFog, bool useAlphaThreshold, int maxInstancesCount = 1) : base(new StreamReader(Storage.OpenFile("app:Lit.vsh", OpenFileMode.Read)).ReadToEnd(), new StreamReader(Storage.OpenFile("app:Lit.psh", OpenFileMode.Read)).ReadToEnd(), PrepareShaderMacros(lightsCount, useEmissionColor, useVertexColor, useTexture, useFog, useAlphaThreshold, maxInstancesCount)) { if (lightsCount < 0 || lightsCount > 3) { throw new ArgumentException("lightsCount"); } if (maxInstancesCount < 0 || maxInstancesCount > 32) { throw new ArgumentException("maxInstancesCount"); } m_worldMatrixParameter = GetParameter("u_worldMatrix", allowNull: true); m_worldViewMatrixParameter = GetParameter("u_worldViewMatrix", allowNull: true); m_worldViewProjectionMatrixParameter = GetParameter("u_worldViewProjectionMatrix", allowNull: true); m_textureParameter = GetParameter("u_texture", allowNull: true); m_samplerStateParameter = GetParameter("u_samplerState", allowNull: true); m_materialColorParameter = GetParameter("u_materialColor", allowNull: true); m_emissionColorParameter = GetParameter("u_emissionColor", allowNull: true); m_alphaThresholdParameter = GetParameter("u_alphaThreshold", allowNull: true); m_ambientLightColorParameter = GetParameter("u_ambientLightColor", allowNull: true); m_diffuseLightColor1Parameter = GetParameter("u_diffuseLightColor1", allowNull: true); m_directionToLight1Parameter = GetParameter("u_directionToLight1", allowNull: true); m_diffuseLightColor2Parameter = GetParameter("u_diffuseLightColor2", allowNull: true); m_directionToLight2Parameter = GetParameter("u_directionToLight2", allowNull: true); m_diffuseLightColor3Parameter = GetParameter("u_diffuseLightColor3", allowNull: true); m_directionToLight3Parameter = GetParameter("u_directionToLight3", allowNull: true); m_fogStartParameter = GetParameter("u_fogStart", allowNull: true); m_fogLengthParameter = GetParameter("u_fogLength", allowNull: true); m_fogColorParameter = GetParameter("u_fogColor", allowNull: true); Transforms = new ShaderTransforms(maxInstancesCount); m_lightsCount = lightsCount; m_instancesCount = 1; m_useFog = useFog; MaterialColor = Vector4.One; if (useEmissionColor) { EmissionColor = Vector4.Zero; } if (lightsCount >= 1) { AmbientLightColor = new Vector3(0.2f); DiffuseLightColor1 = new Vector3(0.8f); LightDirection1 = Vector3.Normalize(new Vector3(1f, -1f, 1f)); } if (lightsCount >= 2) { DiffuseLightColor2 = new Vector3(0.4f); LightDirection2 = Vector3.Normalize(new Vector3(-1f, -0.5f, -0.25f)); } if (lightsCount >= 3) { DiffuseLightColor3 = new Vector3(0.2f); LightDirection3 = Vector3.Normalize(new Vector3(0f, 1f, 0f)); } if (useFog) { FogLength = 100f; } }
public UnlitShader(bool useVertexColor, bool useTexture, bool useAlphaThreshold) : base(new StreamReader(Storage.OpenFile("app:Unlit.vsh", OpenFileMode.Read)).ReadToEnd(), new StreamReader(Storage.OpenFile("app:Unlit.psh", OpenFileMode.Read)).ReadToEnd(), PrepareShaderMacros(useVertexColor, useTexture, useAlphaThreshold)) { m_worldViewProjectionMatrixParameter = GetParameter("u_worldViewProjectionMatrix", allowNull: true); m_textureParameter = GetParameter("u_texture", allowNull: true); m_samplerStateParameter = GetParameter("u_samplerState", allowNull: true); m_colorParameter = GetParameter("u_color", allowNull: true); m_alphaThresholdParameter = GetParameter("u_alphaThreshold", allowNull: true); Transforms = new ShaderTransforms(1); Color = Vector4.One; }
public static void ApplyShaderAndBuffers(Shader shader, VertexDeclaration vertexDeclaration, IntPtr vertexOffset, int arrayBuffer, int?elementArrayBuffer) { shader.PrepareForDrawing(); BindBuffer(All.ArrayBuffer, arrayBuffer); if (elementArrayBuffer.HasValue) { BindBuffer(All.ElementArrayBuffer, elementArrayBuffer.Value); } UseProgram(shader.m_program); if (shader != m_lastShader || vertexOffset != m_lastVertexOffset || arrayBuffer != m_lastArrayBuffer || vertexDeclaration.m_elements != m_lastVertexDeclaration.m_elements) { Shader.VertexAttributeData[] vertexAttribData = shader.GetVertexAttribData(vertexDeclaration); for (int i = 0; i < vertexAttribData.Length; i++) { if (vertexAttribData[i].Size != 0) { GL.VertexAttribPointer(i, vertexAttribData[i].Size, vertexAttribData[i].Type, vertexAttribData[i].Normalize, vertexDeclaration.VertexStride, vertexOffset + vertexAttribData[i].Offset); VertexAttribArray(i, enable: true); } else { VertexAttribArray(i, enable: false); } } m_lastShader = shader; m_lastVertexDeclaration = vertexDeclaration; m_lastVertexOffset = vertexOffset; m_lastArrayBuffer = arrayBuffer; } int num = 0; int num2 = 0; ShaderParameter shaderParameter; while (true) { if (num2 >= shader.m_parameters.Length) { return; } shaderParameter = shader.m_parameters[num2]; if (shaderParameter.IsChanged) { switch (shaderParameter.Type) { case ShaderParameterType.Float: GL.Uniform1(shaderParameter.Location, shaderParameter.Count, shaderParameter.Value); shaderParameter.IsChanged = false; break; case ShaderParameterType.Vector2: GL.Uniform2(shaderParameter.Location, shaderParameter.Count, shaderParameter.Value); shaderParameter.IsChanged = false; break; case ShaderParameterType.Vector3: GL.Uniform3(shaderParameter.Location, shaderParameter.Count, shaderParameter.Value); shaderParameter.IsChanged = false; break; case ShaderParameterType.Vector4: GL.Uniform4(shaderParameter.Location, shaderParameter.Count, shaderParameter.Value); shaderParameter.IsChanged = false; break; case ShaderParameterType.Matrix: GL.UniformMatrix4(shaderParameter.Location, shaderParameter.Count, transpose: false, shaderParameter.Value); shaderParameter.IsChanged = false; break; default: throw new InvalidOperationException("Unsupported shader parameter type."); case ShaderParameterType.Texture2D: case ShaderParameterType.Sampler2D: break; } } if (shaderParameter.Type == ShaderParameterType.Texture2D) { if (num >= 8) { throw new InvalidOperationException("Too many simultaneous textures."); } ActiveTexture((All)(33984 + num)); if (shaderParameter.IsChanged) { GL.Uniform1(shaderParameter.Location, num); } ShaderParameter obj = shader.m_parameters[num2 + 1]; Texture2D texture2D = (Texture2D)shaderParameter.Resource; SamplerState samplerState = (SamplerState)obj.Resource; if (texture2D != null) { if (samplerState == null) { break; } if (m_activeTexturesByUnit[num] != texture2D.m_texture) { BindTexture(All.Texture2D, texture2D.m_texture, forceBind: true); } if (!m_textureSamplerStates.TryGetValue(texture2D.m_texture, out SamplerState value) || value != samplerState) { BindTexture(All.Texture2D, texture2D.m_texture, forceBind: false); if (GL_EXT_texture_filter_anisotropic) { GL.TexParameter(All.Texture2D, All.TextureMaxAnisotropyExt, (samplerState.FilterMode == TextureFilterMode.Anisotropic) ? ((float)samplerState.MaxAnisotropy) : 1f); } GL.TexParameter(All.Texture2D, All.TextureMinFilter, (int)TranslateTextureFilterModeMin(samplerState.FilterMode, texture2D.MipLevelsCount > 1)); GL.TexParameter(All.Texture2D, All.TextureMagFilter, (int)TranslateTextureFilterModeMag(samplerState.FilterMode)); GL.TexParameter(All.Texture2D, All.TextureWrapS, (int)TranslateTextureAddressMode(samplerState.AddressModeU)); GL.TexParameter(All.Texture2D, All.TextureWrapT, (int)TranslateTextureAddressMode(samplerState.AddressModeV)); m_textureSamplerStates[texture2D.m_texture] = samplerState; } } else if (m_activeTexturesByUnit[num] != 0) { BindTexture(All.Texture2D, 0, forceBind: true); } num++; shaderParameter.IsChanged = false; } num2++; } throw new InvalidOperationException($"Associated SamplerState is not set for texture \"{shaderParameter.Name}\"."); }