-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
lib.rs
283 lines (256 loc) · 7.98 KB
/
lib.rs
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
// Remove this when we decide on how to handle multithreaded rendering (especially on wasm)
#![allow(clippy::arc_with_non_send_sync)]
// This lint is helpful, but right now we have too many instances of it.
// TODO: Remove this once all instances are fixed.
#![allow(clippy::needless_pass_by_ref_mut)]
use crate::backend::ActiveFrame;
use crate::bitmaps::BitmapSamplers;
use crate::buffer_pool::{BufferPool, PoolEntry};
use crate::descriptors::Quad;
use crate::mesh::BitmapBinds;
use crate::pipelines::Pipelines;
use crate::target::{RenderTarget, SwapChainTarget};
use crate::utils::{
capture_image, create_buffer_with_data, format_list, get_backend_names, BufferDimensions,
};
use bytemuck::{Pod, Zeroable};
use descriptors::Descriptors;
use enum_map::Enum;
use ruffle_render::backend::RawTexture;
use ruffle_render::bitmap::{BitmapHandle, BitmapHandleImpl, PixelRegion, SyncHandle};
use ruffle_render::shape_utils::GradientType;
use ruffle_render::tessellator::{Gradient as TessGradient, Vertex as TessVertex};
use std::cell::{Cell, OnceCell};
use std::sync::Arc;
use swf::GradientSpread;
pub use wgpu;
type Error = Box<dyn std::error::Error>;
#[macro_use]
pub mod utils;
mod bitmaps;
mod context3d;
mod globals;
mod pipelines;
mod pixel_bender;
pub mod target;
pub mod backend;
mod blend;
mod buffer_builder;
mod buffer_pool;
#[cfg(feature = "clap")]
pub mod clap;
pub mod descriptors;
mod dynamic_transforms;
mod filters;
mod layouts;
mod mesh;
mod shaders;
mod surface;
impl BitmapHandleImpl for Texture {}
pub fn as_texture(handle: &BitmapHandle) -> &Texture {
<dyn BitmapHandleImpl>::downcast_ref(&*handle.0).unwrap()
}
pub fn raw_texture_as_texture(handle: &dyn RawTexture) -> &wgpu::Texture {
<dyn RawTexture>::downcast_ref(handle).unwrap()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Enum)]
pub enum MaskState {
NoMask,
DrawMaskStencil,
DrawMaskedContent,
ClearMaskStencil,
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
pub struct Transforms {
world_matrix: [[f32; 4]; 4],
mult_color: [f32; 4],
add_color: [f32; 4],
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
struct TextureTransforms {
u_matrix: [[f32; 4]; 4],
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
struct PosVertex {
position: [f32; 2],
}
impl From<TessVertex> for PosVertex {
fn from(vertex: TessVertex) -> Self {
Self {
position: [vertex.x, vertex.y],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
struct PosColorVertex {
position: [f32; 2],
color: [f32; 4],
}
impl From<TessVertex> for PosColorVertex {
fn from(vertex: TessVertex) -> Self {
Self {
position: [vertex.x, vertex.y],
color: [
f32::from(vertex.color.r) / 255.0,
f32::from(vertex.color.g) / 255.0,
f32::from(vertex.color.b) / 255.0,
f32::from(vertex.color.a) / 255.0,
],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
struct GradientUniforms {
focal_point: f32,
interpolation: i32,
shape: i32,
repeat: i32,
}
impl From<TessGradient> for GradientUniforms {
fn from(gradient: TessGradient) -> Self {
Self {
focal_point: gradient.focal_point.to_f32().clamp(-0.98, 0.98),
interpolation: (gradient.interpolation == swf::GradientInterpolation::LinearRgb) as i32,
shape: match gradient.gradient_type {
GradientType::Linear => 1,
GradientType::Radial => 2,
GradientType::Focal => 3,
},
repeat: match gradient.repeat_mode {
GradientSpread::Pad => 1,
GradientSpread::Reflect => 2,
GradientSpread::Repeat => 3,
},
}
}
}
#[derive(Debug)]
pub enum QueueSyncHandle {
AlreadyCopied {
index: Option<wgpu::SubmissionIndex>,
buffer: PoolEntry<wgpu::Buffer, BufferDimensions>,
copy_dimensions: BufferDimensions,
descriptors: Arc<Descriptors>,
},
NotCopied {
handle: BitmapHandle,
copy_area: PixelRegion,
descriptors: Arc<Descriptors>,
pool: Arc<BufferPool<wgpu::Buffer, BufferDimensions>>,
},
}
impl SyncHandle for QueueSyncHandle {}
impl QueueSyncHandle {
pub fn capture<R, F: FnOnce(&[u8], u32) -> R>(
self,
with_rgba: F,
frame: &mut ActiveFrame,
) -> R {
match self {
QueueSyncHandle::AlreadyCopied {
index,
buffer,
copy_dimensions,
descriptors,
} => capture_image(
&descriptors.device,
&buffer,
©_dimensions,
index,
with_rgba,
),
QueueSyncHandle::NotCopied {
handle,
copy_area,
descriptors,
pool,
} => {
let texture = as_texture(&handle);
let buffer_dimensions = BufferDimensions::new(
copy_area.width() as usize,
copy_area.height() as usize,
texture.texture.format(),
);
let buffer = pool.take(&descriptors, buffer_dimensions.clone());
frame.command_encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
texture: &texture.texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: copy_area.x_min,
y: copy_area.y_min,
z: 0,
},
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyBuffer {
buffer: &buffer,
layout: wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(buffer_dimensions.padded_bytes_per_row),
rows_per_image: None,
},
},
wgpu::Extent3d {
width: copy_area.width(),
height: copy_area.height(),
depth_or_array_layers: 1,
},
);
let index = frame.submit_direct(&descriptors);
let image = capture_image(
&descriptors.device,
&buffer,
&buffer_dimensions,
Some(index),
with_rgba,
);
// After we've read pixels from a texture enough times, we'll store this buffer so that
// future reads will be faster (it'll copy as part of the draw process instead)
texture
.copy_count
.set(texture.copy_count.get().saturating_add(1));
image
}
}
}
}
#[derive(Debug)]
pub struct Texture {
pub(crate) texture: Arc<wgpu::Texture>,
bind_linear: OnceCell<BitmapBinds>,
bind_nearest: OnceCell<BitmapBinds>,
copy_count: Cell<u8>,
}
impl Texture {
pub fn bind_group(
&self,
smoothed: bool,
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
quad: &Quad,
handle: BitmapHandle,
samplers: &BitmapSamplers,
) -> &BitmapBinds {
let bind = match smoothed {
true => &self.bind_linear,
false => &self.bind_nearest,
};
bind.get_or_init(|| {
BitmapBinds::new(
device,
layout,
samplers.get_sampler(false, smoothed),
&quad.texture_transforms,
0 as wgpu::BufferAddress,
self.texture.create_view(&Default::default()),
create_debug_label!("Bitmap {:?} bind group (smoothed: {})", handle.0, smoothed),
)
})
}
}