-
Notifications
You must be signed in to change notification settings - Fork 8
/
r_composite.cpp
132 lines (102 loc) · 3.64 KB
/
r_composite.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
#include "r_composite.h"
namespace r {
///! compositeMethod
bool compositeMethod::init(const u::vector<const char *> &defines) {
if (!method::init("composite"))
return false;
for (const auto &it : defines)
method::define(it);
if (gl::has(gl::ARB_texture_rectangle))
method::define("HAS_TEXTURE_RECTANGLE");
if (!addShader(GL_VERTEX_SHADER, "shaders/final.vs"))
return false;
if (!addShader(GL_FRAGMENT_SHADER, "shaders/final.fs"))
return false;
if (!finalize({ "position" }))
return false;
m_WVP = getUniform("gWVP", uniform::kMat4);
m_colorMap = getUniform("gColorMap", uniform::kSampler);
m_colorGradingMap = getUniform("gColorGradingMap", uniform::kSampler);
m_screenSize = getUniform("gScreenSize", uniform::kVec2);
post();
return true;
}
void compositeMethod::setWVP(const m::mat4 &wvp) {
m_WVP->set(wvp);
}
void compositeMethod::setColorTextureUnit(int unit) {
m_colorMap->set(unit);
}
void compositeMethod::setColorGradingTextureUnit(int unit) {
m_colorGradingMap->set(unit);
}
void compositeMethod::setPerspective(const m::perspective &p) {
m_screenSize->set(m::vec2(p.width, p.height));
}
composite::composite()
: m_fbo(0)
, m_texture(0)
, m_width(0)
, m_height(0)
{
}
composite::~composite() {
destroy();
}
void composite::destroy() {
if (m_fbo)
gl::DeleteFramebuffers(1, &m_fbo);
if (m_texture)
gl::DeleteTextures(1, &m_texture);
}
void composite::update(const m::perspective &p) {
const size_t width = p.width;
const size_t height = p.height;
const GLenum format = gl::has(gl::ARB_texture_rectangle) ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
if (m_width == width && m_height == height)
return;
m_width = width;
m_height = height;
gl::BindTexture(format, m_texture);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA,
GL_FLOAT, nullptr);
gl::TexParameteri(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
bool composite::init(const m::perspective &p, GLuint depth) {
m_width = p.width;
m_height = p.height;
gl::GenFramebuffers(1, &m_fbo);
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
gl::GenTextures(1, &m_texture);
const GLenum format = gl::has(gl::ARB_texture_rectangle) ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
// output composite
gl::BindTexture(format, m_texture);
gl::TexImage2D(format, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_FLOAT,
nullptr);
gl::TexParameteri(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, format,
m_texture, 0);
gl::BindTexture(format, depth);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
format, depth, 0);
static GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
gl::DrawBuffers(1, drawBuffers);
const GLenum status = gl::CheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
return false;
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
return true;
}
void composite::bindWriting() {
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
}
GLuint composite::texture() const {
return m_texture;
}
}