private void FinishMaterial(BatchInfo material) { //DrawTechnique tech = material.Technique.Res; this.SetupBlendType(BlendMode.Reset); NativeShaderProgram.Bind(null); NativeTexture.ResetBinding(this.sharedSamplerBindings); }
private void FinishVertexFormat(BatchInfo material, VertexDeclaration vertexDeclaration) { DrawTechnique technique = material.Technique.Res ?? DrawTechnique.Solid.Res; NativeShaderProgram program = (technique.Shader.Res != null ? technique.Shader.Res : ShaderProgram.Minimal.Res).Native as NativeShaderProgram; if (program == null) { return; } //VertexDeclaration vertexDeclaration = renderBatch.VertexDeclaration; //VertexElement[] elements = vertexDeclaration.Elements; ShaderFieldInfo[] varInfo = program.Fields; int[] locations = program.FieldLocations; for (int varIndex = 0; varIndex < varInfo.Length; varIndex++) { if (locations[varIndex] == -1) { continue; } GL.DisableVertexAttribArray(locations[varIndex]); } }
/// <summary> /// Applies the specified parameter values to all currently active shaders. /// </summary> /// <param name="sharedParams"></param> /// <seealso cref="RetrieveActiveShaders"/> private void SetupSharedParameters(ShaderParameterCollection sharedParams) { this.sharedSamplerBindings = 0; this.sharedShaderParameters.Clear(); if (sharedParams == null) { return; } foreach (NativeShaderProgram shader in this.activeShaders) { NativeShaderProgram.Bind(shader); ShaderFieldInfo[] varInfo = shader.Fields; int[] locations = shader.FieldLocations; // Setup shared sampler bindings and uniform data for (int i = 0; i < varInfo.Length; i++) { ref ShaderFieldInfo field = ref varInfo[i]; if (field.Scope == ShaderFieldScope.Attribute) { continue; } if (field.Type == ShaderFieldType.Sampler2D) { ContentRef <Texture> texRef; if (!sharedParams.TryGetInternal(field.Name, out texRef)) { continue; } NativeTexture.Bind(texRef, this.sharedSamplerBindings); GL.Uniform1(locations[i], this.sharedSamplerBindings); this.sharedSamplerBindings++; } else { float[] data; if (!sharedParams.TryGetInternal(field.Name, out data)) { continue; } NativeShaderProgram.SetUniform(ref field, locations[i], data); } this.sharedShaderParameters.Add(field.Name); } }
public static void Bind(NativeShaderProgram prog) { if (curBound == prog) { return; } if (prog == null) { GL.UseProgram(0); curBound = null; } else { GL.UseProgram(prog.Handle); curBound = prog; } }
private void SetupMaterial(BatchInfo material, BatchInfo lastMaterial) { if (material == lastMaterial) { return; } DrawTechnique tech = material.Technique.Res ?? DrawTechnique.Solid.Res; DrawTechnique lastTech = lastMaterial != null ? lastMaterial.Technique.Res : null; // Setup BlendType if (lastTech == null || tech.Blending != lastTech.Blending) { this.SetupBlendType(tech.Blending, this.currentDevice.DepthWrite); } // Bind Shader ShaderProgram shader = tech.Shader.Res ?? ShaderProgram.Minimal.Res; NativeShaderProgram nativeShader = shader.Native as NativeShaderProgram; NativeShaderProgram.Bind(nativeShader); // Setup shader data ShaderFieldInfo[] varInfo = nativeShader.Fields; int[] locations = nativeShader.FieldLocations; // Setup sampler bindings automatically int curSamplerIndex = this.sharedSamplerBindings; for (int i = 0; i < varInfo.Length; i++) { if (locations[i] == -1) { continue; } if (varInfo[i].Type != ShaderFieldType.Sampler2D) { continue; } if (this.sharedShaderParameters.Contains(varInfo[i].Name)) { continue; } ContentRef <Texture> texRef = material.GetInternalTexture(varInfo[i].Name); NativeTexture.Bind(texRef, curSamplerIndex); GL.Uniform1(locations[i], curSamplerIndex); curSamplerIndex++; } NativeTexture.ResetBinding(curSamplerIndex); // Setup uniform data for (int i = 0; i < varInfo.Length; i++) { if (locations[i] == -1) { continue; } if (varInfo[i].Type == ShaderFieldType.Sampler2D) { continue; } if (this.sharedShaderParameters.Contains(varInfo[i].Name)) { continue; } float[] data; if (varInfo[i].Name == "ModelView") { data = modelViewData; } else if (varInfo[i].Name == "Projection") { data = projectionData; } else { data = material.GetInternalData(varInfo[i].Name); if (data == null) { continue; } } NativeShaderProgram.SetUniform(ref varInfo[i], locations[i], data); } }
private void SetupVertexFormat(BatchInfo material, VertexDeclaration vertexDeclaration) { DrawTechnique technique = material.Technique.Res ?? DrawTechnique.Solid.Res; NativeShaderProgram program = (technique.Shader.Res != null ? technique.Shader.Res : ShaderProgram.Minimal.Res).Native as NativeShaderProgram; if (program == null) { return; } VertexElement[] elements = vertexDeclaration.Elements; ShaderFieldInfo[] varInfo = program.Fields; int[] locations = program.FieldLocations; bool[] varUsed = new bool[varInfo.Length]; for (int elementIndex = 0; elementIndex < elements.Length; elementIndex++) { int selectedVar = -1; for (int varIndex = 0; varIndex < varInfo.Length; varIndex++) { if (varUsed[varIndex]) { continue; } if (locations[varIndex] == -1 || varInfo[varIndex].Scope != ShaderFieldScope.Attribute) { varUsed[varIndex] = true; continue; } if (!ShaderVarMatches( ref varInfo[varIndex], elements[elementIndex].Type, elements[elementIndex].Count)) { continue; } //if (elements[elementIndex].Role != VertexElementRole.Unknown && varInfo[varIndex].Name != elements[elementIndex].Role.ToString()) { // continue; //} selectedVar = varIndex; varUsed[varIndex] = true; break; } if (selectedVar == -1) { continue; } VertexAttribPointerType attribType; switch (elements[elementIndex].Type) { default: case VertexElementType.Float: attribType = VertexAttribPointerType.Float; break; case VertexElementType.Byte: attribType = VertexAttribPointerType.UnsignedByte; break; } bool isNormalized = (elements[elementIndex].Role == VertexElementRole.Color); GL.EnableVertexAttribArray(locations[selectedVar]); GL.VertexAttribPointer( locations[selectedVar], elements[elementIndex].Count, attribType, isNormalized, vertexDeclaration.Size, elements[elementIndex].Offset); } }