micropdf 0.16.0

A pure Rust PDF library - A pure Rust PDF library with fz_/pdf_ API compatibility
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! DirectX Backend (Windows)
//!
//! Provides GPU acceleration using DirectX 11 and DirectX 12

use super::super::Handle;
use super::backend::GpuDevice;
use super::types::*;
use crate::fitz::geometry::Matrix;

// ============================================================================
// DirectX 11
// ============================================================================

/// DirectX 11 device implementation
#[cfg(target_os = "windows")]
pub struct DirectX11Device {
    capabilities: GpuCapabilities,
    /// ID3D11Device
    _device: u64,
    /// ID3D11DeviceContext
    _context: u64,
}

#[cfg(target_os = "windows")]
impl DirectX11Device {
    /// Create a new DirectX 11 device
    pub fn new() -> GpuResult<Self> {
        // D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
        //     D3D11_CREATE_DEVICE_BGRA_SUPPORT, nullptr, 0,
        //     D3D11_SDK_VERSION, &device, nullptr, &context);

        let capabilities = GpuCapabilities {
            max_texture_size: 16384,
            max_texture_units: 128,
            compute_shaders: true,
            geometry_shaders: true,
            tessellation: true,
            max_msaa_samples: 8,
            float_textures: true,
            instancing: true,
            vram_mb: 0,
            device_name: "DirectX 11 Device".into(),
            vendor_name: "Unknown".into(),
            driver_version: "DirectX 11.1".into(),
        };

        Ok(Self {
            capabilities,
            _device: 0,
            _context: 0,
        })
    }
}

#[cfg(target_os = "windows")]
impl GpuDevice for DirectX11Device {
    fn backend(&self) -> GpuBackendType {
        GpuBackendType::DirectX11
    }

    fn capabilities(&self) -> &GpuCapabilities {
        &self.capabilities
    }

    fn create_texture(&self, width: u32, height: u32, format: GpuFormat) -> GpuResult<GpuTexture> {
        // D3D11_TEXTURE2D_DESC desc = {};
        // desc.Width = width;
        // desc.Height = height;
        // desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        // desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
        // device->CreateTexture2D(&desc, nullptr, &texture);

        let mut texture = GpuTexture::new(width, height, format, GpuBackendType::DirectX11);
        texture.native_handle = 1;
        Ok(texture)
    }

    fn destroy_texture(&self, _texture: &GpuTexture) -> GpuResult<()> {
        // texture->Release();
        Ok(())
    }

    fn upload_texture(
        &self,
        texture: &mut GpuTexture,
        _data: &[u8],
        _stride: u32,
    ) -> GpuResult<()> {
        // context->UpdateSubresource(texture, 0, nullptr, data, stride, 0);
        let _ = texture;
        Ok(())
    }

    fn download_texture(
        &self,
        texture: &GpuTexture,
        _data: &mut [u8],
        _stride: u32,
    ) -> GpuResult<()> {
        // Create staging texture, copy, map, read
        let _ = texture;
        Ok(())
    }

    fn clear_texture(&self, texture: &mut GpuTexture, color: [f32; 4]) -> GpuResult<()> {
        // context->ClearRenderTargetView(rtv, color);
        let _ = (texture, color);
        Ok(())
    }

    fn create_shader(&self, _vertex_src: &str, _fragment_src: &str) -> GpuResult<GpuShader> {
        // D3DCompile for vertex shader
        // D3DCompile for pixel shader
        // device->CreateVertexShader
        // device->CreatePixelShader
        let shader = GpuShader::new("shader", GpuBackendType::DirectX11);
        Ok(shader)
    }

    fn destroy_shader(&self, _shader: &GpuShader) -> GpuResult<()> {
        Ok(())
    }

    fn create_buffer(&self, size: usize, usage: GpuBufferUsage) -> GpuResult<GpuBuffer> {
        // D3D11_BUFFER_DESC desc = {};
        // desc.ByteWidth = size;
        // desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // or INDEX/CONSTANT
        // device->CreateBuffer(&desc, nullptr, &buffer);
        let buffer = GpuBuffer::new(size, usage, GpuBackendType::DirectX11);
        Ok(buffer)
    }

    fn destroy_buffer(&self, _buffer: &GpuBuffer) -> GpuResult<()> {
        Ok(())
    }

    fn upload_buffer(&self, buffer: &mut GpuBuffer, _data: &[u8], _offset: usize) -> GpuResult<()> {
        // context->Map, memcpy, Unmap
        let _ = buffer;
        Ok(())
    }

