Blender V5.0
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
10
11#pragma once
12
14
16
17#include "draw_manager.hh"
18
19#include <fstream>
20
21namespace blender::eevee {
22
23using namespace draw;
24
31 private:
32 int3 table_extent_;
33 float4 *raw_data_ = nullptr;
34
35 public:
36 Precompute(draw::Manager &manager, PrecomputeType type, int3 table_extent);
38
39 /* Cast each pixel data to type `T`. */
40 template<typename T> Vector<T> data()
41 {
42 int64_t table_len = table_extent_.x * table_extent_.y * table_extent_.z;
43 Vector<T> out_data(table_len);
44 for (auto i : IndexRange(table_len)) {
45 out_data[i] = T(raw_data_[i]);
46 }
47 return out_data;
48 }
49
54 template<typename VecT>
56 Span<VecT> pixels,
57 int64_t n_x,
58 int64_t n_y = 1,
59 int64_t n_z = 1,
60 int64_t n_w = 1)
61 {
62 BLI_STATIC_ASSERT(VecT::type_length < 4, "4 component PFM are not possible");
63
64 std::ofstream file;
65
66 /* Write PFM header. */
67 file.open(std::string(name) + ".pfm");
68 file << "PF\n";
69 file << n_x * n_z << " " << n_y * n_w << "\n";
70 /* NOTE: this is endianness-sensitive.
71 * Big endian system would have needed `1.0` value instead. */
72 file << "-1.0\n";
73 file.close();
74
75 /* Write binary float content. */
76 file.open(std::string(name) + ".pfm", std::ios_base::app | std::ios::out | std::ios::binary);
77 /* Iterate over destination pixels. */
78 for (int64_t y : IndexRange(n_y * n_w)) {
79 for (int64_t x : IndexRange(n_x * n_z)) {
80 int64_t src_w = y / n_y;
81 int64_t src_z = x / n_x;
82 int64_t src_y = y % n_y;
83 int64_t src_x = x % n_x;
84 int64_t src = (n_x * n_y * n_z * src_w) + (n_x * n_y * src_z) + (n_x * src_y) + src_x;
85 float3 data(0.0f);
86 for (int c : IndexRange(VecT::type_length)) {
87 data[c] = pixels[src][c];
88 }
89 file.write(reinterpret_cast<char *>(&data), sizeof(float3));
90 }
91 }
92 file.close();
93 }
94
99 template<typename VecT>
101 Span<VecT> pixels,
102 int64_t n_x,
103 int64_t n_y = 1,
104 int64_t n_z = 1,
105 int64_t n_w = 1)
106 {
107 std::ofstream file;
108
109 file.open(std::string(name) + ".hh");
110 file << "const float " << name;
111 if (n_w > 1) {
112 file << "[" << n_w << "]";
113 }
114 if (n_z > 1) {
115 file << "[" << n_z << "]";
116 }
117 if (n_y > 1) {
118 file << "[" << n_y << "]";
119 }
120 if (n_x > 1) {
121 file << "[" << n_x << "]";
122 }
123 file << "[" << VecT::type_length << "]";
124 file << " = {\n";
125 /* Print data formatted as C++ array. */
126 for (auto w : IndexRange(n_w)) {
127 if (n_w > 1) {
128 file << "{\n";
129 }
130 for (auto z : IndexRange(n_z)) {
131 if (n_z > 1 || n_w > 1) {
132 file << "{\n";
133 }
134 for (auto y : IndexRange(n_y)) {
135 if (n_y > 1 || n_z > 1 || n_w > 1) {
136 file << "{\n";
137 }
138 for (auto x : IndexRange(n_x)) {
139 if (n_x > 1 || n_y > 1 || n_z > 1 || n_w > 1) {
140 file << "{";
141 }
142 int64_t pixel_index = (n_x * n_y * n_z * w) + (n_x * n_y * z) + (n_x * y) + x;
143 for (auto c : IndexRange(VecT::type_length)) {
144 file << std::to_string(pixels[pixel_index][c]);
145 if (c + 1 < VecT::type_length) {
146 file << "f, ";
147 }
148 else {
149 file << "f";
150 }
151 }
152 if (n_x > 1 || n_y > 1 || n_z > 1 || n_w > 1) {
153 file << (x + 1 < n_x ? "}, " : "}");
154 }
155 }
156 if (n_y > 1 || n_z > 1 || n_w > 1) {
157 file << (y + 1 < n_y ? "},\n" : "}\n");
158 }
159 }
160 if (n_z > 1 || n_w > 1) {
161 file << (z + 1 < n_z ? "},\n" : "}\n");
162 }
163 }
164 if (n_w > 1) {
165 file << (w + 1 < n_w ? "},\n" : "}\n");
166 }
167 }
168 file << "};\n";
169 file.close();
170 }
171};
172
173} // namespace blender::eevee
long long int int64_t
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)
#define T
BLI_STATIC_ASSERT(MBC_BATCH_LEN< 64, "Number of batches exceeded the limit of bit fields")
VecBase< float, 4 > float4
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
const char * name
i
Definition text_draw.cc:230