yzt
2023-05-05 634ab285812bcc3eb802cacb9ec54f489bc2728f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { BoundingRectangle } from "../../Source/Cesium.js";
import { PixelFormat } from "../../Source/Cesium.js";
import { Buffer } from "../../Source/Cesium.js";
import { BufferUsage } from "../../Source/Cesium.js";
import { ComputeCommand } from "../../Source/Cesium.js";
import { ShaderProgram } from "../../Source/Cesium.js";
import { Texture } from "../../Source/Cesium.js";
import { VertexArray } from "../../Source/Cesium.js";
import { Material } from "../../Source/Cesium.js";
import { ViewportQuad } from "../../Source/Cesium.js";
import createScene from "../createScene.js";
 
describe(
  "Renderer/ComputeCommand",
  function () {
    var scene;
    var context;
 
    beforeAll(function () {
      scene = createScene();
      context = scene.context;
    });
 
    afterAll(function () {
      scene.destroyForSpecs();
    });
 
    afterEach(function () {
      scene.primitives.removeAll();
    });
 
    function CommandMockPrimitive(command) {
      this.update = function (frameState) {
        frameState.commandList.push(command);
      };
      this.destroy = function () {};
    }
 
    it("throws if no shader is provided", function () {
      var outputTexture = new Texture({
        context: context,
        width: 1,
        height: 1,
        pixelFormat: PixelFormat.RGBA,
      });
      var computeCommand = new ComputeCommand({
        outputTexture: outputTexture,
      });
      scene.primitives.add(new CommandMockPrimitive(computeCommand));
 
      expect(function () {
        scene.renderForSpecs();
      }).toThrowDeveloperError();
    });
 
    it("throws if no output texture is provided", function () {
      var computeCommand = new ComputeCommand({
        fragmentShaderSource: "void main() { gl_FragColor = vec4(1.0); }",
      });
      scene.primitives.add(new CommandMockPrimitive(computeCommand));
 
      expect(function () {
        scene.renderForSpecs();
      }).toThrowDeveloperError();
    });
 
    it("renderer resources are preserved or destroyed based on the persists flag", function () {
      var vertexShader =
        "attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }";
      var fragmentShader = "void main() { gl_FragColor = vec4(1.0); }";
      var shaderProgram = ShaderProgram.fromCache({
        context: context,
        vertexShaderSource: vertexShader,
        fragmentShaderSource: fragmentShader,
        attributeLocations: {
          position: 0,
        },
      });
 
      var vertexArray = new VertexArray({
        context: context,
        attributes: [
          {
            index: 0,
            vertexBuffer: Buffer.createVertexBuffer({
              context: context,
              typedArray: new Float32Array([0, 0, 0, 1]),
              usage: BufferUsage.STATIC_DRAW,
            }),
            componentsPerAttribute: 4,
          },
        ],
      });
 
      var outputTexture = new Texture({
        context: context,
        width: 1,
        height: 1,
        pixelFormat: PixelFormat.RGBA,
      });
 
      var computeCommand = new ComputeCommand({
        vertexArray: vertexArray,
        shaderProgram: shaderProgram,
        outputTexture: outputTexture,
      });
 
      // check that resources are preserved when persists is true
      computeCommand.persists = true;
      scene.primitives.add(new CommandMockPrimitive(computeCommand));
      scene.renderForSpecs();
      context.shaderCache.destroyReleasedShaderPrograms();
      scene.primitives.removeAll();
 
      expect(shaderProgram.isDestroyed()).toEqual(false);
      expect(vertexArray.isDestroyed()).toEqual(false);
      expect(outputTexture.isDestroyed()).toEqual(false);
 
      // check that resources are destroyed when persists is false, except
      // outputTexture which is always preserved
      computeCommand.persists = false;
      scene.primitives.add(new CommandMockPrimitive(computeCommand));
      scene.renderForSpecs();
      context.shaderCache.destroyReleasedShaderPrograms();
      scene.primitives.removeAll();
 
      expect(shaderProgram.isDestroyed()).toEqual(true);
      expect(vertexArray.isDestroyed()).toEqual(true);
      expect(outputTexture.isDestroyed()).toEqual(false);
    });
 
    it("renders to a texture and draws that texture to the screen", function () {
      var outputTexture = new Texture({
        context: context,
        width: 1,
        height: 1,
        pixelFormat: PixelFormat.RGBA,
      });
      var computeCommand = new ComputeCommand({
        fragmentShaderSource: "void main() { gl_FragColor = vec4(1.0); }",
        outputTexture: outputTexture,
      });
 
      var viewportQuad = new ViewportQuad();
      viewportQuad.rectangle = new BoundingRectangle(0, 0, 1, 1);
      viewportQuad.material = Material.fromType(Material.ImageType);
      viewportQuad.material.uniforms.image = outputTexture;
 
      expect(scene).toRender([0, 0, 0, 255]);
      scene.primitives.add(new CommandMockPrimitive(computeCommand));
      scene.primitives.add(viewportQuad);
      expect(scene).notToRender([0, 0, 0, 255]);
    });
  },
  "WebGL"
);