    fn render_page(
        &self,
        page: Handle,
        texture: &mut GpuTexture,
        transform: &Matrix,
    ) -> GpuResult<()> {
        // Set render target
        // Set viewport
        // Set shaders
        // Set constant buffers with transform
        // Draw indexed
        let _ = (page, texture, transform);
        Ok(())
    }

    fn composite(
        &self,
        src: &GpuTexture,
        dst: &mut GpuTexture,
        x: i32,
        y: i32,
        blend_mode: GpuBlendMode,
    ) -> GpuResult<()> {
        let _ = (src, dst, x, y, blend_mode);
        Ok(())
    }

    fn draw_quad(
        &self,
        texture: &GpuTexture,
        dst: &mut GpuTexture,
        src_rect: [f32; 4],
        dst_rect: [f32; 4],
        color: [f32; 4],
    ) -> GpuResult<()> {
        let _ = (texture, dst, src_rect, dst_rect, color);
        Ok(())
    }

    fn flush(&self) -> GpuResult<()> {
        // context->Flush();
        Ok(())
    }

    fn finish(&self) -> GpuResult<()> {
        // Create query, end query, wait for data
        Ok(())
    }
}

// ============================================================================
// DirectX 12
// ============================================================================

/// DirectX 12 device implementation
#[cfg(target_os = "windows")]
pub struct DirectX12Device {
    capabilities: GpuCapabilities,
    /// ID3D12Device
    _device: u64,
    /// ID3D12CommandQueue
    _command_queue: u64,
    /// ID3D12CommandAllocator
    _command_allocator: u64,
    /// ID3D12GraphicsCommandList
    _command_list: u64,
    /// ID3D12Fence
    _fence: u64,
    /// Fence value
    _fence_value: u64,
}

#[cfg(target_os = "windows")]
impl DirectX12Device {
    /// Create a new DirectX 12 device
    pub fn new() -> GpuResult<Self> {
        // D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&device));
        // Create command queue, allocator, list, fence

        let capabilities = GpuCapabilities {
            max_texture_size: 16384,
            max_texture_units: 128,
            compute_shaders: true,
            geometry_shaders: true,
            tessellation: true,
            max_msaa_samples: 8,
            float_textures: true,
            instancing: true,
            vram_mb: 0,
            device_name: "DirectX 12 Device".into(),
            vendor_name: "Unknown".into(),
            driver_version: "DirectX 12".into(),
        };

        Ok(Self {
            capabilities,
            _device: 0,
            _command_queue: 0,
            _command_allocator: 0,
            _command_list: 0,
            _fence: 0,
            _fence_value: 0,
        })
    }
}

#[cfg(target_os = "windows")]
impl GpuDevice for DirectX12Device {
    fn backend(&self) -> GpuBackendType {
        GpuBackendType::DirectX12
    }

    fn capabilities(&self) -> &GpuCapabilities {
        &self.capabilities
    }

    fn create_texture(&self, width: u32, height: u32, format: GpuFormat) -> GpuResult<GpuTexture> {
        // D3D12_RESOURCE_DESC desc = {};
        // desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
        // desc.Width = width;
        // desc.Height = height;
        // device->CreateCommittedResource(...);

        let mut texture = GpuTexture::new(width, height, format, GpuBackendType::DirectX12);
        texture.native_handle = 1;
        Ok(texture)
    }

    fn destroy_texture(&self, _texture: &GpuTexture) -> GpuResult<()> {
        Ok(())
    }

    fn upload_texture(
        &self,
        texture: &mut GpuTexture,
        _data: &[u8],
        _stride: u32,
    ) -> GpuResult<()> {
        // Create upload heap, copy to upload heap, copy to texture via command list
        let _ = texture;
        Ok(())
    }

    fn download_texture(
        &self,
        texture: &GpuTexture,
        _data: &mut [u8],
        _stride: u32,
    ) -> GpuResult<()> {
        // Copy to readback heap, map, read
        let _ = texture;
        Ok(())
    }

    fn clear_texture(&self, texture: &mut GpuTexture, color: [f32; 4]) -> GpuResult<()> {
        // commandList->ClearRenderTargetView(rtv, color, 0, nullptr);
        let _ = (texture, color);
        Ok(())
    }

