Home Reference Source

cables_dev/cables/src/core/cgp/cgp_uniform.js

  1. import CgTexture from "../cg/cg_texture.js";
  2. import CgUniform from "../cg/cg_uniform.js";
  3.  
  4. export default class Uniform extends CgUniform
  5. {
  6. constructor(__shader, __type, __name, _value, _port2, _port3, _port4, _structUniformName, _structName, _propertyName)
  7. {
  8. super(__shader, __type, __name, _value, _port2, _port3, _port4, _structUniformName, _structName, _propertyName);
  9. this._cgp = __shader._cgp;
  10.  
  11. if (!_value)
  12. {
  13. // if (this.getType() == "m4") this._value = mat4.create();
  14. if (this.getType() == "t") this._value = this._cgp.getEmptyTexture();
  15. // else if (this.getType() == "2f") this._value = [0, 0];
  16. // else if (this.getType() == "4f") this._value = [0, 1, 0, 1];
  17. // else if (this.getType() == "3f") this._value = [0, 1, 0];
  18. }
  19.  
  20. this.gpuBuffer = null;
  21. }
  22.  
  23.  
  24. updateValueF() { }
  25.  
  26. updateValueArrayF() {}
  27.  
  28. setValueArrayF(v)
  29. {
  30. this.needsUpdate = true;
  31. this._value = v;
  32. }
  33.  
  34. setValueF(v)
  35. {
  36. this.needsUpdate = true;
  37. this._value = v;
  38. }
  39.  
  40. updateValue2F() { }
  41.  
  42. setValue2F(v)
  43. {
  44. this.needsUpdate = true;
  45. this._value = v;
  46. }
  47.  
  48. updateValue3F() { }
  49.  
  50. setValue3F(v)
  51. {
  52. this.needsUpdate = true;
  53. this._value = v;
  54. }
  55.  
  56. updateValue4F() { }
  57.  
  58. setValue4F(v)
  59. {
  60. if (v[0] == undefined)
  61. {
  62. this._log.stack("uniform value undefined");
  63. console.error("uniform value undefined");
  64. }
  65. this.needsUpdate = true;
  66. this._value = v;
  67. }
  68.  
  69. setValueT(v)
  70. {
  71. if (this._value != v)
  72. this._shader.needsPipelineUpdate = "texture changed"; // todo really needed ? change binding instead?
  73.  
  74. this.needsUpdate = true;
  75. this._value = v;
  76. }
  77.  
  78. updateValueM4(v) {}
  79.  
  80. setValueM4(v)
  81. {
  82. this.needsUpdate = true;
  83. this._value = v;
  84. }
  85.  
  86. setValueAny(v)
  87. {
  88. this.needsUpdate = true;
  89. this._value = v;
  90. }
  91.  
  92. updateValueAny() {}
  93.  
  94. updateValueT() {}
  95.  
  96.  
  97. setGpuBuffer(b)
  98. {
  99. this.gpuBuffer = b;
  100. }
  101.  
  102. copyToBuffer(buff, pos = 0)
  103. {
  104. if (this._type == "f")
  105. {
  106. buff[pos] = this._value;
  107. }
  108. else if (this._type == "t")
  109. {
  110. }
  111. else if (this._type == "4f")
  112. {
  113. buff[pos] = this._value[0];
  114. buff[pos + 1] = this._value[1];
  115. buff[pos + 2] = this._value[2];
  116. buff[pos + 3] = this._value[3];
  117. }
  118. else if (this._type == "f[]")
  119. {
  120. for (let i = 0; i < this._value.length; i++)
  121. buff[pos + i] = this._value[i];
  122. }
  123. else if (this._type == "m4")
  124. {
  125. for (let i = 0; i < 16; i++)
  126. buff[pos + i] = this._value[i];
  127. }
  128. else
  129. {
  130. this._log.warn("uniform copy to buffer unknown", this._type);
  131. }
  132. }
  133.  
  134. getWgslTypeStr()
  135. {
  136. if (this._type == "m4") return "mat4x4f";
  137. if (this._type == "4f") return "vec4f";
  138. if (this._type == "3f") return "vec3f";
  139. if (this._type == "2f") return "vec2f";
  140. if (this._type == "f") return "float";
  141. if (this._type == "f[]") return "array<vec4f>";
  142. if (this._type == "i") return "int";
  143. if (this._type == "sampler") return "sampler";
  144. if (this._type == "t") return "texture_2d<f32>";
  145. this._log.warn("unknown type getWgslTypeStr", this._type);
  146. return "???";
  147. }
  148.  
  149. getSizeBytes()
  150. {
  151. const bytesPerFloat = 4;
  152. const bytesPerInt = 4;
  153. if (this._type == "t") return 4;
  154. if (this._type == "sampler") return 4;
  155. if (this._type == "f") return 1 * bytesPerFloat;
  156. if (this._type == "2f") return 2 * bytesPerFloat;
  157. if (this._type == "3f") return 3 * bytesPerFloat;
  158. if (this._type == "4f") return 4 * bytesPerFloat;
  159. if (this._type == "f[]") return this._value.length * bytesPerFloat;
  160.  
  161. if (this._type == "m4") return 4 * 4 * bytesPerFloat;
  162.  
  163. if (this._type == "i") return 1 * bytesPerInt;
  164. if (this._type == "2i") return 2 * bytesPerInt;
  165.  
  166. this._log.warn("unknown type getSizeBytes", this._type);
  167. return 4;
  168. }
  169.  
  170. copy(newShader)
  171. {
  172. const uni = new Uniform(newShader, this._type, this._name, this._value, this._port2, this._port3, this._port4, this._structUniformName, this._structName, this._propertyName);
  173. uni.shaderType = this.shaderType;
  174.  
  175. console.log(this._name, this._value, uni._value);
  176.  
  177.  
  178. return uni;
  179. }
  180. }