./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 112
0.0%
Branches:
0 / 160
0.0%
Functions:
0 / 17
0.0%

libs/render/src/eu/render/material.cc
Line Branch Exec Source
1 #include "eu/render/material.h"
2
3 #include "eu/assert/assert.h"
4 #include "eu/base/cint.h"
5
6 #include "eu/render/renderer.h"
7 // #include "eu/render/assets.h"
8 #include "eu/render/camera.h"
9 #include "eu/render/render_settings.h"
10 #include "eu/render/shader.h"
11 #include "eu/render/shader_resource.h"
12 #include "eu/render/world.h"
13 #include "eu/render/state.h"
14 #include "eu/render/constants.h"
15
16
17 namespace eu::render
18 {
19
20 ✗ UnlitMaterial::UnlitMaterial(const ShaderResource& resource)
21 ✗ : shader_container(&resource.unlit_shader_container)
22 {
23 ✗ }
24
25 ✗ void UnlitMaterial::use_shader(const RenderContext& rc)
26 {
27 ✗ shader_from_container(*shader_container, rc).program->use();
28 ✗ }
29
30 ✗ void set_optional_mat(
31 ShaderProgram* program, const std::optional<Uniform>& uniform, const std::optional<m4>& transform
32 )
33 {
34
0/6
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 7 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 12 not taken.
✗ if (uniform && transform)
35 {
36 ✗ program->set_mat(*uniform, *transform);
37 }
38 else
39 {
40
0/6
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 20 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 20 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 29 not taken.
✗ ASSERT(uniform.has_value() == false);
41
0/6
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 28 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 28 not taken.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 32 not taken.
✗ ASSERT(transform.has_value() == false);
42 }
43 ✗ }
44
45 ✗ v3 vec_from_lin(const Lin_rgb& lin)
46 {
47 ✗ return { lin.r, lin.g, lin.b };
48 }
49
50 ✗ void UnlitMaterial::set_uniforms(
51 const RenderContext& rc, const CompiledCamera&, const std::optional<m4>& world_from_local
52 )
53 {
54 ✗ const auto& shader = shader_from_container(*shader_container, rc);
55
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 11 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 11 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
✗ shader.program->set_vec4(shader.tint_color_uni, { vec_from_lin(linear_from_srgb(color, rc.gamma)), alpha });
56 ✗ set_optional_mat(shader.program.get(), shader.world_from_local_uni, world_from_local);
57 ✗ }
58
59 ✗ void UnlitMaterial::bind_textures(const RenderContext& rc, State* states, Assets* assets)
60 {
61 ✗ std::shared_ptr<Texture2d> t = texture;
62
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (t == nullptr)
63 {
64 ✗ t = assets->white;
65 }
66
67
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 11 not taken.
✗ bind_texture_2d(states, shader_from_container(*shader_container, rc).tex_diffuse_uniform, *t);
68 ✗ }
69
70 ✗ void UnlitMaterial::apply_lights(const RenderContext&, const Lights&, const RenderSettings&, State*, Assets*)
71 {
72 // no lights for unlit material
73 ✗ }
74
75 ✗ bool UnlitMaterial::is_transparent() const
76 {
77 // todo(Gustav): improve transparency
78 ✗ return alpha < kk::alpha_transparency_limit;
79 }
80
81 ✗ DefaultMaterial::DefaultMaterial(const ShaderResource& resource)
82 ✗ : shader_container(&resource.default_shader_container)
83 {
84 ✗ }
85
86 ✗ void DefaultMaterial::use_shader(const RenderContext& rc)
87 {
88 ✗ shader_from_container(*shader_container, rc).program->use();
89 ✗ }
90
91 ✗ void DefaultMaterial::set_uniforms(
92 const RenderContext& rc, const CompiledCamera& cc, const std::optional<m4>& world_from_local
93 )
94 {
95 ✗ const auto& shader = shader_from_container(*shader_container, rc);
96
97
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 25 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 25 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 25 not taken.
✗ shader.program->set_vec4(shader.tint_color_uni, {vec_from_lin(linear_from_srgb(color, rc.gamma)), alpha});
98
0/4
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 28 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 28 not taken.
✗ shader.program->set_vec3(shader.ambient_tint_uni, vec_from_lin(linear_from_srgb(ambient_tint, rc.gamma)));
99
0/4
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 30 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 30 not taken.
✗ shader.program->set_vec3(shader.specular_color_uni, vec_from_lin(linear_from_srgb(specular_color, rc.gamma)));
100 ✗ shader.program->set_float(shader.shininess_uni, shininess);
101 ✗ shader.program->set_float(shader.emissive_factor_uni, emissive_factor);
102
103 ✗ set_optional_mat(shader.program.get(), shader.world_from_local_uni, world_from_local);
104 ✗ shader.program->set_vec3(shader.view_position_uni, cc.position);
105 ✗ }
106
107 ✗ std::shared_ptr<Texture2d> get_or_white(Assets* assets, std::shared_ptr<Texture2d> t)
108 {
109
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (t == nullptr)
110 {
111 ✗ return assets->white;
112 }
113 else
114 {
115 ✗ return t;
116 }
117 };
118
119 ✗ std::shared_ptr<Texture2d> get_or_black(Assets* assets, std::shared_ptr<Texture2d> t)
120 {
121
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (t == nullptr)
122 {
123 ✗ return assets->black;
124 }
125 else
126 {
127 ✗ return t;
128 }
129 };
130
131 ✗ void DefaultMaterial::bind_textures(const RenderContext& rc, State* states, Assets* assets)
132 {
133 ✗ const auto& shader = shader_from_container(*shader_container, rc);
134
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 22 not taken.
✗ bind_texture_2d(states, shader.tex_diffuse_uniform, *get_or_white(assets, diffuse));
135
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 28 not taken.
✗ bind_texture_2d(states, shader.tex_specular_uniform, *get_or_white(assets, specular));
136
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 34 not taken.
✗ bind_texture_2d(states, shader.tex_emissive_uniform, *get_or_black(assets, emissive));
137 ✗ }
138
139 ✗ void DefaultMaterial::apply_lights(
140 const RenderContext& rc, const Lights& lights, const RenderSettings& settings, State* states, Assets* assets
141 )
142 {
143
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 157 not taken.
✗ const auto& shader = shader_from_container(*shader_container, rc);
144
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 118 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 118 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 118 not taken.
✗ shader.program->set_vec3(shader.light_ambient_color_uni, vec_from_lin(linear_from_srgb(lights.ambient_color, rc.gamma)) * lights.ambient_strength);
145
146 ✗ constexpr auto no_directional_light = ([]() {
147 DirectionalLight p;
148 p.color = colors::black;
149 p.diffuse_strength = 0.0f;
150 p.specular_strength = 0.0f;
151 return p;
152 })();
153 ✗ constexpr auto no_point_light = ([]() {
154 PointLight p;
155 p.color = colors::black;
156 p.diffuse_strength = 0.0f;
157 p.specular_strength = 0.0f;
158 return p;
159 })();
160 ✗ auto no_frustum_light = ([]() {
161 ✗ FrustumLight p;
162 ✗ p.color = colors::black;
163 ✗ p.diffuse_strength = 0.0f;
164 ✗ p.specular_strength = 0.0f;
165 ✗ return p;
166
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 121 not taken.
✗ })();
167
168 // todo(Gustav): graph the most influential lights instead of the first N lights
169
0/2
✗ Branch 32 → 10 not taken.
✗ Branch 32 → 33 not taken.
✗ for (int index = 0; index < settings.number_of_directional_lights; index += 1)
170 {
171 ✗ const auto& p = sizet_from_int(index) < lights.directional_lights.size()
172
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
✗ ? lights.directional_lights[sizet_from_int(index)]
173 ✗ : no_directional_light;
174 ✗ const auto& u = shader.directional_lights[sizet_from_int(index)];
175
0/6
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 122 not taken.
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 122 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 122 not taken.
✗ shader.program->set_vec3(u.light_diffuse_color_uni, vec_from_lin(linear_from_srgb(p.color, settings.gamma)) * p.diffuse_strength);
176
0/6
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 125 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 125 not taken.
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 125 not taken.
✗ shader.program->set_vec3(u.light_specular_color_uni, vec_from_lin(linear_from_srgb(p.color, settings.gamma)) * p.specular_strength);
177
0/2
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 128 not taken.
✗ const auto vectors = create_vectors(p);
178
0/2
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 128 not taken.
✗ shader.program->set_vec3(u.dir_uni, vectors.front);
179 }
180
181
0/2
✗ Branch 58 → 34 not taken.
✗ Branch 58 → 59 not taken.
✗ for (int index = 0; index < settings.number_of_point_lights; index += 1)
182 {
183 ✗ const auto& p = sizet_from_int(index) < lights.point_lights.size()
184
0/2
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 39 not taken.
✗ ? lights.point_lights[sizet_from_int(index)]
185 ✗ : no_point_light
186 ;
187 ✗ const auto& u = shader.point_lights[sizet_from_int(index)];
188
0/6
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 129 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 129 not taken.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 129 not taken.
✗ shader.program->set_vec3(u.light_diffuse_color_uni, vec_from_lin(linear_from_srgb(p.color, settings.gamma)) * p.diffuse_strength);
189
0/6
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 132 not taken.
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 132 not taken.
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 132 not taken.
✗ shader.program->set_vec3(u.light_specular_color_uni, vec_from_lin(linear_from_srgb(p.color, settings.gamma)) * p.specular_strength);
190
0/2
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 155 not taken.
✗ shader.program->set_vec3(u.light_world_uni, p.position);
191
0/4
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 135 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 135 not taken.
✗ shader.program->set_vec4(u.light_attenuation_uni, {p.min_range, p.max_range, p.curve.slope, p.curve.threshold});
192 }
193
194
0/2
✗ Branch 96 → 60 not taken.
✗ Branch 96 → 97 not taken.
✗ for (int index = 0; index < settings.number_of_frustum_lights; index += 1)
195 {
196
0/2
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 65 not taken.
✗ const auto& p = sizet_from_int(index) < lights.frustum_lights.size() ? lights.frustum_lights[sizet_from_int(index)]
197 ✗ : no_frustum_light;
198 ✗ const auto& u = shader.frustum_lights[sizet_from_int(index)];
199
0/6
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 136 not taken.
✗ Branch 71 → 72 not taken.
✗ Branch 71 → 136 not taken.
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 136 not taken.
✗ shader.program->set_vec3(u.diffuse_uni, vec_from_lin(linear_from_srgb(p.color, settings.gamma)) * p.diffuse_strength);
200
0/6
✗ Branch 74 → 75 not taken.
✗ Branch 74 → 139 not taken.
✗ Branch 76 → 77 not taken.
✗ Branch 76 → 139 not taken.
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 139 not taken.
✗ shader.program->set_vec3(u.specular_uni, vec_from_lin(linear_from_srgb(p.color, settings.gamma)) * p.specular_strength);
201
0/2
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 151 not taken.
✗ shader.program->set_vec3(u.world_pos_uni, p.position);
202
0/4
✗ Branch 81 → 82 not taken.
✗ Branch 81 → 142 not taken.
✗ Branch 82 → 83 not taken.
✗ Branch 82 → 142 not taken.
✗ shader.program->set_vec4(u.attenuation_uni, {p.min_range, p.max_range, p.curve.slope, p.curve.threshold});
203
204
0/4
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 143 not taken.
✗ Branch 84 → 85 not taken.
✗ Branch 84 → 143 not taken.
✗ const auto view_from_world = create_view_from_world_mat(p.position, create_vectors(p.rotation));
205
0/2
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 151 not taken.
✗ const auto clip_from_view = m4::create_perspective(p.fov, p.aspect, 0.1f, p.max_range);
206
0/4
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 144 not taken.
✗ Branch 88 → 89 not taken.
✗ Branch 88 → 144 not taken.
✗ shader.program->set_mat(u.clip_from_world_uni, clip_from_view * view_from_world);
207
208
0/2
✗ Branch 92 → 93 not taken.
✗ Branch 92 → 145 not taken.
✗ bind_texture_2d(states, u.tex_cookie_uniform, *get_or_white(assets, p.cookie));
209 }
210
211 // directional light shadows
212
0/8
✗ Branch 97 → 98 not taken.
✗ Branch 97 → 104 not taken.
✗ Branch 98 → 99 not taken.
✗ Branch 98 → 155 not taken.
✗ Branch 99 → 100 not taken.
✗ Branch 99 → 104 not taken.
✗ Branch 101 → 102 not taken.
✗ Branch 101 → 152 not taken.
✗ ASSERT(rc.shadow_context);
213
0/2
✗ Branch 104 → 105 not taken.
✗ Branch 104 → 106 not taken.
✗ auto* shadow_map = rc.shadow_context != nullptr ? rc.shadow_context->directional_shadow_map : nullptr;
214
0/2
✗ Branch 107 → 108 not taken.
✗ Branch 107 → 109 not taken.
✗ if (shadow_map != nullptr)
215 {
216
0/2
✗ Branch 108 → 111 not taken.
✗ Branch 108 → 155 not taken.
✗ bind_texture_2d(states, shader.tex_directional_light_depth_uni, *shadow_map);
217 }
218 else
219 {
220
0/2
✗ Branch 110 → 111 not taken.
✗ Branch 110 → 155 not taken.
✗ bind_texture_2d(states, shader.tex_directional_light_depth_uni, *assets->white);
221 }
222
223 ✗ const auto directional_shadow_clip_from_world
224
0/2
✗ Branch 111 → 112 not taken.
✗ Branch 111 → 113 not taken.
✗ = rc.shadow_context != nullptr ? rc.shadow_context->directional_shadow_clip_from_world : m4_identity;
225
0/2
✗ Branch 115 → 116 not taken.
✗ Branch 115 → 155 not taken.
✗ shader.program->set_mat(shader.directional_shadow_clip_from_world_uni, directional_shadow_clip_from_world);
226 ✗ }
227
228 ✗ bool DefaultMaterial::is_transparent() const
229 {
230 // todo(Gustav): improve transparency
231 ✗ return alpha < kk::alpha_transparency_limit;
232 }
233
234 } // namespace eu::render
235