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
use crate::core::render_target::*;
pub struct RenderTargetArray<'a, 'b, T: TextureDataType> {
context: Context,
id: crate::context::Framebuffer,
color_texture: Option<&'a Texture2DArray<T>>,
depth_texture: Option<&'b DepthTargetTexture2DArray>,
}
impl<'a, 'b> RenderTargetArray<'a, 'b, u8> {
pub fn new_depth(
context: &Context,
depth_texture: &'b DepthTargetTexture2DArray,
) -> ThreeDResult<Self> {
Ok(Self {
context: context.clone(),
id: new_framebuffer(context)?,
color_texture: None,
depth_texture: Some(depth_texture),
})
}
}
impl<'a, 'b, T: TextureDataType> RenderTargetArray<'a, 'b, T> {
pub fn new(
context: &Context,
color_texture: &'a Texture2DArray<T>,
depth_texture: &'b DepthTargetTexture2DArray,
) -> ThreeDResult<Self> {
Ok(Self {
context: context.clone(),
id: new_framebuffer(context)?,
color_texture: Some(color_texture),
depth_texture: Some(depth_texture),
})
}
pub fn new_color(
context: &Context,
color_texture: &'a Texture2DArray<T>,
) -> ThreeDResult<Self> {
Ok(Self {
context: context.clone(),
id: new_framebuffer(context)?,
color_texture: Some(color_texture),
depth_texture: None,
})
}
pub(crate) fn new_depth_internal(
context: &Context,
depth_texture: &'b DepthTargetTexture2DArray,
) -> ThreeDResult<Self> {
Ok(Self {
context: context.clone(),
id: new_framebuffer(context)?,
color_texture: None,
depth_texture: Some(depth_texture),
})
}
pub fn write(
&self,
color_layers: &[u32],
depth_layer: u32,
clear_state: ClearState,
render: impl FnOnce() -> ThreeDResult<()>,
) -> ThreeDResult<()> {
self.bind(Some(color_layers), Some(depth_layer))?;
clear(
&self.context,
&ClearState {
red: self.color_texture.and(clear_state.red),
green: self.color_texture.and(clear_state.green),
blue: self.color_texture.and(clear_state.blue),
alpha: self.color_texture.and(clear_state.alpha),
depth: self.depth_texture.and(clear_state.depth),
},
);
render()?;
if let Some(color_texture) = self.color_texture {
color_texture.generate_mip_maps();
}
Ok(())
}
#[deprecated = "Use RenderTarget::copy_from_array or Screen::copy_from_array instead"]
pub fn copy_to(
&self,
color_layer: u32,
depth_layer: u32,
destination: CopyDestination<T>,
viewport: Viewport,
write_mask: WriteMask,
) -> ThreeDResult<()> {
let copy = || {
let fragment_shader_source = "
uniform sampler2DArray colorMap;
uniform sampler2DArray depthMap;
uniform int colorLayer;
uniform int depthLayer;
in vec2 uv;
layout (location = 0) out vec4 color;
void main()
{
color = texture(colorMap, vec3(uv, colorLayer));
gl_FragDepth = texture(depthMap, vec3(uv, depthLayer)).r;
}";
self.context.effect(fragment_shader_source, |effect| {
if let Some(tex) = self.color_texture {
effect.use_texture_array("colorMap", tex)?;
effect.use_uniform_int("colorLayer", &(color_layer as i32))?;
}
if let Some(tex) = self.depth_texture {
effect.use_texture_array("depthMap", tex)?;
effect.use_uniform_int("depthLayer", &(depth_layer as i32))?;
}
effect.apply(
RenderStates {
depth_test: DepthTest::Always,
write_mask,
..Default::default()
},
viewport,
)
})
};
match destination {
CopyDestination::RenderTarget(other) => {
other.write(ClearState::none(), copy)?;
}
CopyDestination::Screen => {
Screen::write(&self.context, ClearState::none(), copy)?;
}
CopyDestination::ColorTexture(tex) => {
if self.color_texture.is_none() {
Err(CoreError::RenderTargetCopy(
"color".to_string(),
"depth".to_string(),
))?;
}
tex.write(ClearState::none(), copy)?;
}
CopyDestination::DepthTexture(tex) => {
if self.depth_texture.is_none() {
Err(CoreError::RenderTargetCopy(
"depth".to_string(),
"color".to_string(),
))?;
}
tex.write(None, copy)?;
}
}
Ok(())
}
fn bind(&self, color_layers: Option<&[u32]>, depth_layer: Option<u32>) -> ThreeDResult<()> {
self.context
.bind_framebuffer(consts::DRAW_FRAMEBUFFER, Some(&self.id));
if let Some(color_texture) = self.color_texture {
if let Some(color_layers) = color_layers {
self.context.draw_buffers(
&(0..color_layers.len())
.map(|i| consts::COLOR_ATTACHMENT0 + i as u32)
.collect::<Vec<u32>>(),
);
for channel in 0..color_layers.len() {
color_texture.bind_as_color_target(color_layers[channel], channel as u32);
}
}
}
if let Some(depth_texture) = self.depth_texture {
if let Some(depth_layer) = depth_layer {
depth_texture.bind_as_depth_target(depth_layer);
}
}
#[cfg(feature = "debug")]
check(&self.context)?;
Ok(())
}
}
impl<T: TextureDataType> Drop for RenderTargetArray<'_, '_, T> {
fn drop(&mut self) {
self.context.delete_framebuffer(Some(&self.id));
}
}