private bool FindUniforms(Dictionary <string, UniformValue> nameUniformDict, Dictionary <int, UniformValue> locationUniformDict) { nameUniformDict.Clear(); locationUniformDict.Clear(); int nextLoc = 0; foreach (var shader in this.attachedShaders) { foreach (var item in shader.UniformVariableDict) { string varName = item.Key; UniformVariable v = item.Value; if (nameUniformDict.ContainsKey(varName)) { if (v.fieldInfo.FieldType != nameUniformDict[varName].variable.fieldInfo.FieldType) { this.logInfo = string.Format("Different uniform variable types of the same name[{0}!]", varName); return(false); } } else { v.location = nextLoc; int byteSize = this.GetByteSize(v.fieldInfo.FieldType); nextLoc += byteSize; var value = new UniformValue(v, null); nameUniformDict.Add(varName, value); locationUniformDict.Add(v.location, value); } } } return(true); }
public UniformVariable GetUniformVariable(int location) { UniformValue v = null; if (!this.locationUniformDict.TryGetValue(location, out v)) { v = null; } return(v != null ? v.variable : null); }
public int GetUniformLocation(string name) { int result = -1; if (this.logInfo.Length > 0) { return(-1); } UniformValue v = null; if (this.nameUniformDict.TryGetValue(name, out v)) { result = (int)v.variable.location; } return(result); }
internal void SetUniform(int location, Object value) { if (value == null) { return; } Dictionary <int, UniformValue> locationUniformDict = this.locationUniformDict; UniformValue uniformValue = null; if (locationUniformDict.TryGetValue(location, out uniformValue)) { if (uniformValue.variable.fieldInfo.FieldType != value.GetType()) { throw new ArgumentException("value"); } uniformValue.value = value; } else { // TODO: what to do when specified uniform variable not exists? Silent or throwing exception? } }
private unsafe PassBuffer[] VertexShaderStage(int count, DrawElementsType type, IntPtr indices, VertexArrayObject vao, ShaderProgram program, GLBuffer indexBuffer) { PassBuffer[] passBuffers = null; VertexShader vs = program.VertexShader; if (vs == null) { return(passBuffers); } // init pass-buffers to record output from vertex shader. FieldInfo[] outFieldInfos = (from item in vs.outVariableDict select item.Value.fieldInfo).ToArray(); uint vertexCount = GetVertexCount(vao, indexBuffer, type); //int vertexSize = GetVertexSize(outFieldInfos); passBuffers = new PassBuffer[1 + outFieldInfos.Length]; void *[] pointers = new void *[1 + outFieldInfos.Length]; { // the first pass-buffer stores gl_Position. var passBuffer = new PassBuffer(PassType.Vec4, (int)vertexCount); pointers[0] = (void *)passBuffer.Mapbuffer(); passBuffers[0] = passBuffer; } for (int i = 1; i < passBuffers.Length; i++) { var outField = outFieldInfos[i - 1]; PassType passType = outField.FieldType.GetPassType(); var passBuffer = new PassBuffer(passType, (int)vertexCount); pointers[i] = (void *)passBuffer.Mapbuffer(); passBuffers[i] = passBuffer; } // execute vertex shader for each vertex. byte[] indexData = indexBuffer.Data; int indexLength = indexData.Length / ByteLength(type); GCHandle pin = GCHandle.Alloc(indexData, GCHandleType.Pinned); IntPtr pointer = pin.AddrOfPinnedObject(); var gl_VertexIDList = new List <uint>(); for (int indexID = indices.ToInt32() / ByteLength(type), c = 0; c < count && indexID < indexLength; indexID++, c++) { uint gl_VertexID = GetVertexID(pointer, type, indexID); if (gl_VertexIDList.Contains(gl_VertexID)) { continue; } else { gl_VertexIDList.Add(gl_VertexID); } var instance = vs.CreateCodeInstance() as VertexCodeBase; // an executable vertex shader. instance.gl_VertexID = (int)gl_VertexID; // setup gl_VertexID. // setup "in SomeType varName;" vertex attributes. Dictionary <uint, VertexAttribDesc> locVertexAttribDict = vao.LocVertexAttribDict; foreach (InVariable inVar in vs.inVariableDict.Values) // Dictionary<string, InVariable>.Values { VertexAttribDesc desc = null; if (locVertexAttribDict.TryGetValue(inVar.location, out desc)) { byte[] dataStore = desc.vbo.Data; int byteIndex = desc.GetDataIndex(gl_VertexID); VertexAttribType vertexAttribType = (VertexAttribType)desc.dataType; object value = dataStore.ToStruct(inVar.fieldInfo.FieldType, byteIndex); inVar.fieldInfo.SetValue(instance, value); } } // setup "uniform SomeType varName;" in vertex shader. Dictionary <string, UniformValue> nameUniformDict = program.nameUniformDict; foreach (UniformVariable uniformVar in vs.UniformVariableDict.Values) { string name = uniformVar.fieldInfo.Name; UniformValue obj = null; if (nameUniformDict.TryGetValue(name, out obj)) { if (obj.value != null) { uniformVar.fieldInfo.SetValue(instance, obj.value); } } } instance.main(); // execute vertex shader code. // copy data to pass-buffer. { PassBuffer passBuffer = passBuffers[0]; var array = (vec4 *)pointers[0]; array[gl_VertexID] = instance.gl_Position; } for (int i = 1; i < passBuffers.Length; i++) { var outField = outFieldInfos[i - 1]; var obj = outField.GetValue(instance); switch (outField.FieldType.GetPassType()) { case PassType.Float: { var array = (float *)pointers[i]; array[gl_VertexID] = (float)obj; } break; case PassType.Vec2: { var array = (vec2 *)pointers[i]; array[gl_VertexID] = (vec2)obj; } break; case PassType.Vec3: { var array = (vec3 *)pointers[i]; array[gl_VertexID] = (vec3)obj; } break; case PassType.Vec4: { var array = (vec4 *)pointers[i]; array[gl_VertexID] = (vec4)obj; } break; case PassType.Mat2: { var array = (mat2 *)pointers[i]; array[gl_VertexID] = (mat2)obj; } break; case PassType.Mat3: { var array = (mat3 *)pointers[i]; array[gl_VertexID] = (mat3)obj; } break; case PassType.Mat4: { var array = (mat4 *)pointers[i]; array[gl_VertexID] = (mat4)obj; } break; default: throw new NotImplementedException(); } // a general way to do this: //var obj = outField.GetValue(instance); //byte[] bytes = obj.ToBytes(); //PassBuffer passBuffer = passBuffers[i]; //var array = (byte*)passBuffer.AddrOfPinnedObject(); //for (int t = 0; t < bytes.Length; t++) //{ // array[gl_VertexID * vertexSize + t] = bytes[t]; //} } } pin.Free(); for (int i = 0; i < passBuffers.Length; i++) { passBuffers[i].Unmapbuffer(); } return(passBuffers); }
private unsafe void FragmentShaderStage(ShaderProgram program, List <Fragment> fragmentList) { FragmentShader fs = program.FragmentShader; if (fs == null) { return; } foreach (var fragment in fragmentList) { var instance = fs.CreateCodeInstance() as FragmentCodeBase; // an executable fragment shader. instance.gl_FragCoord = fragment.gl_FragCoord; // setup fragmen coordinatein window/screen space. // setup "in SomeType varName;" vertex attributes. InVariable[] inVariables = fs.inVariableDict.Values.ToArray(); if (inVariables.Length != fragment.attributes.Length) { throw new Exception("This should not happen!"); } for (int index = 0; index < inVariables.Length; index++) { InVariable inVar = inVariables[index]; PassBuffer attribute = fragment.attributes[index]; var pointer = attribute.Mapbuffer().ToPointer(); switch (attribute.elementType) { case PassType.Float: inVar.fieldInfo.SetValue(instance, ((float *)pointer)[0]); break; case PassType.Vec2: inVar.fieldInfo.SetValue(instance, ((vec2 *)pointer)[0]); break; case PassType.Vec3: inVar.fieldInfo.SetValue(instance, ((vec3 *)pointer)[0]); break; case PassType.Vec4: inVar.fieldInfo.SetValue(instance, ((vec4 *)pointer)[0]); break; case PassType.Mat2: inVar.fieldInfo.SetValue(instance, ((mat2 *)pointer)[0]); break; case PassType.Mat3: inVar.fieldInfo.SetValue(instance, ((mat3 *)pointer)[0]); break; case PassType.Mat4: inVar.fieldInfo.SetValue(instance, ((mat4 *)pointer)[0]); break; default: throw new NotDealWithNewEnumItemException(typeof(PassType)); } attribute.Unmapbuffer(); } // setup "uniform SomeType varName;" in fragment shader. Dictionary <string, UniformValue> nameUniformDict = program.nameUniformDict; foreach (UniformVariable uniformVar in fs.UniformVariableDict.Values) { string name = uniformVar.fieldInfo.Name; UniformValue obj = null; if (nameUniformDict.TryGetValue(name, out obj)) { if (obj.value != null) { uniformVar.fieldInfo.SetValue(instance, obj.value); } } } instance.main(); // execute fragment shader code. fragment.discard = instance.discard; if (!instance.discard) // if this fragment is not discarded. { OutVariable[] outVariables = fs.outVariableDict.Values.ToArray(); var outBuffers = new PassBuffer[outVariables.Length]; for (int index = 0; index < outVariables.Length; index++) { OutVariable outVar = outVariables[index]; var outBuffer = new PassBuffer(outVar.fieldInfo.FieldType.GetPassType(), 1); var pointer = outBuffer.Mapbuffer().ToPointer(); switch (outBuffer.elementType) { case PassType.Float: ((float *)pointer)[0] = (float)outVar.fieldInfo.GetValue(instance); break; case PassType.Vec2: ((vec2 *)pointer)[0] = (vec2)outVar.fieldInfo.GetValue(instance); break; case PassType.Vec3: ((vec3 *)pointer)[0] = (vec3)outVar.fieldInfo.GetValue(instance); break; case PassType.Vec4: ((vec4 *)pointer)[0] = (vec4)outVar.fieldInfo.GetValue(instance); break; case PassType.Mat2: ((mat2 *)pointer)[0] = (mat2)outVar.fieldInfo.GetValue(instance); break; case PassType.Mat3: ((mat3 *)pointer)[0] = (mat3)outVar.fieldInfo.GetValue(instance); break; case PassType.Mat4: ((mat4 *)pointer)[0] = (mat4)outVar.fieldInfo.GetValue(instance); break; default: throw new NotDealWithNewEnumItemException(typeof(PassType)); } outBuffer.Unmapbuffer(); outBuffers[index] = outBuffer; } fragment.outVariables = outBuffers; } } }