private Expression GetReadUniformBufferExpression(FieldInfo fieldInfo, int binding, int set) { string debugFieldName = string.Format("{0}.{1}", fieldInfo.DeclaringType.Name, fieldInfo.Name); Type returnType = fieldInfo.FieldType; var bufferInfo = m_context.m_DescriptorSets[set].m_BufferInfo[binding]; if (bufferInfo.buffer == null) { DebugMsg(string.Format("ERROR: no buffer bound to uniform set={0} binding={1} for field {2}", set, binding, debugFieldName)); return(null); } SoftwareBuffer softwareBuffer = (SoftwareBuffer)bufferInfo.buffer; if (softwareBuffer.m_deviceMemory == null) { DebugMsg(string.Format("ERROR: no memory bound to buffer on uniform set={0} binding={1} for field {2}", set, binding, debugFieldName)); return(null); } SoftwareDeviceMemory mem = softwareBuffer.m_deviceMemory; byte[] memory = mem.m_bytes; int offset = bufferInfo.offset + softwareBuffer.m_memoryOffset; int size = Marshal.SizeOf(returnType); return(IntrospectionUtil.GetGenericMethodCallExpression( this, nameof(ReadUniform), new Type[] { returnType }, Expression.Constant(memory), Expression.Constant(offset), Expression.Constant(size))); }
private Expression GetWriteColorOutputExpression(int location, Expression colorParam) { var framebuffer = (SoftwareFramebuffer)m_context.m_RenderPassBeginInfo.framebuffer; var imgView = framebuffer.m_createInfo.pAttachments[m_context.m_CurrentSubpass.pColorAttachments[location].attachment]; Expression fragCoord = IntrospectionUtil.GetExpressionFor(() => internal_FragCoord); Expression call = IntrospectionUtil.GetMethodCallExpression(imgView, "SetPixel", fragCoord, colorParam); return(call); }
public CompiledPipelineProgram Compile() { CompiledPipelineProgram compiled = new CompiledPipelineProgram(); compiled.VertexShader = IntrospectionUtil.CreateCall(GetExpression(StageType.VertexShader)); compiled.FragmentShader = IntrospectionUtil.CreateCall(GetExpression(StageType.FragmentShader)); compiled.InitializePipeline = IntrospectionUtil.CreateCall(GetExpression(UniformLoader, null, null)); return(compiled); }
public void TestFieldDefault() { //direct FieldInfo f = IntrospectionUtil.FindField(typeof(ClassFixtureA), "internalA", typeof(int), true, false); Assert.AreEqual(f, defaultAField); //inheritance f = IntrospectionUtil.FindField(typeof(ClassFixtureB), "internalA", typeof(int), true, false); Assert.AreEqual(f, defaultAField); }
public void TestPropertyDefault() { // direct PropertyInfo m = IntrospectionUtil.FindProperty(typeof(ClassFixtureE), "InternalE", false); Assert.AreEqual(m, defaultEProperty); //inherited m = IntrospectionUtil.FindProperty(typeof(ClassFixtureF), "InternalE", true); Assert.AreEqual(m, defaultEProperty); }
public void TestMethodProtected() { // direct MethodInfo m = IntrospectionUtil.FindMethod(typeof(ClassFixtureC), "setProtectedC", __INTEGER_ARG, true, false); Assert.AreEqual(m, protectedCMethod); //inherited m = IntrospectionUtil.FindMethod(typeof(ClassFixtureD), "setProtectedC", __INTEGER_ARG, true, false); Assert.AreEqual(m, protectedCMethod); }
public void TestPropertyPublic() { // direct PropertyInfo m = IntrospectionUtil.FindProperty(typeof(ClassFixtureE), "PublicE", false); Assert.AreEqual(m, publicEProperty); //inherited m = IntrospectionUtil.FindProperty(typeof(ClassFixtureF), "PublicE", true); Assert.AreEqual(m, publicEProperty); }
public void TestMethodDefault() { // direct MethodInfo m = IntrospectionUtil.FindMethod(typeof(ClassFixtureC), "setInternalC", __INTEGER_ARG, true, false); Assert.AreEqual(m, defaultCMethod); //inherited m = IntrospectionUtil.FindMethod(typeof(ClassFixtureD), "setInternalC", __INTEGER_ARG, true, false); Assert.AreEqual(m, defaultCMethod); }
public static CompiledShaderModule Compile(VkPipelineShaderStageCreateInfo stageCreateInfo) { SoftwareShaderModule module = (SoftwareShaderModule)stageCreateInfo.module; string entryPointName = stageCreateInfo.pName; CompiledShaderModule ret = new CompiledShaderModule(); ret.instance = module.GetNewInstance(); ret.entryPoint = IntrospectionUtil.GetMethodCallExpression(ret.instance, entryPointName); return(ret); }
public void TestFieldProtected() { //direct FieldInfo f = IntrospectionUtil.FindField(typeof(ClassFixtureA), "protectedA", typeof(int), true, false); Assert.AreEqual(f, protectedAField); //inheritance f = IntrospectionUtil.FindField(typeof(ClassFixtureB), "protectedA", typeof(int), true, false); Assert.AreEqual(f, protectedAField); }
public void TestPropertyPrivate() { // direct PropertyInfo m = IntrospectionUtil.FindProperty(typeof(ClassFixtureE), "PrivateE", false); Assert.AreEqual(m, privateEProperty); try { IntrospectionUtil.FindProperty(typeof(ClassFixtureF), "PrivateE", true); Assert.Fail("Should Be Null"); } catch (ArgumentException) { } }
public void TestMethodPrivate() { //direct MethodInfo m = IntrospectionUtil.FindMethod(typeof(ClassFixtureC), "setPrivateC", __INTEGER_ARG, true, false); Assert.AreEqual(m, privateCMethod); try { IntrospectionUtil.FindMethod(typeof(ClassFixtureD), "setPrivateC", __INTEGER_ARG, true, false); Assert.Fail("Private method should not be inherited"); } catch (ArgumentException) { } }
public void TestFieldPrivate() { //direct FieldInfo f = IntrospectionUtil.FindField(typeof(ClassFixtureA), "privateA", typeof(int), true, false); Assert.AreEqual(privateAField, f); try { //inheritance IntrospectionUtil.FindField(typeof(ClassFixtureB), "privateB", typeof(int), true, false); Assert.Fail("Private fields should not be inherited"); } catch (ArgumentException) { } }
private Expression GetReadSmothExpression(FieldInfo fieldInfo, Expression inputData) { string debugFieldName = string.Format("{0}.{1}", fieldInfo.DeclaringType.Name, fieldInfo.Name); try { return(IntrospectionUtil.GetMethodCallExpression( this, nameof(ReadSmoth), inputData )); } catch (InvalidOperationException) { } DebugMsg(string.Format( "WARNING: incompatible datatype {0} for fast interpolation for field {1}. Using slow-method instead.", fieldInfo.FieldType.Name, debugFieldName)); return(Expression.Convert(IntrospectionUtil.GetMethodCallExpression( this, nameof(ReadSmothDynamic), inputData ), fieldInfo.FieldType)); }
// TBD: // The problem with this algo is we have no way to consistently represent null node. // For map value and list element, we can just use Java null. // But, in general, it may not be possible..... ???? Is this true???? // Seems to be working so far (based on the limited unit test cases...) private object _BuildJsonStruct(object obj, int depth) { // if(depth < 0) { // return null; // } object jsonStruct = null; if (obj == null || obj is JsonNull) // ???? // return null; { } else { // System.Diagnostics.Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> depth = " + depth); // if(depth == 0) { if (depth <= 0) { if (obj is bool || obj is char || Number.IsNumber(obj) ) { jsonStruct = obj; } else if (obj is String) { // Note that this string is not a "json string" (e.g., forward slash escaped, etc.) // e.g., if the string is "...\\/...", we will read it as "...\\/..." not as ".../...". jsonStruct = (String)obj; } else { // ???? jsonStruct = obj.ToString(); } } else { // if (obj is IDictionary<String, Object>) { if (GenericUtil.IsDictionary(obj)) { //IDictionary<String, Object> jsonIDictionary = new OrderedMap<String, Object>(); IDictionary <String, Object> jsonIDictionary = new Dictionary <String, Object>(); // ... // ???? // IDictionary<String,Object> map = null; dynamic map = null; try { // map = (IDictionary<String,Object>) obj; map = obj; } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Invalid map type.", e); // What to do??? // Use map.ToString??? } if (map != null && map.Count > 0) { foreach (string f in map.Keys) { object val = map[f]; object jsonVal = _BuildJsonStruct(val, depth - 1); if (jsonVal != null) { jsonIDictionary.Add(f, jsonVal); } else { // ??? jsonIDictionary.Add(f, null); } } } jsonStruct = jsonIDictionary; // } else if (obj is IList<Object>) { } else if (GenericUtil.IsList(obj)) { IList <Object> jsonList = new List <Object>(); // ???? // IList<Object> list = null; dynamic list = null; try { // list = (IList<Object>) obj; list = obj; } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Invalid list type.", e); // What to do??? // Use list.ToString??? } if (list != null && list.Count > 0) { foreach (object v in list) { object jsonVal = _BuildJsonStruct(v, depth - 1); if (jsonVal != null) { jsonList.Add(jsonVal); } else { // ??? jsonList.Add(null); } } } jsonStruct = jsonList; } else if (obj.GetType().IsArray) // ??? { List <Object> jsonList = new List <Object>(); // ???? // object array = obj; dynamic array = obj; if (array != null && array.Length > 0) { int arrLen = array.Length; // System.Diagnostics.Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> arrLen = " + arrLen); for (int i = 0; i < arrLen; i++) { object o = array[i]; // System.Diagnostics.Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> o = " + o + "; " + o.Type); object jsonVal = _BuildJsonStruct(o, depth - 1); // System.Diagnostics.Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> jsonVal = " + jsonVal + "; " + o.Type); if (jsonVal != null) { jsonList.Add(jsonVal); } else { // ??? jsonList.Add(null); } } } jsonStruct = jsonList; } else if (obj is Collection <Object> ) // ?????? Not implemented yet. { List <Object> jsonList = new List <Object>(); // jsonList.AddAll((Collection<Object>) ((Collection<?>) obj)); IEnumerator <Object> it = ((Collection <Object>)obj).GetEnumerator(); while (it.MoveNext()) { object o = it.Current; object jsonVal = _BuildJsonStruct(o, depth - 1); if (jsonVal != null) { jsonList.Add(jsonVal); } else { // ??? jsonList.Add(null); } } jsonStruct = jsonList; } else { // ??? // This can potentially cause infinite recursion. // because maybe JsonCompatible object : toJsonStructure() using JsonBuilder.buidJsonStructure() // which calls the object's toJsonStructure(), which calls JsonBuilder.buidJsonStructure(), etc. // .... // if(obj is JsonCompatible) { // jsonStruct = ((JsonCompatible) obj).toJsonStructure(depth); // } else { // primitive types... ??? if (obj is bool || obj is char || Number.IsNumber(obj) || obj is string ) { jsonStruct = obj; } else { // Use inspection.... // TBD: // BuilderPolicy ??? // ... IDictionary <String, Object> mapEquivalent = null; try { // mapEquivalent = IntrospectionUtil.Introspect(obj, depth); // depth? or depth - 1 ? // Because we are just converting a bean to a map, // the depth param is not used. (or, depth == 1). mapEquivalent = IntrospectionUtil.Introspect(obj); } catch (Exception ex) { // Ignore. System.Diagnostics.Debug.WriteLine("Faild to Introspect a bean.", ex); } if (mapEquivalent != null) { jsonStruct = _BuildJsonStruct(mapEquivalent, depth); // Note: We do not change the depth. } else { // ???? // jsonStruct = null; ??? jsonStruct = obj.ToString(); // ... } // System.Diagnostics.Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> jsonStruct = " + jsonStruct); } // } } } } return(jsonStruct); }
public DataStorageItem AddGeneric(string name, Type dataType, int length) { var methodCall = IntrospectionUtil.GetGenericMethodCallExpression(this, nameof(Add), new Type[] { dataType }, new Expression[] { Expression.Constant(name), Expression.Constant(length) }); return(IntrospectionUtil.CreateFunc <DataStorageItem>(methodCall).Invoke()); }
private void PrepareShaderStage(StageType stage, object shaderInstance) { Type type = shaderInstance.GetType(); Expression primiteVertexIndexExpression = IntrospectionUtil.GetFieldExpression(this, nameof(PrimitiveVertexIndex)); foreach (var fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { string debugFieldName = string.Format("{0}.{1}", type.Name, fieldInfo.Name); var fieldExpression = IntrospectionUtil.GetFieldExpression(shaderInstance, fieldInfo); // BUILTIN if (m_ShaderBuiltins.IsBuiltin(stage, fieldInfo.Name)) { if (!m_ShaderBuiltins.Validate(stage, fieldInfo)) { DebugMsg(string.Format("ERROR: failed to bind: invalid builtin {0}", debugFieldName)); continue; } var builtin = m_ShaderBuiltins.Get(stage, fieldInfo.Name); var directionIn = builtin.Flags.HasFlag(BuiltinFlags.In); var directionOut = builtin.Flags.HasFlag(BuiltinFlags.Out); if (directionIn || directionOut) { Expression builtinVarExpression = IntrospectionUtil.GetFieldExpression(this, builtin.Name); Expression builtinExpression = builtinVarExpression; // Indexed? if (builtin.Flags.HasFlag(BuiltinFlags.IndexedPerVertex)) { builtinExpression = IntrospectionUtil.GetArrayAccessExpression(builtinVarExpression, primiteVertexIndexExpression); } if (!ValidateFieldType(fieldInfo, builtinExpression.Type)) { continue; } if (directionIn) { // m_Stages[stage].AddPreaction(IntrospectionUtil.CreateAssign(fieldExpression, builtinExpression)); m_PipelineProgramDefinition.PreActions[stage].Add(Expression.Assign(fieldExpression, builtinExpression)); } if (directionOut) { // m_Stages[stage].AddPostAction(IntrospectionUtil.CreateAssign(builtinExpression, fieldExpression)); m_PipelineProgramDefinition.PostActions[stage].Add(Expression.Assign(builtinExpression, fieldExpression)); } } if (builtin.Flags.HasFlag(BuiltinFlags.HasAction)) { builtin.UseAction.Invoke(); } continue; } // ----- Verify ShaderAttribute ----- var attrs = fieldInfo.GetCustomAttributes(typeof(ShaderLayoutAttribute)).ToArray(); if (attrs.Length < 1) { continue; } if (attrs.Length > 1) { DebugMsg(string.Format("Ambiguous attributes for field {0}", debugFieldName)); continue; } ShaderLayoutAttribute atr = (ShaderLayoutAttribute)attrs[0]; // ----- User variable with ShaderAttribute ----- if (atr.type == ShaderLayoutType.Uniform) { var readUniformCall = GetReadUniformExpression(fieldInfo, atr.binding, atr.set); if (readUniformCall != null) { m_PipelineProgramDefinition.UniformLoader.Add(Expression.Assign(fieldExpression, readUniformCall)); } continue; } if (stage == StageType.VertexShader) { // IF IN: obj.var = ReadVertexBuffer(location) // IF OUT: storage[var][index] = obj.var if (atr.type == ShaderLayoutType.In) { Type vertexBufferType = GetVertexBufferType(fieldInfo, atr.location); if (vertexBufferType == null) { // incompatible or not found continue; } if (!ValidateFieldType(fieldInfo, vertexBufferType)) { continue; } var readVertexBufferCall = GetReadVertexBufferExpression(fieldInfo, atr.location); if (readVertexBufferCall != null) { m_PipelineProgramDefinition.PreActions[stage].Add(Expression.Assign(fieldExpression, readVertexBufferCall)); } } else if (atr.type == ShaderLayoutType.Out) { DataStorageItem storage = m_Storage.AddGeneric(fieldInfo.Name, fieldInfo.FieldType, PrimitiveLength); Expression storageExpression = storage.GetIndexedExpression(primiteVertexIndexExpression); m_PipelineProgramDefinition.PostActions[stage].Add(Expression.Assign(storageExpression, fieldExpression)); } } else if (stage == StageType.FragmentShader) { // IF IN: obj.var = ReadSmoth(storage[var]) // IF OUT: WriteColorOutput(location, obj.var) if (atr.type == ShaderLayoutType.In) { if (!m_Storage.HasKey(fieldInfo.Name)) { DebugMsg(string.Format("ERROR: failed to bind field {0} (not found)", debugFieldName)); continue; } DataStorageItem storage = m_Storage.Get(fieldInfo.Name); if (!ValidateFieldType(fieldInfo, storage.DataType)) { continue; } var storageArrayExpression = storage.GetArrayExpression(); var readSmothCall = GetReadSmothExpression(fieldInfo, storageArrayExpression); m_PipelineProgramDefinition.PreActions[stage].Add(Expression.Assign(fieldExpression, readSmothCall)); } else if (atr.type == ShaderLayoutType.Out) { if (!ValidateFieldType(fieldInfo, P_WRITE_COLOR_TYPE)) { continue; } var writeColorOutputCall = GetWriteColorOutputExpression(atr.location, fieldExpression); m_PipelineProgramDefinition.PostActions[stage].Add(writeColorOutputCall); } } } }
private Expression GetReadVertexBufferExpression(FieldInfo fieldInfo, int location) { Type returnType = fieldInfo.FieldType; string debugFieldName = string.Format("{0}.{1}", fieldInfo.DeclaringType.Name, fieldInfo.Name); foreach (var attributeDesc in m_context.m_GraphicsPipeline.m_graphicsPipelineCreateInfo.pVertexInputState.pVertexAttributeDescriptions) { if (location == attributeDesc.location) { bool found = false; VkVertexInputBindingDescription inputBindingDesc = new VkVertexInputBindingDescription(); foreach (var item in m_context.m_GraphicsPipeline.m_graphicsPipelineCreateInfo.pVertexInputState.pVertexBindingDescriptions) { if (item.binding == attributeDesc.binding) { inputBindingDesc = item; found = true; break; } } if (!found) { // Binding not found DebugMsg(string.Format( "ERROR: vertex buffer binding={0} not found for location={1} for field {2}", attributeDesc.binding, location, debugFieldName )); return(null); } var vb = m_context.m_CommandBuffer.m_context.m_VertexBuffers[attributeDesc.binding]; var mem = vb.m_deviceMemory; int stride = inputBindingDesc.stride; int offset = attributeDesc.offset; byte[] memory = mem.m_bytes; if (vb.m_VertexBufferCache != null) { // Use vertex buffer cache Expression indexExpression = IntrospectionUtil.GetExpressionFor(() => this.gl_VertexIndex); return(vb.m_VertexBufferCache.GetReadExpression(returnType, offset, stride, indexExpression)); } else { // Read from raw return(IntrospectionUtil.GetGenericMethodCallExpression( this, nameof(ReadVertexBuffer), new Type[] { returnType }, Expression.Constant(memory), Expression.Constant(offset), Expression.Constant(stride))); } } } DebugMsg(string.Format( "ERROR: vertex buffer location={0} not found for field {1}", location, debugFieldName )); return(null); }