    fn create_shader(&self, _vertex_src: &str, _fragment_src: &str) -> GpuResult<GpuShader> {
        // Compile HLSL to DXIL
        // Create root signature
        // Create pipeline state object
        let shader = GpuShader::new("shader", GpuBackendType::DirectX12);
        Ok(shader)
    }

    fn destroy_shader(&self, _shader: &GpuShader) -> GpuResult<()> {
        Ok(())
    }

    fn create_buffer(&self, size: usize, usage: GpuBufferUsage) -> GpuResult<GpuBuffer> {
        // device->CreateCommittedResource for buffer
        let buffer = GpuBuffer::new(size, usage, GpuBackendType::DirectX12);
        Ok(buffer)
    }

    fn destroy_buffer(&self, _buffer: &GpuBuffer) -> GpuResult<()> {
        Ok(())
    }

    fn upload_buffer(&self, buffer: &mut GpuBuffer, _data: &[u8], _offset: usize) -> GpuResult<()> {
        let _ = buffer;
        Ok(())
    }

    fn render_page(
        &self,
        page: Handle,
        texture: &mut GpuTexture,
        transform: &Matrix,
    ) -> GpuResult<()> {
        // Reset command allocator
        // Reset command list
        // Transition texture to render target
        // Set render targets
        // Set viewport, scissor
        // Set pipeline state
        // Set root signature
        // Set descriptor heaps
        // Draw
        // Transition texture back
        // Close command list
        // Execute
        let _ = (page, texture, transform);
        Ok(())
    }

    fn composite(
        &self,
        src: &GpuTexture,
        dst: &mut GpuTexture,
        x: i32,
        y: i32,
        blend_mode: GpuBlendMode,
    ) -> GpuResult<()> {
        let _ = (src, dst, x, y, blend_mode);
        Ok(())
    }

    fn draw_quad(
        &self,
        texture: &GpuTexture,
        dst: &mut GpuTexture,
        src_rect: [f32; 4],
        dst_rect: [f32; 4],
        color: [f32; 4],
    ) -> GpuResult<()> {
        let _ = (texture, dst, src_rect, dst_rect, color);
        Ok(())
    }

    fn flush(&self) -> GpuResult<()> {
        // Execute command list
        Ok(())
    }

    fn finish(&self) -> GpuResult<()> {
        // Signal fence, wait for fence
        Ok(())
    }
}

// ============================================================================
// HLSL Shaders
// ============================================================================

/// Quad vertex shader (HLSL)
pub const QUAD_VERTEX_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
    float4x4 projection;
    float4x4 transform;
    float4 color;
};

struct VSInput {
    float2 position : POSITION;
    float2 texcoord : TEXCOORD0;
};

struct VSOutput {
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

VSOutput main(VSInput input) {
    VSOutput output;
    output.position = mul(projection, mul(transform, float4(input.position, 0.0, 1.0)));
    output.texcoord = input.texcoord;
    return output;
}
"#;

/// Quad pixel shader (HLSL)
pub const QUAD_PIXEL_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
    float4x4 projection;
    float4x4 transform;
    float4 color;
};

Texture2D tex : register(t0);
SamplerState samp : register(s0);

struct PSInput {
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

float4 main(PSInput input) : SV_TARGET {
    return tex.Sample(samp, input.texcoord) * color;
}
"#;

/// Path vertex shader (HLSL)
pub const PATH_VERTEX_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
    float4x4 projection;
    float4x4 transform;
    float4 color;
};

struct VSInput {
    float2 position : POSITION;
};

struct VSOutput {
    float4 position : SV_POSITION;
};

VSOutput main(VSInput input) {
    VSOutput output;
    output.position = mul(projection, mul(transform, float4(input.position, 0.0, 1.0)));
    return output;
}
"#;

/// Path pixel shader (HLSL)
pub const PATH_PIXEL_SHADER_HLSL: &str = r#"
cbuffer Constants : register(b0) {
    float4x4 projection;
    float4x4 transform;
    float4 color;
};

float4 main() : SV_TARGET {
    return color;
}
"#;

#[cfg(all(test, target_os = "windows"))]
mod tests {
    use super::*;

    #[test]
    fn test_create_dx11_device() {
        let device = DirectX11Device::new().unwrap();
        assert_eq!(device.backend(), GpuBackendType::DirectX11);
    }

    #[test]
    fn test_create_dx12_device() {
        let device = DirectX12Device::new().unwrap();
        assert_eq!(device.backend(), GpuBackendType::DirectX12);
    }
}