Blender V4.3
eevee_precompute.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#pragma once
12
14
16
17#include <fstream>
18
19namespace blender::eevee {
20
27 private:
28 int3 table_extent_;
29 float4 *raw_data_ = nullptr;
30
31 public:
32 Precompute(draw::Manager &manager, PrecomputeType type, int3 table_extent);
34
35 /* Cast each pixel data to type `T`. */
36 template<typename T> Vector<T> data()
37 {
38 int64_t table_len = table_extent_.x * table_extent_.y * table_extent_.z;
39 Vector<T> out_data(table_len);
40 for (auto i : IndexRange(table_len)) {
41 out_data[i] = T(raw_data_[i]);
42 }
43 return out_data;
44 }
45
50 template<typename VecT>
51 static void write_to_pfm(StringRefNull name,
52 Span<VecT> pixels,
53 int64_t n_x,
54 int64_t n_y = 1,
55 int64_t n_z = 1,
56 int64_t n_w = 1)
57 {
58 BLI_STATIC_ASSERT(VecT::type_length < 4, "4 component PFM are not possible");
59
60 std::ofstream file;
61
62 /* Write PFM header. */
63 file.open(std::string(name) + ".pfm");
64 file << "PF\n";
65 file << n_x * n_z << " " << n_y * n_w << "\n";
66#ifdef __LITTLE_ENDIAN__
67 file << "-1.0\n";
68#else
69 file << "1.0\n";
70#endif
71 file.close();
72
73 /* Write binary float content. */
74 file.open(std::string(name) + ".pfm", std::ios_base::app | std::ios::out | std::ios::binary);
75 /* Iterate over destination pixels. */
76 for (int64_t y : IndexRange(n_y * n_w)) {
77 for (int64_t x : IndexRange(n_x * n_z)) {
78 int64_t src_w = y / n_y;
79 int64_t src_z = x / n_x;
80 int64_t src_y = y % n_y;
81 int64_t src_x = x % n_x;
82 int64_t src = (n_x * n_y * n_z * src_w) + (n_x * n_y * src_z) + (n_x * src_y) + src_x;
83 float3 data(0.0f);
84 for (int c : IndexRange(VecT::type_length)) {
85 data[c] = pixels[src][c];
86 }
87 file.write(reinterpret_cast<char *>(&data), sizeof(float3));
88 }
89 }
90 file.close();
91 }
92
97 template<typename VecT>
99 Span<VecT> pixels,
100 int64_t n_x,
101 int64_t n_y = 1,
102 int64_t n_z = 1,
103 int64_t n_w = 1)
104 {
105 std::ofstream file;
106
107 file.open(std::string(name) + ".hh");
108 file << "const float " << name;
109 if (n_w > 1) {
110 file << "[" << n_w << "]";
111 }
112 if (n_z > 1) {
113 file << "[" << n_z << "]";
114 }
115 if (n_y > 1) {
116 file << "[" << n_y << "]";
117 }
118 if (n_x > 1) {
119 file << "[" << n_x << "]";
120 }
121 file << "[" << VecT::type_length << "]";
122 file << " = {\n";
123 /* Print data formatted as C++ array. */
124 for (auto w : IndexRange(n_w)) {
125 if (n_w > 1) {
126 file << "{\n";
127 }
128 for (auto z : IndexRange(n_z)) {
129 if (n_z > 1 || n_w > 1) {
130 file << "{\n";
131 }
132 for (auto y : IndexRange(n_y)) {
133 if (n_y > 1 || n_z > 1 || n_w > 1) {
134 file << "{\n";
135 }
136 for (auto x : IndexRange(n_x)) {
137 if (n_x > 1 || n_y > 1 || n_z > 1 || n_w > 1) {
138 file << "{";
139 }
140 int64_t pixel_index = (n_x * n_y * n_z * w) + (n_x * n_y * z) + (n_x * y) + x;
141 for (auto c : IndexRange(VecT::type_length)) {
142 file << std::to_string(pixels[pixel_index][c]);
143 if (c + 1 < VecT::type_length) {
144 file << "f, ";
145 }
146 else {
147 file << "f";
148 }
149 }
150 if (n_x > 1 || n_y > 1 || n_z > 1 || n_w > 1) {
151 file << (x + 1 < n_x ? "}, " : "}");
152 }
153 }
154 if (n_y > 1 || n_z > 1 || n_w > 1) {
155 file << (y + 1 < n_y ? "},\n" : "}\n");
156 }
157 }
158 if (n_z > 1 || n_w > 1) {
159 file << (z + 1 < n_z ? "},\n" : "}\n");
160 }
161 }
162 if (n_w > 1) {
163 file << (w + 1 < n_w ? "},\n" : "}\n");
164 }
165 }
166 file << "};\n";
167 file.close();
168 }
169};
170
171} // namespace blender::eevee
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
static void write_to_header(StringRefNull name, Span< VecT > pixels, int64_t n_x, int64_t n_y=1, int64_t n_z=1, int64_t n_w=1)
Precompute(draw::Manager &manager, PrecomputeType type, int3 table_extent)
static void write_to_pfm(StringRefNull name, Span< VecT > pixels, int64_t n_x, int64_t n_y=1, int64_t n_z=1, int64_t n_w=1)
FILE * file
#define T
BLI_STATIC_ASSERT(MBC_BATCH_LEN< 32, "Number of batches exceeded the limit of bit fields")
VecBase< float, 3 > float3
__int64 int64_t
Definition stdint.h:89