1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "tools/render/program_state.h"
16
17 #include "tools/render/sdl_util.h"
18
19 namespace quic_trace {
20 namespace render {
21
22 // This has to mirror the definition of ProgramStateData in the .h file.
23 const char* kProgramStateData = R"(
24 layout(std140) uniform ProgramStateBlock {
25 vec2 window;
26 vec2 offset;
27 vec2 viewport;
28 float dpi_scale;
29 } program_state;
30 )";
31
ProgramState(const ProgramStateData * data)32 ProgramState::ProgramState(const ProgramStateData* data) : data_(data) {
33 glBufferData(GL_UNIFORM_BUFFER, sizeof(*data), data, GL_DYNAMIC_COPY);
34 }
35
Bind(const GlProgram & program) const36 void ProgramState::Bind(const GlProgram& program) const {
37 GLuint block_index = glGetUniformBlockIndex(*program, "ProgramStateBlock");
38 CHECK_NE(block_index, GL_INVALID_INDEX);
39 glUniformBlockBinding(*program, block_index, 1);
40 glBindBufferBase(GL_UNIFORM_BUFFER, 1, *buffer_);
41 }
42
Refresh()43 void ProgramState::Refresh() {
44 glBindBuffer(GL_UNIFORM_BUFFER, *buffer_);
45 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(*data_), data_);
46 }
47
48 } // namespace render
49 } // namespace quic_trace
50