File: gl_state_trashing.cpp

package info (click to toggle)
renderdoc 1.24%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 105,156 kB
  • sloc: cpp: 759,405; ansic: 309,460; python: 26,606; xml: 22,599; java: 11,365; cs: 7,181; makefile: 6,707; yacc: 5,682; ruby: 4,648; perl: 3,461; sh: 2,354; php: 2,119; lisp: 1,835; javascript: 1,524; tcl: 1,068; ml: 747
file content (192 lines) | stat: -rw-r--r-- 6,570 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2019-2022 Baldur Karlsson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

#include "gl_test.h"

RD_TEST(GL_State_Trashing, OpenGLGraphicsTest)
{
  static constexpr const char *Description =
      "Ensures that implicit shadowed GL state isn't trashed by initial states or capture overlay.";

  std::string pixel = R"EOSHADER(
#version 420 core

#define v2f v2f_block \
{                     \
	vec4 pos;           \
	vec4 col;           \
	vec4 uv;            \
}

in v2f vertIn;

layout(location = 0, index = 0) out vec4 Color;

layout(binding = 0, std140) uniform constsbuf
{
  vec4 tint;
};

uniform vec4 tint2;

void main()
{
	Color = vertIn.col * tint * tint2;
}

)EOSHADER";

  int main()
  {
    // initialise, create window, create context, etc
    if(!Init())
      return 3;

    GLuint vao = MakeVAO();
    // only time we bind the VAO, to ensure VAO state isn't trashed
    glBindVertexArray(vao);

    GLuint vb = MakeBuffer();
    // only time we bind the array buffer
    glBindBuffer(GL_ARRAY_BUFFER, vb);
    glBufferStorage(GL_ARRAY_BUFFER, sizeof(DefaultTri), NULL, GL_DYNAMIC_STORAGE_BIT);

    GLuint ubo = MakeBuffer();
    // only time we bind the uniform buffer
    glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    glBufferStorage(GL_UNIFORM_BUFFER, sizeof(DefaultTri), NULL, GL_DYNAMIC_STORAGE_BIT);
    glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
    glBindBuffer(GL_UNIFORM_BUFFER, ubo);

    GLuint program = MakeProgram(GLDefaultVertex, pixel);
    glUseProgram(program);

    GLint loc = glGetUniformLocation(program, "tint2");

    uint32_t empty[1024] = {};

    GLuint fbo = MakeFBO();
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    // create some things to force different types of initial states
    GLuint texs[] = {MakeTexture(), MakeTexture(), MakeTexture()};
    glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texs[0]);
    glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB16F, screenWidth, screenHeight,
                            false);

    glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, texs[1]);
    glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 4, GL_DEPTH_COMPONENT24, screenWidth,
                            screenHeight, 6, false);

    glBindTexture(GL_TEXTURE_3D, texs[2]);
    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F, 64, 48, 16, 0, GL_RGBA, GL_FLOAT, NULL);

    GLuint samp = 0;
    glGenSamplers(1, &samp);

    GLuint pipe = MakePipeline();
    GLuint sepprog = MakeProgram("", GLDefaultPixel);

    // force things to be dirty
    for(int i = 0; i < 100; i++)
    {
      glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(DefaultTri), DefaultTri);
      glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(DefaultTri), DefaultTri);

      glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texs[0], 0);
      glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texs[1], 0, 0);

      glUseProgramStages(pipe, GL_FRAGMENT_SHADER_BIT, sepprog);

      glSamplerParameterf(samp, GL_TEXTURE_LOD_BIAS, 0.0f);

      glEnableVertexAttribArray(0);
    }

    while(Running())
    {
      // forcibly reference all objects to ensure we prepare AND serialise their initial contents
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texs[0]);
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, texs[1]);
      glBindTexture(GL_TEXTURE_3D, texs[2]);

      glBindFramebuffer(GL_FRAMEBUFFER, fbo);

      glBindProgramPipeline(pipe);
      glBindProgramPipeline(0);

      glBindSampler(6, samp);

      glBindFramebuffer(GL_FRAMEBUFFER, 0);

      float col[] = {0.2f, 0.2f, 0.2f, 1.0f};
      glClearBufferfv(GL_COLOR, 0, col);

      glViewport(0, 0, GLsizei(screenWidth), GLsizei(screenHeight));

      // configure the VAO. If state tracking has been corrupted this won't modify the right VAO
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), (void *)(0));
      glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), (void *)(sizeof(Vec3f)));
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V),
                            (void *)(sizeof(Vec3f) + sizeof(Vec4f)));

      glEnableVertexAttribArray(0);
      glEnableVertexAttribArray(1);
      glEnableVertexAttribArray(2);

      // upload the data to the implicit buffer binding - same thing as above this won't modify the
      // right buffer.
      Vec4f tint = Vec4f(1.0f, 1.0f, 1.0f, 1.0f);
      glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(DefaultTri), DefaultTri);
      glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(Vec4f), &tint);

      // set the bare uniform
      glUniform4f(loc, 1.0f, 1.0f, 1.0f, 1.0f);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      // trash everything so we don't get the state saved as initial contents
      glUniform4f(loc, 0.0f, 0.0f, 0.0f, 0.0f);

      glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(DefaultTri), empty);
      glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(DefaultTri), empty);

      glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 64, (void *)(0));
      glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 64, (void *)(0));
      glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 64, (void *)(0));

      glDisableVertexAttribArray(0);
      glDisableVertexAttribArray(1);
      glDisableVertexAttribArray(2);

      Present();
    }

    glDeleteSamplers(1, &samp);

    return 0;
  }
};

REGISTER_TEST();