-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrenderpass.h
138 lines (125 loc) · 4.87 KB
/
renderpass.h
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
// Copyright (C) 2020-2023 Sami Väisänen
// Copyright (C) 2020-2023 Ensisoft http://www.ensisoft.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "config.h"
#include <optional>
#include "graphics/types.h"
#include "graphics/painter.h"
#include "graphics/shader_programs.h"
#include "graphics/transform.h"
namespace gfx
{
class Material;
class Drawable;
class GenericRenderPass
{
public:
GenericRenderPass(Painter& painter)
: mPainter(painter)
{}
void Draw(const Drawable& drawable, const Transform& transform, const Material& material) const
{
Painter::DrawState state;
state.write_color = true;
state.stencil_func = Painter::StencilFunc::Disabled;
state.depth_test = Painter::DepthTest::Disabled;
FlatShadedColorProgram program;
mPainter.Draw(drawable, transform, material, state, program);
}
private:
Painter& mPainter;
};
class StencilMaskPass
{
public:
using ClearValue = StencilClearValue;
using WriteValue = StencilWriteValue;
enum class StencilFunc {
Overwrite,
BitwiseAnd,
OverlapIncrement
};
StencilMaskPass(StencilClearValue clear_value, StencilWriteValue write_value, Painter& painter, StencilFunc func = StencilFunc::Overwrite)
: mStencilWriteValue(write_value)
, mStencilFunc(func)
, mPainter(painter)
{
painter.ClearStencil(clear_value);
}
StencilMaskPass(StencilWriteValue write_value, Painter& painter, StencilFunc func = StencilFunc::Overwrite)
: mStencilWriteValue(write_value)
, mStencilFunc(func)
, mPainter(painter)
{}
bool Draw(const Drawable& drawable, const Transform& transform, const Material& material) const
{
Painter::DrawState state;
state.write_color = false;
state.depth_test = Painter::DepthTest::Disabled;
state.stencil_dpass = Painter::StencilOp::WriteRef;
state.stencil_dfail = Painter::StencilOp::WriteRef;
state.stencil_ref = mStencilWriteValue;
state.stencil_mask = 0xff;
if (mStencilFunc == StencilFunc::Overwrite)
{
state.stencil_func = Painter::StencilFunc::PassAlways;
}
else if (mStencilFunc == StencilFunc::BitwiseAnd)
{
state.stencil_ref = 1;
state.stencil_func = Painter::StencilFunc::RefIsEqual;
state.stencil_fail = Painter::StencilOp::WriteZero;
}
else if (mStencilFunc == StencilFunc::OverlapIncrement)
{
state.stencil_func = Painter::StencilFunc::RefIsEqual;
state.stencil_dpass = Painter::StencilOp::Increment;
state.stencil_fail = Painter::StencilOp::WriteZero;
}
StencilShaderProgram program;
return mPainter.Draw(drawable, transform, material, state, program);
}
private:
const WriteValue mStencilWriteValue = 1;
const StencilFunc mStencilFunc;
Painter& mPainter;
};
class StencilTestColorWritePass
{
public:
using PassValue = StencilPassValue;
explicit StencilTestColorWritePass(PassValue stencil_pass_value, Painter& painter)
: mStencilRefValue(stencil_pass_value)
, mPainter(painter)
{}
bool Draw(const Drawable& drawable, const Transform& transform, const Material& material) const
{
Painter::DrawState state;
state.write_color = true;
state.depth_test = Painter::DepthTest::Disabled;
state.stencil_func = Painter::StencilFunc::RefIsEqual;
state.stencil_dpass = Painter::StencilOp::DontModify;
state.stencil_dfail = Painter::StencilOp::DontModify;
state.stencil_ref = mStencilRefValue;
state.stencil_mask = 0xff;
FlatShadedColorProgram program;
return mPainter.Draw(drawable, transform, material, state, program);
}
private:
const PassValue mStencilRefValue;
Painter& mPainter;
};
} // namespace