j2k_cuda_runtime/
build_flags.rs1use crate::driver::CuResult;
2#[cfg(j2k_cuda_oxide_enabled)]
3use crate::error::CudaError;
4use std::{os::raw::c_uint, sync::OnceLock};
5
6pub(crate) const CUDA_SUCCESS: CuResult = 0;
7
8pub(crate) const CUDA_ERROR_NOT_READY: CuResult = 600;
9
10pub(crate) const PINNED_POOLED_I16_UPLOAD_MAX_BYTES: usize = 4 * 1024 * 1024;
11
12pub(crate) const DWT97_ROW_LIFT_MAX_WIDTH: i32 = 1024;
13
14pub(crate) const DWT97_ROW_LIFT_COOP_THREADS_X: c_uint = 128;
15
16pub(crate) const DWT97_ROW_LIFT_COOP_ROWS_PER_BLOCK: c_uint = 4;
17
18pub(crate) const CUDA_IDWT_TRACE_ENV_VAR: &str = "J2K_CUDA_IDWT_TRACE";
19
20#[cfg(j2k_cuda_oxide_enabled)]
21pub(crate) const REQUIRE_CUDA_OXIDE_BUILD_ENV_VAR: &str = "J2K_REQUIRE_CUDA_OXIDE_BUILD";
22
23pub(crate) const DWT97_FUSED_COLUMN_QUANTIZE_DISABLE_ENV_VAR: &str =
24 "J2K_CUDA_DISABLE_DWT97_FUSED_COLUMN_QUANTIZE";
25
26pub(crate) static CUDA_STAGE_TIMINGS_DISABLED: OnceLock<bool> = OnceLock::new();
27
28pub(crate) static DWT97_FUSED_COLUMN_QUANTIZE_DISABLED: OnceLock<bool> = OnceLock::new();
29
30pub(crate) fn cuda_stage_timings_disabled() -> bool {
31 *CUDA_STAGE_TIMINGS_DISABLED
32 .get_or_init(|| std::env::var_os("J2K_CUDA_DISABLE_STAGE_TIMINGS").is_some())
33}
34
35pub(crate) fn dwt97_fused_column_quantize_disabled() -> bool {
36 *DWT97_FUSED_COLUMN_QUANTIZE_DISABLED
37 .get_or_init(|| std::env::var_os(DWT97_FUSED_COLUMN_QUANTIZE_DISABLE_ENV_VAR).is_some())
38}
39
40#[cfg(j2k_cuda_oxide_enabled)]
41fn ensure_cuda_oxide_ptx_built(built: bool, display_name: &str) -> Result<(), CudaError> {
42 if built {
43 Ok(())
44 } else {
45 Err(CudaError::InvalidArgument {
46 message: format!(
47 "{display_name} PTX was not built; set {REQUIRE_CUDA_OXIDE_BUILD_ENV_VAR} on a Linux cuda-oxide host to require it"
48 ),
49 })
50 }
51}
52
53macro_rules! cuda_oxide_ptx_guard {
54 (feature = $feature:literal, $ensure_fn:ident, $built_const:ident, $display_name:literal, $built_cfg:meta) => {
55 #[cfg(feature = $feature)]
56 pub(crate) fn $ensure_fn() -> Result<(), CudaError> {
57 ensure_cuda_oxide_ptx_built($built_const, $display_name)
58 }
59
60 #[cfg(feature = $feature)]
61 pub(crate) const $built_const: bool = cfg!($built_cfg);
62 };
63}
64
65cuda_oxide_ptx_guard!(
66 feature = "cuda-oxide-copy-u8",
67 ensure_cuda_oxide_copy_u8_ptx_built,
68 CUDA_OXIDE_COPY_U8_PTX_BUILT,
69 "cuda-oxide CopyU8",
70 j2k_cuda_oxide_copy_u8_built
71);
72cuda_oxide_ptx_guard!(
73 feature = "cuda-oxide-j2k-encode",
74 ensure_cuda_oxide_j2k_encode_ptx_built,
75 CUDA_OXIDE_J2K_ENCODE_PTX_BUILT,
76 "cuda-oxide J2K encode",
77 j2k_cuda_oxide_j2k_encode_built
78);
79cuda_oxide_ptx_guard!(
80 feature = "cuda-oxide-j2k-decode-store",
81 ensure_cuda_oxide_j2k_decode_store_ptx_built,
82 CUDA_OXIDE_J2K_DECODE_STORE_PTX_BUILT,
83 "cuda-oxide J2K decode store",
84 j2k_cuda_oxide_j2k_decode_store_built
85);
86cuda_oxide_ptx_guard!(
87 feature = "cuda-oxide-j2k-classic-decode",
88 ensure_cuda_oxide_j2k_classic_decode_ptx_built,
89 CUDA_OXIDE_J2K_CLASSIC_DECODE_PTX_BUILT,
90 "cuda-oxide classic J2K decode",
91 j2k_cuda_oxide_j2k_classic_decode_built
92);
93cuda_oxide_ptx_guard!(
94 feature = "cuda-oxide-j2k-dequantize",
95 ensure_cuda_oxide_j2k_dequantize_ptx_built,
96 CUDA_OXIDE_J2K_DEQUANTIZE_PTX_BUILT,
97 "cuda-oxide J2K dequantize",
98 j2k_cuda_oxide_j2k_dequantize_built
99);
100cuda_oxide_ptx_guard!(
101 feature = "cuda-oxide-j2k-idwt",
102 ensure_cuda_oxide_j2k_idwt_ptx_built,
103 CUDA_OXIDE_J2K_IDWT_PTX_BUILT,
104 "cuda-oxide J2K IDWT",
105 j2k_cuda_oxide_j2k_idwt_built
106);
107cuda_oxide_ptx_guard!(
108 feature = "cuda-oxide-j2k-ml",
109 ensure_cuda_oxide_j2k_ml_ptx_built,
110 CUDA_OXIDE_J2K_ML_PTX_BUILT,
111 "cuda-oxide j2k-ml",
112 j2k_cuda_oxide_j2k_ml_built
113);
114cuda_oxide_ptx_guard!(
115 feature = "cuda-oxide-transcode",
116 ensure_cuda_oxide_transcode_ptx_built,
117 CUDA_OXIDE_TRANSCODE_PTX_BUILT,
118 "cuda-oxide transcode",
119 j2k_cuda_oxide_transcode_built
120);
121cuda_oxide_ptx_guard!(
122 feature = "cuda-oxide-htj2k-decode",
123 ensure_cuda_oxide_htj2k_decode_ptx_built,
124 CUDA_OXIDE_HTJ2K_DECODE_PTX_BUILT,
125 "cuda-oxide HTJ2K decode",
126 j2k_cuda_oxide_htj2k_decode_built
127);
128cuda_oxide_ptx_guard!(
129 feature = "cuda-oxide-htj2k-encode",
130 ensure_cuda_oxide_htj2k_encode_ptx_built,
131 CUDA_OXIDE_HTJ2K_ENCODE_PTX_BUILT,
132 "cuda-oxide HTJ2K encode",
133 j2k_cuda_oxide_htj2k_encode_built
134);
135cuda_oxide_ptx_guard!(
136 feature = "cuda-oxide-jpeg-decode",
137 ensure_cuda_oxide_jpeg_decode_ptx_built,
138 CUDA_OXIDE_JPEG_DECODE_PTX_BUILT,
139 "cuda-oxide JPEG decode",
140 j2k_cuda_oxide_jpeg_decode_built
141);
142cuda_oxide_ptx_guard!(
143 feature = "cuda-oxide-jpeg-encode",
144 ensure_cuda_oxide_jpeg_encode_ptx_built,
145 CUDA_OXIDE_JPEG_ENCODE_PTX_BUILT,
146 "cuda-oxide JPEG encode",
147 j2k_cuda_oxide_jpeg_encode_built
148);
149
150#[must_use]
154pub fn transcode_kernels_built() -> bool {
155 #[cfg(feature = "cuda-oxide-transcode")]
156 {
157 CUDA_OXIDE_TRANSCODE_PTX_BUILT
158 }
159 #[cfg(not(feature = "cuda-oxide-transcode"))]
160 {
161 false
162 }
163}