-
Notifications
You must be signed in to change notification settings - Fork 17
/
renderer_opengl.cpp
283 lines (252 loc) · 9.09 KB
/
renderer_opengl.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
//--------------------------------------------------------------------
#define EXTERNSVCUI
#define WINDOWINERTIACAMERA_EXTERN
#include "renderer_base.h"
#include "NVFBOBox.h"
#include <nvgl/profiler_gl.hpp>
namespace glstandard
{
//-----------------------------------------------------------------------------
// Shaders
//-----------------------------------------------------------------------------
static const char *g_glslv_fur =
"#version 430\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_NV_command_list : enable\n"
"layout(std140,commandBindableNV,binding=" TOSTR(UBO_MATRIX) ") uniform matrixBuffer {\n"
" uniform mat4 mV;\n"
" uniform mat4 mP;\n"
"} matrix;\n"
"layout(location=0) in vec3 P;\n"
"layout(location=1) in vec3 N;\n"
"layout(location=2) in vec4 col;\n"
"layout(location=0) out vec4 outCol;\n"
"out gl_PerVertex {\n"
" vec4 gl_Position;\n"
"};\n"
"void main() {\n"
" gl_Position = matrix.mP * (matrix.mV * ( vec4(P, 1.0)));\n"
" vec3 NV = (matrix.mV * ( vec4(N, 0.0))).xyz;\n"
" float diff = abs(NV.x);\n"
" outCol = vec4(diff * col.rgb, col.a);\n"
"}\n"
;
static const char *g_glslf_fur =
"#version 430\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_NV_command_list : enable\n"
"layout(location=0) in vec4 inCol;\n"
"layout(location=0) out vec4 outCol;\n"
"void main() {\n"
" outCol = inCol;\n"
"}\n"
;
GLSLShader s_shaderfur;
struct BO {
GLuint Id;
GLuint Sz;
};
BO g_uboMatrix = { 0,0 };
static GLuint s_vbofur;
static GLuint s_vbofurSz;
static GLuint s_nElmts;
static GLuint s_vao = 0;
//------------------------------------------------------------------------------
// Renderer: can be OpenGL or other
//------------------------------------------------------------------------------
class RendererStandard : public Renderer
{
private:
bool m_bValid;
bool initResourcesfur();
bool deleteResourcesfur();
NVFBOBox::DownSamplingTechnique downsamplingMode;
NVFBOBox m_fboBox;
int m_winSize[2];
nvgl::ProfilerGL m_profilerGL;
public:
RendererStandard() {
m_bValid = false;
g_renderers[g_numRenderers++] = this;
}
virtual ~RendererStandard() {}
virtual const char *getName() { return "Naive Standard VBO"; }
virtual bool valid() { return m_bValid; };
virtual bool initGraphics(int w, int h, float SSScale, int MSAA);
virtual bool terminateGraphics();
virtual void display(const InertiaCamera& camera, const glm::mat4& projection);
virtual void updateMSAA(int MSAA);
virtual void updateViewport(GLint x, GLint y, GLsizei width, GLsizei height, float SSFactor);
virtual void setDownSamplingMode(int i) { downsamplingMode = (NVFBOBox::DownSamplingTechnique)i; }
};
RendererStandard s_renderer;
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initResourcesfur()
{
glCreateBuffers(1, &s_vbofur);
std::vector<Vertex> data;
buildFur(data);
s_nElmts = data.size();
s_vbofurSz = data.size() * sizeof(Vertex);
glNamedBufferData(s_vbofur, s_vbofurSz, &(data[0]), GL_STATIC_DRAW);
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::deleteResourcesfur()
{
glDeleteBuffers(1, &s_vbofur);
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::display(const InertiaCamera& camera, const glm::mat4& projection)
{
const nvgl::ProfilerGL::Section profile(m_profilerGL, "frame");
// bind the FBO
m_fboBox.Activate();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
GLuint fbo = m_fboBox.GetFBO();
//
// Update what is inside buffers
//
g_globalMatrices.mP = projection;
g_globalMatrices.mV = camera.m4_view;
glNamedBufferSubData(g_uboMatrix.Id, 0, sizeof(g_globalMatrices), &g_globalMatrices);
// ------------------------------------------------------------------------------------------
// Case of regular rendering
//
s_shaderfur.bindShader();
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// --------------------------------------------------------------------------------------
// Using regular VBO
//
glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATRIX, g_uboMatrix.Id);
glBindVertexBuffer(0, s_vbofur, 0, sizeof(Vertex));
glBindVertexBuffer(1, s_vbofur, 0, sizeof(Vertex));
glBindVertexBuffer(2, s_vbofur, 0, sizeof(Vertex));
glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3));
glVertexAttribFormat(2, 4, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3));
//
// Draw!
//
glDrawArrays(GL_TRIANGLES, 0, s_nElmts);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
//
// Blit to backbuffer
//
m_fboBox.Deactivate();
m_fboBox.Draw(downsamplingMode, 0, 0, m_winSize[0], m_winSize[1], NULL);
}
void RendererStandard::updateMSAA(int MSAA)
{
m_fboBox.resize(0, 0, -1, MSAA, -1);
m_fboBox.MakeResourcesResident();
}
//------------------------------------------------------------------------------
// this is an example of creating a piece of token buffer that would be put
// as a header before every single glDrawCommandsStatesAddressNV so that
// proper view setup (viewport) get properly done without relying on any
// typical OpenGL command.
// this approach is good avoid messing with OpenGL state machine and later could
// prevent extra driver validation
//------------------------------------------------------------------------------
void RendererStandard::updateViewport(GLint x, GLint y, GLsizei width, GLsizei height, float SSFactor)
{
m_winSize[0] = width;
m_winSize[1] = height;
m_fboBox.resize(width, height, SSFactor);
m_fboBox.MakeResourcesResident();
width = m_fboBox.getBufferWidth();
height = m_fboBox.getBufferHeight();
glLineWidth(m_fboBox.getSSFactor());
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initGraphics(int w, int h, float SSScale, int MSAA)
{
//
// some offscreen buffer
//
m_fboBox.Initialize(w, h, SSScale, MSAA, 0);
m_fboBox.MakeResourcesResident();
//
// Shader compilation
//
if (!s_shaderfur.addVertexShaderFromString(g_glslv_fur))
return false;
if (!s_shaderfur.addFragmentShaderFromString(g_glslf_fur))
return false;
if (!s_shaderfur.link())
return false;
//
// Create some UBO for later share their 64 bits
//
glCreateBuffers(1, &g_uboMatrix.Id);
g_uboMatrix.Sz = sizeof(MatrixBufferGlobal);
glNamedBufferData(g_uboMatrix.Id, g_uboMatrix.Sz, &g_globalMatrices, GL_STREAM_DRAW);
//
// Misc OGL setup
//
glClearColor(0.0f, 0.1f, 0.15f, 1.0f);
glGenVertexArrays(1, &s_vao);
glBindVertexArray(s_vao);
//
// fur
//
if (!initResourcesfur())
return false;
m_profilerGL = nvgl::ProfilerGL(&g_profiler);
m_profilerGL.init();
LOGOK("Initialized renderer %s\n", getName());
m_bValid = true;
return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::terminateGraphics()
{
m_fboBox.Finish();
deleteResourcesfur();
glDeleteVertexArrays(1, &s_vao);
s_vao = 0;
glDeleteBuffers(1, &g_uboMatrix.Id);
g_uboMatrix.Id = 0;
s_shaderfur.cleanup();
m_profilerGL.deinit();
m_bValid = false;
return true;
}
} //glstandard