RavEngine
Loading...
Searching...
No Matches
NvFlowResourceCPU.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2014-2022 NVIDIA Corporation. All rights reserved.
26
27// Workaround to scope includes per shader
28#ifdef NV_FLOW_CPU_SHADER
29#undef NV_FLOW_SHADER_TYPES_H
30#undef NV_FLOW_SHADER_HLSLI
31#undef NV_FLOW_RAY_MARCH_PARAMS_H
32#undef NV_FLOW_RAY_MARCH_HLSLI
33#undef NV_FLOW_RAY_MARCH_COMMON_HLSLI
34#endif
35
36// Disabled by default, to save build time
37#define NV_FLOW_CPU_SHADER_DISABLE
38
39#ifndef NV_FLOW_RESOURCE_CPU_H
40#define NV_FLOW_RESOURCE_CPU_H
41
42#include "NvFlowContext.h"
43#include <math.h>
44#include <atomic>
45#include <string.h>
46
47typedef NvFlowUint NvFlowCPU_Uint;
48
49struct NvFlowCPU_Float2;
50struct NvFlowCPU_Float3;
51struct NvFlowCPU_Float4;
53
54struct NvFlowCPU_Int2;
55struct NvFlowCPU_Int3;
56struct NvFlowCPU_Int4;
57
58struct NvFlowCPU_Uint2;
59struct NvFlowCPU_Uint3;
60struct NvFlowCPU_Uint4;
61
62NV_FLOW_INLINE int NvFlowCPU_max(int a, int b)
63{
64 return a > b ? a : b;
65}
66
67NV_FLOW_INLINE int NvFlowCPU_min(int a, int b)
68{
69 return a < b ? a : b;
70}
71
72NV_FLOW_INLINE float NvFlowCPU_round(float v)
73{
74 return roundf(v);
75}
76
77NV_FLOW_INLINE float NvFlowCPU_abs(float v)
78{
79 return fabsf(v);
80}
81
82NV_FLOW_INLINE float NvFlowCPU_floor(float v)
83{
84 return floorf(v);
85}
86
87NV_FLOW_INLINE int NvFlowCPU_abs(int v)
88{
89 return v < 0 ? -v : v;
90}
91
92NV_FLOW_INLINE float NvFlowCPU_sqrt(float v)
93{
94 return sqrtf(v);
95}
96
97NV_FLOW_INLINE float NvFlowCPU_exp(float v)
98{
99 return expf(v);
100}
101
102NV_FLOW_INLINE float NvFlowCPU_pow(float a, float b)
103{
104 return powf(a, b);
105}
106
107NV_FLOW_INLINE float NvFlowCPU_log2(float v)
108{
109 return log2f(v);
110}
111
112NV_FLOW_INLINE float NvFlowCPU_min(float a, float b)
113{
114 //return fminf(a, b);
115 return a < b ? a : b;
116}
117
118NV_FLOW_INLINE float NvFlowCPU_max(float a, float b)
119{
120 //return fmaxf(a, b);
121 return a > b ? a : b;
122}
123
124NV_FLOW_INLINE float NvFlowCPU_clamp(float v, float min, float max)
125{
126 return NvFlowCPU_max(min, NvFlowCPU_min(v, max));
127}
128
130{
131 float x, y;
132
134 NvFlowCPU_Float2(float x, float y) : x(x), y(y) {}
135
136 NV_FLOW_INLINE NvFlowCPU_Float2(const NvFlowCPU_Int2& rhs);
137
138 NvFlowCPU_Float2 operator+(const NvFlowCPU_Float2& rhs) const { return NvFlowCPU_Float2(x + rhs.x, y + rhs.y); }
139 NvFlowCPU_Float2 operator-(const NvFlowCPU_Float2& rhs) const { return NvFlowCPU_Float2(x - rhs.x, y - rhs.y); }
140 NvFlowCPU_Float2 operator*(const NvFlowCPU_Float2& rhs) const { return NvFlowCPU_Float2(x * rhs.x, y * rhs.y); }
141 NvFlowCPU_Float2 operator/(const NvFlowCPU_Float2& rhs) const { return NvFlowCPU_Float2(x / rhs.x, y / rhs.y); }
142
143 NvFlowCPU_Float2 operator+(const float& rhs) const { return NvFlowCPU_Float2(x + rhs, y + rhs); }
144 NvFlowCPU_Float2 operator-(const float& rhs) const { return NvFlowCPU_Float2(x - rhs, y - rhs); }
145 NvFlowCPU_Float2 operator*(const float& rhs) const { return NvFlowCPU_Float2(x * rhs, y * rhs); }
146 NvFlowCPU_Float2 operator/(const float& rhs) const { return NvFlowCPU_Float2(x / rhs, y / rhs); }
147
148 NvFlowCPU_Float2& operator+=(const NvFlowCPU_Float2& rhs) { x += rhs.x; y += rhs.y; return *this; }
149 NvFlowCPU_Float2& operator-=(const NvFlowCPU_Float2& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
150 NvFlowCPU_Float2& operator*=(const NvFlowCPU_Float2& rhs) { x *= rhs.x; y *= rhs.y; return *this; }
151 NvFlowCPU_Float2& operator/=(const NvFlowCPU_Float2& rhs) { x /= rhs.x; y /= rhs.y; return *this; }
152
153 NvFlowCPU_Float2& operator+=(const float& rhs) { x += rhs; y += rhs; return *this; }
154 NvFlowCPU_Float2& operator-=(const float& rhs) { x -= rhs; y -= rhs; return *this; }
155 NvFlowCPU_Float2& operator*=(const float& rhs) { x *= rhs; y *= rhs; return *this; }
156 NvFlowCPU_Float2& operator/=(const float& rhs) { x /= rhs; y /= rhs; return *this; }
157
158 NvFlowCPU_Float2 operator+() const { return NvFlowCPU_Float2(+x, +y); }
159 NvFlowCPU_Float2 operator-() const { return NvFlowCPU_Float2(-x, -y); }
160};
161
162NV_FLOW_INLINE NvFlowCPU_Float2 operator+(const float& lhs, const NvFlowCPU_Float2& rhs) { return NvFlowCPU_Float2(lhs + rhs.x, lhs + rhs.y); }
163NV_FLOW_INLINE NvFlowCPU_Float2 operator-(const float& lhs, const NvFlowCPU_Float2& rhs) { return NvFlowCPU_Float2(lhs - rhs.x, lhs - rhs.y); }
164NV_FLOW_INLINE NvFlowCPU_Float2 operator*(const float& lhs, const NvFlowCPU_Float2& rhs) { return NvFlowCPU_Float2(lhs * rhs.x, lhs * rhs.y); }
165NV_FLOW_INLINE NvFlowCPU_Float2 operator/(const float& lhs, const NvFlowCPU_Float2& rhs) { return NvFlowCPU_Float2(lhs / rhs.x, lhs / rhs.y); }
166
167NV_FLOW_INLINE NvFlowCPU_Float2 NvFlowCPU_floor(NvFlowCPU_Float2 v)
168{
169 return NvFlowCPU_Float2(floorf(v.x), floorf(v.y));
170}
171
173{
174 float x, y, z;
175
177 NvFlowCPU_Float3(float x, float y, float z) : x(x), y(y), z(z) {}
178 NV_FLOW_INLINE NvFlowCPU_Float3(const NvFlowCPU_Int3& v);
179
180 NvFlowCPU_Float3 operator+(const NvFlowCPU_Float3& rhs) const { return NvFlowCPU_Float3(x + rhs.x, y + rhs.y, z + rhs.z); }
181 NvFlowCPU_Float3 operator-(const NvFlowCPU_Float3& rhs) const { return NvFlowCPU_Float3(x - rhs.x, y - rhs.y, z - rhs.z); }
182 NvFlowCPU_Float3 operator*(const NvFlowCPU_Float3& rhs) const { return NvFlowCPU_Float3(x * rhs.x, y * rhs.y, z * rhs.z); }
183 NvFlowCPU_Float3 operator/(const NvFlowCPU_Float3& rhs) const { return NvFlowCPU_Float3(x / rhs.x, y / rhs.y, z / rhs.z); }
184
185 NvFlowCPU_Float3 operator+(const float& rhs) const { return NvFlowCPU_Float3(x + rhs, y + rhs, z + rhs); }
186 NvFlowCPU_Float3 operator-(const float& rhs) const { return NvFlowCPU_Float3(x - rhs, y - rhs, z - rhs); }
187 NvFlowCPU_Float3 operator*(const float& rhs) const { return NvFlowCPU_Float3(x * rhs, y * rhs, z * rhs); }
188 NvFlowCPU_Float3 operator/(const float& rhs) const { return NvFlowCPU_Float3(x / rhs, y / rhs, z / rhs); }
189
190 NvFlowCPU_Float3& operator+=(const NvFlowCPU_Float3& rhs) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; }
191 NvFlowCPU_Float3& operator-=(const NvFlowCPU_Float3& rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; }
192 NvFlowCPU_Float3& operator*=(const NvFlowCPU_Float3& rhs) { x *= rhs.x; y *= rhs.y; z *= rhs.z; return *this; }
193 NvFlowCPU_Float3& operator/=(const NvFlowCPU_Float3& rhs) { x /= rhs.x; y /= rhs.y; z /= rhs.z; return *this; }
194
195 NvFlowCPU_Float3& operator+=(const float& rhs) { x += rhs; y += rhs; z += rhs; return *this; }
196 NvFlowCPU_Float3& operator-=(const float& rhs) { x -= rhs; y -= rhs; z -= rhs; return *this; }
197 NvFlowCPU_Float3& operator*=(const float& rhs) { x *= rhs; y *= rhs; z *= rhs; return *this; }
198 NvFlowCPU_Float3& operator/=(const float& rhs) { x /= rhs; y /= rhs; z /= rhs; return *this; }
199
200 NvFlowCPU_Float3 operator+() const { return NvFlowCPU_Float3(+x, +y, +z); }
201 NvFlowCPU_Float3 operator-() const { return NvFlowCPU_Float3(-x, -y, -z); }
202};
203
204NV_FLOW_INLINE NvFlowCPU_Float3 operator*(const float& lhs, const NvFlowCPU_Float3& rhs) { return NvFlowCPU_Float3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z); }
205
206NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_abs(NvFlowCPU_Float3 v)
207{
208 return NvFlowCPU_Float3(fabsf(v.x), fabsf(v.y), fabsf(v.z));
209}
210
211NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_floor(NvFlowCPU_Float3 v)
212{
213 return NvFlowCPU_Float3(floorf(v.x), floorf(v.y), floorf(v.z));
214}
215
216NV_FLOW_INLINE float NvFlowCPU_length(NvFlowCPU_Float3 v)
217{
218 return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
219}
220
221NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_max(NvFlowCPU_Float3 a, NvFlowCPU_Float3 b)
222{
223 return NvFlowCPU_Float3(NvFlowCPU_max(a.x, b.x), NvFlowCPU_max(a.y, b.y), NvFlowCPU_max(a.z, b.z));
224}
225
226NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_min(NvFlowCPU_Float3 a, NvFlowCPU_Float3 b)
227{
228 return NvFlowCPU_Float3(NvFlowCPU_min(a.x, b.x), NvFlowCPU_min(a.y, b.y), NvFlowCPU_min(a.z, b.z));
229}
230
231NV_FLOW_INLINE float NvFlowCPU_dot(NvFlowCPU_Float3 a, NvFlowCPU_Float3 b)
232{
233 return a.x * b.x + a.y * b.y + a.z * b.z;
234}
235
236NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_normalize(NvFlowCPU_Float3 v)
237{
238 float length = NvFlowCPU_length(v);
239 if (length > 0.f)
240 {
241 v /= length;
242 }
243 return v;
244}
245
247{
248 float x, y, z, w;
249
251 NvFlowCPU_Float4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
252 NvFlowCPU_Float4(const NvFlowCPU_Float3& rhs, float w) : x(rhs.x), y(rhs.y), z(rhs.z), w(w) {}
253
254 NvFlowCPU_Float3& rgb() { return *((NvFlowCPU_Float3*)this); }
255 NvFlowCPU_Float2& rg() { return *((NvFlowCPU_Float2*)this); }
256 float& r() { return *((float*)this); }
257 NvFlowCPU_Float2& ba() { return *((NvFlowCPU_Float2*)&z); }
258
259 const NvFlowCPU_Float3& rgb() const { return *((const NvFlowCPU_Float3*)this); }
260 const NvFlowCPU_Float2& rg() const { return *((const NvFlowCPU_Float2*)this); }
261 const float& r() const { return *((const float*)this); }
262 const NvFlowCPU_Float2& ba() const { return *((const NvFlowCPU_Float2*)&z); }
263
264 NvFlowCPU_Float4 operator+(const NvFlowCPU_Float4& rhs) const { return NvFlowCPU_Float4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w); }
265 NvFlowCPU_Float4 operator-(const NvFlowCPU_Float4& rhs) const { return NvFlowCPU_Float4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w); }
266 NvFlowCPU_Float4 operator*(const NvFlowCPU_Float4& rhs) const { return NvFlowCPU_Float4(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w); }
267 NvFlowCPU_Float4 operator/(const NvFlowCPU_Float4& rhs) const { return NvFlowCPU_Float4(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w); }
268
269 NvFlowCPU_Float4 operator+(const float& rhs) const { return NvFlowCPU_Float4(x + rhs, y + rhs, z + rhs, w + rhs); }
270 NvFlowCPU_Float4 operator-(const float& rhs) const { return NvFlowCPU_Float4(x - rhs, y - rhs, z - rhs, w - rhs); }
271 NvFlowCPU_Float4 operator*(const float& rhs) const { return NvFlowCPU_Float4(x * rhs, y * rhs, z * rhs, w * rhs); }
272 NvFlowCPU_Float4 operator/(const float& rhs) const { return NvFlowCPU_Float4(x / rhs, y / rhs, z / rhs, w / rhs); }
273
274 NvFlowCPU_Float4& operator+=(const NvFlowCPU_Float4& rhs) { x += rhs.x; y += rhs.y; z += rhs.z; w += rhs.w; return *this; }
275 NvFlowCPU_Float4& operator-=(const NvFlowCPU_Float4& rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; w -= rhs.w; return *this; }
276 NvFlowCPU_Float4& operator*=(const NvFlowCPU_Float4& rhs) { x *= rhs.x; y *= rhs.y; z *= rhs.z; w *= rhs.w; return *this; }
277 NvFlowCPU_Float4& operator/=(const NvFlowCPU_Float4& rhs) { x /= rhs.x; y /= rhs.y; z /= rhs.z; w /= rhs.w; return *this; }
278
279 NvFlowCPU_Float4& operator*=(const float& rhs) { x *= rhs; y *= rhs; z *= rhs; w *= rhs; return *this; }
280};
281
282NV_FLOW_INLINE NvFlowCPU_Float4 operator*(const float& lhs, const NvFlowCPU_Float4& rhs) { return NvFlowCPU_Float4(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z, lhs * rhs.w); }
283
284NV_FLOW_INLINE float NvFlowCPU_dot(NvFlowCPU_Float4 a, NvFlowCPU_Float4 b)
285{
286 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
287}
288
289NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_max(NvFlowCPU_Float4 a, NvFlowCPU_Float4 b)
290{
291 return NvFlowCPU_Float4(NvFlowCPU_max(a.x, b.x), NvFlowCPU_max(a.y, b.y), NvFlowCPU_max(a.z, b.z), NvFlowCPU_max(a.w, b.w));
292}
293
294NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_min(NvFlowCPU_Float4 a, NvFlowCPU_Float4 b)
295{
296 return NvFlowCPU_Float4(NvFlowCPU_min(a.x, b.x), NvFlowCPU_min(a.y, b.y), NvFlowCPU_min(a.z, b.z), NvFlowCPU_min(a.w, b.w));
297}
298
299NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_sign(NvFlowCPU_Float4 v)
300{
301 return NvFlowCPU_Float4(
302 v.x == 0.f ? 0.f : (v.x < 0.f ? -1.f : +1.f),
303 v.y == 0.f ? 0.f : (v.y < 0.f ? -1.f : +1.f),
304 v.z == 0.f ? 0.f : (v.z < 0.f ? -1.f : +1.f),
305 v.w == 0.f ? 0.f : (v.w < 0.f ? -1.f : +1.f)
306 );
307}
308
309NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_abs(NvFlowCPU_Float4 v)
310{
311 return NvFlowCPU_Float4(fabsf(v.x), fabsf(v.y), fabsf(v.z), fabsf(v.w));
312}
313
315{
316 NvFlowCPU_Float4 x, y, z, w;
317
319 NvFlowCPU_Float4x4(const NvFlowCPU_Float4& x, const NvFlowCPU_Float4& y, const NvFlowCPU_Float4& z, const NvFlowCPU_Float4& w) : x(x), y(y), z(z), w(w) {}
320};
321
322NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_mul(const NvFlowCPU_Float4& x, const NvFlowCPU_Float4x4 A)
323{
324 return NvFlowCPU_Float4(
325 { A.x.x * x.x + A.x.y * x.y + A.x.z * x.z + A.x.w * x.w },
326 { A.y.x * x.x + A.y.y * x.y + A.y.z * x.z + A.y.w * x.w },
327 { A.z.x * x.x + A.z.y * x.y + A.z.z * x.z + A.z.w * x.w },
328 { A.w.x * x.x + A.w.y * x.y + A.w.z * x.z + A.w.w * x.w }
329 );
330}
331
333{
334 int x, y;
335
336 NvFlowCPU_Int2() {}
337 NvFlowCPU_Int2(int x, int y) : x(x), y(y) {}
338 NvFlowCPU_Int2(const NvFlowCPU_Float2& rhs) : x(int(rhs.x)), y(int(rhs.y)) {}
339 NV_FLOW_INLINE NvFlowCPU_Int2(const NvFlowCPU_Uint2& rhs);
340
341 NvFlowCPU_Int2 operator+(const NvFlowCPU_Int2& rhs) const { return NvFlowCPU_Int2(x + rhs.x, y + rhs.y); }
342 NvFlowCPU_Int2 operator-(const NvFlowCPU_Int2& rhs) const { return NvFlowCPU_Int2(x - rhs.x, y - rhs.y); }
343 NvFlowCPU_Int2 operator*(const NvFlowCPU_Int2& rhs) const { return NvFlowCPU_Int2(x * rhs.x, y * rhs.y); }
344 NvFlowCPU_Int2 operator/(const NvFlowCPU_Int2& rhs) const { return NvFlowCPU_Int2(x / rhs.x, y / rhs.y); }
345
346 NvFlowCPU_Int2 operator+(const int& rhs) const { return NvFlowCPU_Int2(x + rhs, y + rhs); }
347 NvFlowCPU_Int2 operator-(const int& rhs) const { return NvFlowCPU_Int2(x - rhs, y - rhs); }
348 NvFlowCPU_Int2 operator*(const int& rhs) const { return NvFlowCPU_Int2(x * rhs, y * rhs); }
349 NvFlowCPU_Int2 operator/(const int& rhs) const { return NvFlowCPU_Int2(x / rhs, y / rhs); }
350};
351
352NV_FLOW_INLINE NvFlowCPU_Float2::NvFlowCPU_Float2(const NvFlowCPU_Int2& rhs) : x(float(rhs.x)), y(float(rhs.y)) {}
353
354NV_FLOW_INLINE NvFlowCPU_Int2 NvFlowCPU_max(NvFlowCPU_Int2 a, NvFlowCPU_Int2 b)
355{
356 return NvFlowCPU_Int2(NvFlowCPU_max(a.x, b.x), NvFlowCPU_max(a.y, b.y));
357}
358
359NV_FLOW_INLINE NvFlowCPU_Int2 NvFlowCPU_min(NvFlowCPU_Int2 a, NvFlowCPU_Int2 b)
360{
361 return NvFlowCPU_Int2(NvFlowCPU_min(a.x, b.x), NvFlowCPU_min(a.y, b.y));
362}
363
365{
366 int x, y, z;
367
368 NvFlowCPU_Int3() {}
369 NvFlowCPU_Int3(int x, int y, int z) : x(x), y(y), z(z) {}
370 NV_FLOW_INLINE NvFlowCPU_Int3(const NvFlowCPU_Uint3& v);
371 NV_FLOW_INLINE NvFlowCPU_Int3(const NvFlowCPU_Float3& v);
372
373 NvFlowCPU_Int2& rg() { return *((NvFlowCPU_Int2*)this); }
374 int& r() { return *((int*)this); }
375
376 NvFlowCPU_Int3 operator+(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x + rhs.x, y + rhs.y, z + rhs.z); }
377 NvFlowCPU_Int3 operator-(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x - rhs.x, y - rhs.y, z - rhs.z); }
378 NvFlowCPU_Int3 operator*(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x * rhs.x, y * rhs.y, z * rhs.z); }
379 NvFlowCPU_Int3 operator/(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x / rhs.x, y / rhs.y, z / rhs.z); }
380
381 NvFlowCPU_Int3& operator+=(const NvFlowCPU_Int3& rhs) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; }
382 NvFlowCPU_Int3& operator-=(const NvFlowCPU_Int3& rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; }
383 NvFlowCPU_Int3& operator*=(const NvFlowCPU_Int3& rhs) { x *= rhs.x; y *= rhs.y; z *= rhs.z; return *this; }
384 NvFlowCPU_Int3& operator/=(const NvFlowCPU_Int3& rhs) { x /= rhs.x; y /= rhs.y; z /= rhs.z; return *this; }
385
386 NV_FLOW_INLINE NvFlowCPU_Int3 operator>>(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x >> rhs.x, y >> rhs.y, z >> rhs.z); }
387 NV_FLOW_INLINE NvFlowCPU_Int3 operator<<(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x << rhs.x, y << rhs.y, z << rhs.z); }
388 NV_FLOW_INLINE NvFlowCPU_Int3 operator&(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x & rhs.x, y & rhs.y, z & rhs.z); }
389 NV_FLOW_INLINE NvFlowCPU_Int3 operator|(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Int3(x | rhs.x, y | rhs.y, z | rhs.z); }
390
391 NV_FLOW_INLINE NvFlowCPU_Int3 operator>>(const int& rhs) const { return NvFlowCPU_Int3(x >> rhs, y >> rhs, z >> rhs); }
392 NV_FLOW_INLINE NvFlowCPU_Int3 operator<<(const int& rhs) const { return NvFlowCPU_Int3(x << rhs, y << rhs, z << rhs); }
393 NV_FLOW_INLINE NvFlowCPU_Int3 operator>>(const NvFlowCPU_Uint& rhs) const { return NvFlowCPU_Int3(x >> rhs, y >> rhs, z >> rhs); }
394 NV_FLOW_INLINE NvFlowCPU_Int3 operator<<(const NvFlowCPU_Uint& rhs) const { return NvFlowCPU_Int3(x << rhs, y << rhs, z << rhs); }
395
396 NV_FLOW_INLINE NvFlowCPU_Int3 operator>>(const NvFlowCPU_Uint3& rhs) const;
397 NV_FLOW_INLINE NvFlowCPU_Int3 operator<<(const NvFlowCPU_Uint3& rhs) const;
398};
399
400NV_FLOW_INLINE NvFlowCPU_Int3 NvFlowCPU_max(NvFlowCPU_Int3 a, NvFlowCPU_Int3 b)
401{
402 return NvFlowCPU_Int3(NvFlowCPU_max(a.x, b.x), NvFlowCPU_max(a.y, b.y), NvFlowCPU_max(a.z, b.z));
403}
404
405NV_FLOW_INLINE NvFlowCPU_Int3 NvFlowCPU_min(NvFlowCPU_Int3 a, NvFlowCPU_Int3 b)
406{
407 return NvFlowCPU_Int3(NvFlowCPU_min(a.x, b.x), NvFlowCPU_min(a.y, b.y), NvFlowCPU_min(a.z, b.z));
408}
409
411{
412 int x, y, z, w;
413
414 NvFlowCPU_Int4() {}
415 NvFlowCPU_Int4(int x, int y, int z, int w) : x(x), y(y), z(z), w(w) {}
416 NvFlowCPU_Int4(const NvFlowCPU_Int2& a, const NvFlowCPU_Int2& b) : x(a.x), y(a.y), z(b.x), w(b.y) {}
417 NvFlowCPU_Int4(const NvFlowCPU_Int3& rhs, int w) : x(rhs.x), y(rhs.y), z(rhs.z), w(w) {}
419
420 NvFlowCPU_Int3& rgb() { return *((NvFlowCPU_Int3*)this); }
421 NvFlowCPU_Int2& rg() { return *((NvFlowCPU_Int2*)this); }
422 int& r() { return *((int*)this); }
423 NvFlowCPU_Int2& ba() { return *((NvFlowCPU_Int2*)&z); }
424
425 const NvFlowCPU_Int3& rgb()const { return *((const NvFlowCPU_Int3*)this); }
426 const NvFlowCPU_Int2& rg()const { return *((const NvFlowCPU_Int2*)this); }
427 const int& r()const { return *((const int*)this); }
428 const NvFlowCPU_Int2& ba()const { return *((const NvFlowCPU_Int2*)&z); }
429
430 NvFlowCPU_Int4 operator+(const NvFlowCPU_Int4& rhs) const { return NvFlowCPU_Int4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w); }
431};
432
434{
435 NvFlowUint x, y;
436
437 NvFlowCPU_Uint2() {}
438 NvFlowCPU_Uint2(NvFlowUint x, NvFlowUint y) : x(x), y(y) {}
439 NvFlowCPU_Uint2(const NvFlowCPU_Int2& rhs) : x(rhs.x), y(rhs.y) {}
440};
441
442NV_FLOW_INLINE NvFlowCPU_Int2::NvFlowCPU_Int2(const NvFlowCPU_Uint2& rhs) : x(rhs.x), y(rhs.y) {}
443
445{
446 NvFlowUint x, y, z;
447
448 NvFlowCPU_Uint3() {}
449 NvFlowCPU_Uint3(NvFlowUint x, NvFlowUint y, NvFlowUint z) : x(x), y(y), z(z) {}
450 NV_FLOW_INLINE NvFlowCPU_Uint3(const NvFlowCPU_Int3& v);
451
452 NvFlowCPU_Uint2& rg() { return *((NvFlowCPU_Uint2*)this); }
453 NvFlowCPU_Uint& r() { return *((NvFlowCPU_Uint*)this); }
454
455 NvFlowCPU_Uint3 operator+(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x + rhs.x, y + rhs.y, z + rhs.z); }
456 NvFlowCPU_Uint3 operator-(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x - rhs.x, y - rhs.y, z - rhs.z); }
457 NvFlowCPU_Uint3 operator*(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x * rhs.x, y * rhs.y, z * rhs.z); }
458 NvFlowCPU_Uint3 operator/(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x / rhs.x, y / rhs.y, z / rhs.z); }
459
460 NV_FLOW_INLINE NvFlowCPU_Uint3 operator&(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x & rhs.x, y & rhs.y, z & rhs.z); }
461 NV_FLOW_INLINE NvFlowCPU_Uint3 operator|(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x | rhs.x, y | rhs.y, z | rhs.z); }
462 NV_FLOW_INLINE NvFlowCPU_Uint3 operator>>(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x >> rhs.x, y >> rhs.y, z >> rhs.z); }
463 NV_FLOW_INLINE NvFlowCPU_Uint3 operator<<(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Uint3(x << rhs.x, y << rhs.y, z << rhs.z); }
464
465 NV_FLOW_INLINE NvFlowCPU_Uint3 operator>>(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Uint3(x >> rhs.x, y >> rhs.y, z >> rhs.z); }
466 NV_FLOW_INLINE NvFlowCPU_Uint3 operator<<(const NvFlowCPU_Int3& rhs) const { return NvFlowCPU_Uint3(x << rhs.x, y << rhs.y, z << rhs.z); }
467};
468
469NV_FLOW_INLINE NvFlowCPU_Uint3 operator>>(const NvFlowCPU_Uint& lhs, const NvFlowCPU_Uint3& rhs) { return NvFlowCPU_Uint3(lhs >> rhs.x, lhs >> rhs.y, lhs >> rhs.z); }
470NV_FLOW_INLINE NvFlowCPU_Uint3 operator>>(const NvFlowCPU_Uint3& lhs, const NvFlowCPU_Uint& rhs) { return NvFlowCPU_Uint3(lhs.x >> rhs, lhs.y >> rhs, lhs.z >> rhs); }
471NV_FLOW_INLINE NvFlowCPU_Uint3 operator<<(const NvFlowCPU_Uint& lhs, const NvFlowCPU_Uint3& rhs) { return NvFlowCPU_Uint3(lhs << rhs.x, lhs << rhs.y, lhs << rhs.z); }
472NV_FLOW_INLINE NvFlowCPU_Uint3 operator<<(const NvFlowCPU_Uint3& lhs, const NvFlowCPU_Uint& rhs) { return NvFlowCPU_Uint3(lhs.x << rhs, lhs.y << rhs, lhs.z << rhs); }
473
474NV_FLOW_INLINE NvFlowCPU_Uint3 operator+(const NvFlowCPU_Uint& lhs, const NvFlowCPU_Uint3& rhs) { return NvFlowCPU_Uint3(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z); }
475NV_FLOW_INLINE NvFlowCPU_Uint3 operator+(const NvFlowCPU_Uint3& lhs, const NvFlowCPU_Uint& rhs) { return NvFlowCPU_Uint3(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs); }
476NV_FLOW_INLINE NvFlowCPU_Uint3 operator-(const NvFlowCPU_Uint& lhs, const NvFlowCPU_Uint3& rhs) { return NvFlowCPU_Uint3(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z); }
477NV_FLOW_INLINE NvFlowCPU_Uint3 operator-(const NvFlowCPU_Uint3& lhs, const NvFlowCPU_Uint& rhs) { return NvFlowCPU_Uint3(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs); }
478
480{
481 NvFlowUint x, y, z, w;
482
483 NvFlowCPU_Uint4() {}
484 NvFlowCPU_Uint4(NvFlowUint x, NvFlowUint y, NvFlowUint z, NvFlowUint w) : x(x), y(y), z(z), w(w) {}
485
486 NvFlowCPU_Uint4 operator+(const NvFlowCPU_Uint4& rhs) const { return NvFlowCPU_Uint4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w); }
487 NvFlowCPU_Uint4 operator-(const NvFlowCPU_Uint4& rhs) const { return NvFlowCPU_Uint4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w); }
488 NvFlowCPU_Uint4 operator*(const NvFlowCPU_Uint4& rhs) const { return NvFlowCPU_Uint4(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w); }
489 NvFlowCPU_Uint4 operator/(const NvFlowCPU_Uint4& rhs) const { return NvFlowCPU_Uint4(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w); }
490
491 NvFlowCPU_Uint4& operator+=(const NvFlowCPU_Uint4& rhs) { x += rhs.x; y += rhs.y; z += rhs.z; w += rhs.w; return *this; }
492 NvFlowCPU_Uint4& operator-=(const NvFlowCPU_Uint4& rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; w -= rhs.w; return *this; }
493 NvFlowCPU_Uint4& operator*=(const NvFlowCPU_Uint4& rhs) { x *= rhs.x; y *= rhs.y; z *= rhs.z; w *= rhs.w; return *this; }
494 NvFlowCPU_Uint4& operator/=(const NvFlowCPU_Uint4& rhs) { x /= rhs.x; y /= rhs.y; z /= rhs.z; w /= rhs.w; return *this; }
495};
496
497NV_FLOW_INLINE NvFlowCPU_Int3 NvFlowCPU_Int3::operator>>(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Int3(x >> rhs.x, y >> rhs.y, z >> rhs.z); }
498NV_FLOW_INLINE NvFlowCPU_Int3 NvFlowCPU_Int3::operator<<(const NvFlowCPU_Uint3& rhs) const { return NvFlowCPU_Int3(x << rhs.x, y << rhs.y, z << rhs.z); }
499
500NV_FLOW_INLINE NvFlowCPU_Float3::NvFlowCPU_Float3(const NvFlowCPU_Int3& v) : x(float(v.x)), y(float(v.y)), z(float(v.z)) {}
501
502NV_FLOW_INLINE NvFlowCPU_Int3::NvFlowCPU_Int3(const NvFlowCPU_Uint3& v) : x(int(v.x)), y(int(v.y)), z(int(v.z)) {}
503NV_FLOW_INLINE NvFlowCPU_Int3::NvFlowCPU_Int3(const NvFlowCPU_Float3& v) : x(int(v.x)), y(int(v.y)), z(int(v.z)) {}
504
505NV_FLOW_INLINE NvFlowCPU_Uint3::NvFlowCPU_Uint3(const NvFlowCPU_Int3& v) : x(int(v.x)), y(int(v.y)), z(int(v.z)) {}
506
507NV_FLOW_INLINE NvFlowCPU_Int4::NvFlowCPU_Int4(const NvFlowCPU_Uint4& rhs) : x(int(rhs.x)), y(int(rhs.y)), z(int(rhs.z)), w(int(rhs.w)) {}
508
509NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_asfloat(NvFlowCPU_Uint4 v) {return *((NvFlowCPU_Float4*)&v);}
510NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_asfloat(NvFlowCPU_Uint3 v) {return *((NvFlowCPU_Float3*)&v);}
511NV_FLOW_INLINE NvFlowCPU_Float2 NvFlowCPU_asfloat(NvFlowCPU_Uint2 v) {return *((NvFlowCPU_Float2*)&v);}
512NV_FLOW_INLINE float NvFlowCPU_asfloat(NvFlowUint v) {return *((float*)&v);}
513
514NV_FLOW_INLINE NvFlowCPU_Float4 NvFlowCPU_asfloat(NvFlowCPU_Int4 v) {return *((NvFlowCPU_Float4*)&v);}
515NV_FLOW_INLINE NvFlowCPU_Float3 NvFlowCPU_asfloat(NvFlowCPU_Int3 v) {return *((NvFlowCPU_Float3*)&v);}
516NV_FLOW_INLINE NvFlowCPU_Float2 NvFlowCPU_asfloat(NvFlowCPU_Int2 v) {return *((NvFlowCPU_Float2*)&v);}
517NV_FLOW_INLINE float NvFlowCPU_asfloat(int v) {return *((float*)&v);}
518
519NV_FLOW_INLINE NvFlowCPU_Uint4 NvFlowCPU_asuint(NvFlowCPU_Float4 v) {return *((NvFlowCPU_Uint4*)&v);}
520NV_FLOW_INLINE NvFlowCPU_Uint3 NvFlowCPU_asuint(NvFlowCPU_Float3 v) {return *((NvFlowCPU_Uint3*)&v);}
521NV_FLOW_INLINE NvFlowCPU_Uint2 NvFlowCPU_asuint(NvFlowCPU_Float2 v) {return *((NvFlowCPU_Uint2*)&v);}
522NV_FLOW_INLINE NvFlowUint NvFlowCPU_asuint(float v) {return *((NvFlowUint*)&v);}
523
524NV_FLOW_INLINE NvFlowCPU_Int4 NvFlowCPU_asint(NvFlowCPU_Float4 v) {return *((NvFlowCPU_Int4*)&v);}
525NV_FLOW_INLINE NvFlowCPU_Int3 NvFlowCPU_asint(NvFlowCPU_Float3 v) {return *((NvFlowCPU_Int3*)&v);}
526NV_FLOW_INLINE NvFlowCPU_Int2 NvFlowCPU_asint(NvFlowCPU_Float2 v) {return *((NvFlowCPU_Int2*)&v);}
527NV_FLOW_INLINE int NvFlowCPU_asint(float v) {return *((int*)&v);}
528
530{
531 void* data;
532 NvFlowUint64 sizeInBytes;
533 NvFlowUint elementSizeInBytes;
534 NvFlowUint elementCount;
535 NvFlowFormat format;
536 NvFlowUint width;
537 NvFlowUint height;
538 NvFlowUint depth;
539 NvFlowSamplerDesc samplerDesc;
540};
541
542template <typename T>
544{
545 const T* data;
546
547 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
548 {
549 data = (const T*)resource->data;
550 }
551};
552
553template <typename T>
555{
556 const T* data;
557 NvFlowUint count;
558
559 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
560 {
561 data = (const T*)resource->data;
562 count = resource->elementCount;
563 }
564
565 const T& operator[](int index) {
566 if (index < 0 || index >= int(count)) index = 0;
567 return data[index];
568 }
569};
570
571template <typename T>
573{
574 T* data;
575 NvFlowUint count;
576
577 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
578 {
579 data = (T*)resource->data;
580 count = resource->elementCount;
581 }
582
583 T& operator[](int index) {
584 if (index < 0 || index >= int(count)) index = 0;
585 return data[index];
586 }
587};
588
590{
592
593 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
594 {
595 desc = resource->samplerDesc;
596 }
597};
598
599template <typename T>
601{
602 const T* data;
603 NvFlowFormat format;
604 NvFlowUint width;
605 T out_of_bounds = {};
606
607 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
608 {
609 data = (const T*)resource->data;
610 format = resource->format;
611 width = resource->width;
612 memset(&out_of_bounds, 0, sizeof(out_of_bounds));
613 }
614};
615
616template <typename T>
617NV_FLOW_FORCE_INLINE const T NvFlowCPU_textureRead(NvFlowCPU_Texture1D<T>& tex, int index)
618{
619 if (index < 0 || index >= int(tex.width))
620 {
621 return tex.out_of_bounds;
622 }
623 return tex.data[index];
624}
625
626template <typename T>
627NV_FLOW_FORCE_INLINE T NvFlowCPU_textureSampleLevel(NvFlowCPU_Texture1D<T>& tex, NvFlowCPU_SamplerState state, const float pos, float lod)
628{
629 float posf(float(tex.width) * pos);
630
631 // clamp sampler
632 if (posf < 0.5f) posf = 0.5f;
633 if (posf > float(tex.width) - 0.5f) posf = float(tex.width) - 0.5f;
634
635 int pos0 = int(NvFlowCPU_floor(posf - 0.5f));
636 float f = posf - 0.5f - float(pos0);
637 float of = 1.f - f;
638
639 T sum = of * NvFlowCPU_textureRead(tex, pos0 + 0);
640 sum += f * NvFlowCPU_textureRead(tex, pos0 + 1);
641
642 return sum;
643}
644
645template <typename T>
647{
648 T* data;
649 NvFlowFormat format;
650 NvFlowUint width;
651
652 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
653 {
654 data = (T*)resource->data;
655 format = resource->format;
656 width = resource->width;
657 }
658};
659
660template <typename T>
661NV_FLOW_FORCE_INLINE const T NvFlowCPU_textureRead(NvFlowCPU_RWTexture1D<T>& tex, int index)
662{
663 return tex.data[index];
664}
665
666template <typename T>
667NV_FLOW_FORCE_INLINE void NvFlowCPU_textureWrite(NvFlowCPU_RWTexture1D<T>& tex, int index, const T value)
668{
669 tex.data[index] = value;
670}
671
672template <typename T>
673NV_FLOW_FORCE_INLINE void NvFlowCPU_textureWrite(bool pred, NvFlowCPU_RWTexture1D<T>& tex, int index, const T value)
674{
675 if (pred)
676 {
677 NvFlowCPU_textureWrite(tex, index, value);
678 }
679}
680
681template <typename T>
683{
684 const T* data;
685 NvFlowFormat format;
686 NvFlowUint width;
687 NvFlowUint height;
688 T out_of_bounds;
689
690 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
691 {
692 data = (const T*)resource->data;
693 format = resource->format;
694 width = resource->width;
695 height = resource->height;
696 memset(&out_of_bounds, 0, sizeof(out_of_bounds));
697 }
698};
699
700template <typename T>
701NV_FLOW_FORCE_INLINE const T NvFlowCPU_textureRead(NvFlowCPU_Texture2D<T>& tex, NvFlowCPU_Int2 index)
702{
703 if (index.x < 0 || index.x >= int(tex.width) ||
704 index.y < 0 || index.y >= int(tex.height))
705 {
706 return tex.out_of_bounds;
707 }
708 return tex.data[index.y * tex.width + index.x];
709}
710
711template <typename T>
712NV_FLOW_FORCE_INLINE T NvFlowCPU_textureSampleLevel(NvFlowCPU_Texture2D<T>& tex, NvFlowCPU_SamplerState state, const NvFlowCPU_Float2 pos, float lod)
713{
714 NvFlowCPU_Float2 posf(NvFlowCPU_Float2(float(tex.width), float(tex.height)) * pos);
715
716 // clamp sampler
717 if (posf.x < 0.5f) posf.x = 0.5f;
718 if (posf.x > float(tex.width) - 0.5f) posf.x = float(tex.width) - 0.5f;
719 if (posf.y < 0.5f) posf.y = 0.5f;
720 if (posf.y > float(tex.height) - 0.5f) posf.y = float(tex.height) - 0.5f;
721
722 NvFlowCPU_Int2 pos00 = NvFlowCPU_Int2(NvFlowCPU_floor(posf - NvFlowCPU_Float2(0.5f, 0.5f)));
723 NvFlowCPU_Float2 f = posf - NvFlowCPU_Float2(0.5f, 0.5f) - NvFlowCPU_Float2(pos00);
724 NvFlowCPU_Float2 of = NvFlowCPU_Float2(1.f, 1.f) - f;
725
726 T sum = of.x * of.y * NvFlowCPU_textureRead(tex, pos00 + NvFlowCPU_Int2(0, 0));
727 sum += f.x * of.y * NvFlowCPU_textureRead(tex, pos00 + NvFlowCPU_Int2(1, 0));
728 sum += of.x * f.y * NvFlowCPU_textureRead(tex, pos00 + NvFlowCPU_Int2(0, 1));
729 sum += f.x * f.y * NvFlowCPU_textureRead(tex, pos00 + NvFlowCPU_Int2(1, 1));
730
731 return sum;
732}
733
734template <typename T>
736{
737 T* data;
738 NvFlowFormat format;
739 NvFlowUint width;
740 NvFlowUint height;
741
742 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
743 {
744 data = (T*)resource->data;
745 format = resource->format;
746 width = resource->width;
747 height = resource->height;
748 }
749};
750
751template <typename T>
752NV_FLOW_FORCE_INLINE const T NvFlowCPU_textureRead(NvFlowCPU_RWTexture2D<T>& tex, NvFlowCPU_Int2 index)
753{
754 return tex.data[index.y * tex.width + index.x];
755}
756
757template <typename T>
758NV_FLOW_FORCE_INLINE void NvFlowCPU_textureWrite(NvFlowCPU_RWTexture2D<T>& tex, NvFlowCPU_Int2 index, const T value)
759{
760 tex.data[index.y * tex.width + index.x] = value;
761}
762
763template <typename T>
764NV_FLOW_FORCE_INLINE void NvFlowCPU_textureWrite(bool pred, NvFlowCPU_RWTexture2D<T>& tex, NvFlowCPU_Int2 index, const T value)
765{
766 if (pred)
767 {
768 NvFlowCPU_textureWrite(tex, index, value);
769 }
770}
771
772template <typename T>
774{
775 const T* data;
776 NvFlowFormat format;
777 NvFlowUint width;
778 NvFlowUint height;
779 NvFlowUint depth;
780 T out_of_bounds;
781
782 NvFlowUint wh;
783 NvFlowUint whd;
784
785 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
786 {
787 data = (const T*)resource->data;
788 format = resource->format;
789 width = resource->width;
790 height = resource->height;
791 depth = resource->depth;
792 memset(&out_of_bounds, 0, sizeof(out_of_bounds));
793
794 wh = width * height;
795 whd = wh * depth;
796 }
797};
798
799template <typename T>
800NV_FLOW_FORCE_INLINE const T NvFlowCPU_textureRead(NvFlowCPU_Texture3D<T>& tex, NvFlowCPU_Int3 index)
801{
802 if (index.x < 0 || index.x >= int(tex.width) ||
803 index.y < 0 || index.y >= int(tex.height) ||
804 index.z < 0 || index.z >= int(tex.depth))
805 {
806 return tex.out_of_bounds;
807 }
808 return tex.data[(index.z * tex.height + index.y) * tex.width + index.x];
809}
810
811template <typename T>
812NV_FLOW_FORCE_INLINE T NvFlowCPU_textureSampleLevel(NvFlowCPU_Texture3D<T>& tex, NvFlowCPU_SamplerState state, const NvFlowCPU_Float3 pos, float lod)
813{
814 NvFlowCPU_Float3 posf(NvFlowCPU_Float3(float(tex.width), float(tex.height), float(tex.depth)) * pos);
815
816 // clamp sampler
817 if (posf.x < 0.5f) posf.x = 0.5f;
818 if (posf.x > float(tex.width) - 0.5f) posf.x = float(tex.width) - 0.5f;
819 if (posf.y < 0.5f) posf.y = 0.5f;
820 if (posf.y > float(tex.height) - 0.5f) posf.y = float(tex.height) - 0.5f;
821 if (posf.z < 0.5f) posf.z = 0.5f;
822 if (posf.z > float(tex.depth) - 0.5f) posf.z = float(tex.depth) - 0.5f;
823
824 NvFlowCPU_Int4 pos000 = NvFlowCPU_Int4(NvFlowCPU_floor(posf - NvFlowCPU_Float3(0.5f, 0.5f, 0.5f)), 0);
825 NvFlowCPU_Float3 f = posf - NvFlowCPU_Float3(0.5f, 0.5f, 0.5f) - NvFlowCPU_Float3(float(pos000.x), float(pos000.y), float(pos000.z));
826 NvFlowCPU_Float3 of = NvFlowCPU_Float3(1.f, 1.f, 1.f) - f;
827
829 of.x * of.y * of.z,
830 f.x * of.y * of.z,
831 of.x * f.y * of.z,
832 f.x * f.y * of.z
833 );
835 of.x * of.y * f.z,
836 f.x * of.y * f.z,
837 of.x * f.y * f.z,
838 f.x * f.y * f.z
839 );
840
841 T sum;
842 if (pos000.x >= 0 && pos000.y >= 0 && pos000.z >= 0 &&
843 pos000.x <= int(tex.width - 2) && pos000.y <= int(tex.height - 2) && pos000.z <= int(tex.depth - 2))
844 {
845 NvFlowUint idx000 = pos000.z * tex.wh + pos000.y * tex.width + pos000.x;
846 sum = wl.x * tex.data[idx000];
847 sum += wl.y * tex.data[idx000 + 1u];
848 sum += wl.z * tex.data[idx000 + tex.width];
849 sum += wl.w * tex.data[idx000 + 1u + tex.width];
850 sum += wh.x * tex.data[idx000 + tex.wh];
851 sum += wh.y * tex.data[idx000 + 1u + tex.wh];
852 sum += wh.z * tex.data[idx000 + tex.width + tex.wh];
853 sum += wh.w * tex.data[idx000 + 1u + tex.width + tex.wh];
854 }
855 else
856 {
857 sum = wl.x * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(0, 0, 0));
858 sum += wl.y * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(1, 0, 0));
859 sum += wl.z * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(0, 1, 0));
860 sum += wl.w * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(1, 1, 0));
861 sum += wh.x * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(0, 0, 1));
862 sum += wh.y * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(1, 0, 1));
863 sum += wh.z * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(0, 1, 1));
864 sum += wh.w * NvFlowCPU_textureRead(tex, pos000.rgb() + NvFlowCPU_Int3(1, 1, 1));
865 }
866 return sum;
867}
868
869template <typename T>
871{
872 T* data;
873 NvFlowFormat format;
874 NvFlowUint width;
875 NvFlowUint height;
876 NvFlowUint depth;
877
878 NV_FLOW_INLINE void bind(NvFlowCPU_Resource* resource)
879 {
880 data = (T*)resource->data;
881 format = resource->format;
882 width = resource->width;
883 height = resource->height;
884 depth = resource->depth;
885 }
886};
887
888template <typename T>
889NV_FLOW_FORCE_INLINE const T NvFlowCPU_textureRead(NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index)
890{
891 return tex.data[(index.z * tex.height + index.y) * tex.width + index.x];
892}
893
894template <typename T>
895NV_FLOW_FORCE_INLINE void NvFlowCPU_textureWrite(NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index, const T value)
896{
897 tex.data[(index.z * tex.height + index.y) * tex.width + index.x] = value;
898}
899
900template <typename T>
901NV_FLOW_FORCE_INLINE void NvFlowCPU_textureWrite(bool pred, NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index, const T value)
902{
903 if (pred)
904 {
905 NvFlowCPU_textureWrite(tex, index, value);
906 }
907}
908
909template <typename T>
910NV_FLOW_FORCE_INLINE void NvFlowCPU_InterlockedAdd(NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index, T value)
911{
912 ((std::atomic<T>*)&tex.data[(index.z * tex.height + index.y) * tex.width + index.x])->fetch_add(value);
913}
914
915template <typename T>
916NV_FLOW_FORCE_INLINE void NvFlowCPU_InterlockedMin(NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index, T value)
917{
918 ((std::atomic<T>*)&tex.data[(index.z * tex.height + index.y) * tex.width + index.x])->fetch_min(value);
919}
920
921template <typename T>
922NV_FLOW_FORCE_INLINE void NvFlowCPU_InterlockedOr(NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index, T value)
923{
924 ((std::atomic<T>*)&tex.data[(index.z * tex.height + index.y) * tex.width + index.x])->fetch_or(value);
925}
926
927template <typename T>
928NV_FLOW_FORCE_INLINE void NvFlowCPU_InterlockedAnd(NvFlowCPU_RWTexture3D<T>& tex, NvFlowCPU_Int3 index, T value)
929{
930 ((std::atomic<T>*)&tex.data[(index.z * tex.height + index.y) * tex.width + index.x])->fetch_and(value);
931}
932
933template <class T>
935{
936 T data;
937};
938
939template <class T>
940NV_FLOW_FORCE_INLINE void NvFlowCPU_swrite(int _groupshared_pass, int _groupshared_sync_count, NvFlowCPU_Groupshared<T>& g, const T& value)
941{
942 if (_groupshared_pass == _groupshared_sync_count)
943 {
944 g.data = value;
945 }
946}
947
948template <class T>
949NV_FLOW_FORCE_INLINE T NvFlowCPU_sread(NvFlowCPU_Groupshared<T>& g)
950{
951 return g.data;
952}
953
954template <class T, unsigned int arraySize>
956{
957 T data[arraySize];
958};
959
960template <class T, unsigned int arraySize>
961NV_FLOW_FORCE_INLINE void NvFlowCPU_swrite(int _groupshared_pass, int _groupshared_sync_count, NvFlowCPU_GroupsharedArray<T, arraySize>& g, int index, const T& value)
962{
963 if (_groupshared_pass == _groupshared_sync_count)
964 {
965 g.data[index] = value;
966 }
967}
968
969template <class T, unsigned int arraySize>
970NV_FLOW_FORCE_INLINE T NvFlowCPU_sread(NvFlowCPU_GroupsharedArray<T, arraySize>& g, int index)
971{
972 return g.data[index];
973}
974
975#endif
Definition NvFlowResourceCPU.h:544
Definition NvFlowResourceCPU.h:130
Definition NvFlowResourceCPU.h:173
Definition NvFlowResourceCPU.h:247
Definition NvFlowResourceCPU.h:315
Definition NvFlowResourceCPU.h:956
Definition NvFlowResourceCPU.h:935
Definition NvFlowResourceCPU.h:333
Definition NvFlowResourceCPU.h:365
Definition NvFlowResourceCPU.h:411
Definition NvFlowResourceCPU.h:573
Definition NvFlowResourceCPU.h:647
Definition NvFlowResourceCPU.h:736
Definition NvFlowResourceCPU.h:871
Definition NvFlowResourceCPU.h:530
Definition NvFlowResourceCPU.h:590
Definition NvFlowResourceCPU.h:555
Definition NvFlowResourceCPU.h:601
Definition NvFlowResourceCPU.h:683
Definition NvFlowResourceCPU.h:774
Definition NvFlowResourceCPU.h:434
Definition NvFlowResourceCPU.h:445
Definition NvFlowResourceCPU.h:480
Definition NvFlowContext.h:169
Definition color.h:192