getUniformFromNode method

dynamic getUniformFromNode(
  1. dynamic node,
  2. dynamic shaderStage,
  3. dynamic type
)
override

Implementation

getUniformFromNode(node, shaderStage, type) {
  var uniformNode = super.getUniformFromNode(node, shaderStage, type);
  Map nodeData = this.getDataFromNode(node, shaderStage);

  if (nodeData["uniformGPU"] == undefined) {
    var uniformGPU;

    var bindings = this.bindings[shaderStage];

    if (type == 'texture') {
      var sampler = new WebGPUNodeSampler(
          "${uniformNode.name}_sampler", uniformNode.node);
      var texture =
          new WebGPUNodeSampledTexture(uniformNode.name, uniformNode.node);

      // add first textures in sequence and group for last
      var lastBinding = bindings[bindings.length - 1];
      var index = lastBinding && lastBinding.isUniformsGroup
          ? bindings.length - 1
          : bindings.length;

      if (shaderStage == 'fragment') {
        bindings.splice(index, 0, sampler, texture);

        uniformGPU = [sampler, texture];
      } else {
        bindings.splice(index, 0, texture);

        uniformGPU = [texture];
      }
    } else if (type == 'buffer') {
      var buffer = new WebGPUUniformBuffer('NodeBuffer', node.value);

      // add first textures in sequence and group for last
      var lastBinding = bindings[bindings.length - 1];
      var index = lastBinding && lastBinding.isUniformsGroup
          ? bindings.length - 1
          : bindings.length;

      bindings.splice(index, 0, buffer);

      uniformGPU = buffer;
    } else {
      var uniformsGroup = this.uniformsGroup[shaderStage];

      if (uniformsGroup == undefined) {
        uniformsGroup = WebGPUNodeUniformsGroup(shaderStage);

        this.uniformsGroup[shaderStage] = uniformsGroup;

        bindings.add(uniformsGroup);
      }

      if (node is ArrayInputNode) {
        uniformGPU = [];

        for (var inputNode in node.nodes) {
          var uniformNodeGPU = this._getNodeUniform(inputNode, type);

          // fit bounds to buffer
          uniformNodeGPU.boundary = getVectorLength(uniformNodeGPU.itemSize);
          uniformNodeGPU.itemSize = getStrideLength(uniformNodeGPU.itemSize);

          uniformsGroup.addUniform(uniformNodeGPU);

          uniformGPU.add(uniformNodeGPU);
        }
      } else {
        uniformGPU = this._getNodeUniform(uniformNode, type);

        uniformsGroup.addUniform(uniformGPU);
      }
    }

    nodeData["uniformGPU"] = uniformGPU;

    if (shaderStage == 'vertex') {
      this.bindingsOffset['fragment'] = bindings.length;
    }
  }

  return uniformNode;
}