Skip to content

Commit d73a310

Browse files
committed
Update optix code to use closure pool.
Signed-off-by: Curtis Black <curtis.w.black@gmail.com>
1 parent c0f8462 commit d73a310

3 files changed

Lines changed: 176 additions & 328 deletions

File tree

src/testrender/cuda/rend_lib.cu

Lines changed: 85 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ extern __device__ CUdeviceptr xform_buffer;
3030
} // namespace pvt
3131
OSL_NAMESPACE_END
3232

33-
34-
// Taken from the SimplePool class
35-
__device__ static inline size_t
36-
alignment_offset_calc(void* ptr, size_t alignment)
37-
{
38-
uintptr_t ptrbits = reinterpret_cast<uintptr_t>(ptr);
39-
uintptr_t offset = ((ptrbits + alignment - 1) & -alignment) - ptrbits;
40-
return offset;
41-
}
42-
43-
4433
// These functions are declared extern to prevent name mangling.
4534
extern "C" {
4635

@@ -50,200 +39,120 @@ __direct_callable__dummy_rend_lib()
5039
{
5140
}
5241

53-
54-
__device__ void*
55-
closure_component_allot(void* pool, int id, size_t prim_size,
56-
const OSL::Color3& w)
57-
{
58-
((OSL::ClosureComponent*)pool)->id = id;
59-
((OSL::ClosureComponent*)pool)->w = w;
60-
61-
size_t needed = (sizeof(OSL::ClosureComponent) + prim_size
62-
+ (alignof(OSL::ClosureComponent) - 1))
63-
& ~(alignof(OSL::ClosureComponent) - 1);
64-
char* char_ptr = (char*)pool;
65-
66-
return (void*)&char_ptr[needed];
67-
}
68-
69-
70-
__device__ void*
71-
closure_mul_allot(void* pool, const OSL::Color3& w, OSL::ClosureColor* c)
72-
{
73-
((OSL::ClosureMul*)pool)->id = OSL::ClosureColor::MUL;
74-
((OSL::ClosureMul*)pool)->weight = w;
75-
((OSL::ClosureMul*)pool)->closure = c;
76-
77-
size_t needed = (sizeof(OSL::ClosureMul)
78-
+ (alignof(OSL::ClosureComponent) - 1))
79-
& ~(alignof(OSL::ClosureComponent) - 1);
80-
char* char_ptr = (char*)pool;
81-
82-
return &char_ptr[needed];
83-
}
84-
85-
86-
__device__ void*
87-
closure_mul_float_allot(void* pool, const float& w, OSL::ClosureColor* c)
88-
{
89-
((OSL::ClosureMul*)pool)->id = OSL::ClosureColor::MUL;
90-
((OSL::ClosureMul*)pool)->weight.x = w;
91-
((OSL::ClosureMul*)pool)->weight.y = w;
92-
((OSL::ClosureMul*)pool)->weight.z = w;
93-
((OSL::ClosureMul*)pool)->closure = c;
94-
95-
size_t needed = (sizeof(OSL::ClosureMul)
96-
+ (alignof(OSL::ClosureComponent) - 1))
97-
& ~(alignof(OSL::ClosureComponent) - 1);
98-
char* char_ptr = (char*)pool;
99-
100-
return &char_ptr[needed];
101-
}
102-
103-
10442
__device__ void*
105-
closure_add_allot(void* pool, OSL::ClosureColor* a, OSL::ClosureColor* b)
43+
osl_add_closure_closure(void* sg_, const void* a_, const void* b_)
10644
{
107-
((OSL::ClosureAdd*)pool)->id = OSL::ClosureColor::ADD;
108-
((OSL::ClosureAdd*)pool)->closureA = a;
109-
((OSL::ClosureAdd*)pool)->closureB = b;
110-
111-
size_t needed = (sizeof(OSL::ClosureAdd)
112-
+ (alignof(OSL::ClosureComponent) - 1))
113-
& ~(alignof(OSL::ClosureComponent) - 1);
114-
char* char_ptr = (char*)pool;
115-
116-
return &char_ptr[needed];
45+
a_ = __builtin_assume_aligned(a_, alignof(float));
46+
b_ = __builtin_assume_aligned(b_, alignof(float));
47+
ShaderGlobals* sg = (ShaderGlobals*)sg_;
48+
const OSL::ClosureColor* a = (const OSL::ClosureColor*)a_;
49+
const OSL::ClosureColor* b = (const OSL::ClosureColor*)b_;
50+
if (a == NULL)
51+
return b;
52+
if (b == NULL)
53+
return a;
54+
auto* closure_pool = ((RenderState*)sg->renderstate)->closure_pool;
55+
OSL::ClosureAdd* add
56+
= (OSL::ClosureAdd*)closure_pool->allocate(sizeof(OSL::ClosureAdd),
57+
alignof(OSL::ClosureAdd));
58+
if (add) {
59+
add->id = OSL::ClosureColor::ADD;
60+
add->closureA = a;
61+
add->closureB = b;
62+
}
63+
return add;
11764
}
11865

119-
12066
__device__ void*
121-
osl_allocate_closure_component(void* sg_, int id, int size)
67+
osl_mul_closure_color(void* sg_, const void* a_, const void* w_)
12268
{
123-
OSL_CUDA::ShaderGlobals* sg_ptr = (OSL_CUDA::ShaderGlobals*)sg_;
124-
125-
OSL::Color3 w = OSL::Color3(1, 1, 1);
126-
// Fix up the alignment
127-
void* ret = ((char*)sg_ptr->renderstate)
128-
+ alignment_offset_calc(sg_ptr->renderstate,
129-
alignof(OSL::ClosureComponent));
130-
131-
size = max(4, size);
132-
133-
sg_ptr->renderstate = closure_component_allot(ret, id, size, w);
69+
a_ = __builtin_assume_aligned(a_, alignof(float));
70+
w_ = __builtin_assume_aligned(w_, alignof(float));
13471

135-
return ret;
136-
}
137-
138-
139-
140-
__device__ void*
141-
osl_allocate_weighted_closure_component(void* sg_, int id, int size,
142-
const void* w)
143-
{
144-
OSL_CUDA::ShaderGlobals* sg_ptr = (OSL_CUDA::ShaderGlobals*)sg_;
145-
146-
const OSL::Color3* wc
147-
= (const OSL::Color3*)__builtin_assume_aligned(w, alignof(float));
148-
149-
if (wc->x == 0.0f && wc->y == 0.0f && wc->z == 0.0f) {
72+
ShaderGlobals* sg = (ShaderGlobals*)sg_;
73+
const OSL::ClosureColor* a = (const OSL::ClosureColor*)a_;
74+
const OSL::Color3* w = (const OSL::Color3*)w_;
75+
if (a == NULL)
76+
return NULL;
77+
if (w->x == 0.0f && w->y == 0.0f && w->z == 0.0f)
15078
return NULL;
79+
if (w->x == 1.0f && w->y == 1.0f && w->z == 1.0f)
80+
return a;
81+
auto* closure_pool = ((RenderState*)sg->renderstate)->closure_pool;
82+
OSL::ClosureMul* mul
83+
= (OSL::ClosureMul*)closure_pool->allocate(sizeof(OSL::ClosureMul),
84+
alignof(OSL::ClosureMul));
85+
if (mul) {
86+
mul->id = OSL::ClosureColor::MUL;
87+
mul->weight = *w;
88+
mul->closure = a;
15189
}
152-
153-
size = max(4, size);
154-
155-
// Fix up the alignment
156-
void* ret = ((char*)sg_ptr->renderstate)
157-
+ alignment_offset_calc(sg_ptr->renderstate,
158-
alignof(OSL::ClosureComponent));
159-
sg_ptr->renderstate = closure_component_allot(ret, id, size, *wc);
160-
161-
return ret;
90+
return mul;
16291
}
16392

164-
165-
16693
__device__ void*
167-
osl_mul_closure_color(void* sg_, void* a, const void* w)
94+
osl_mul_closure_float(void* sg_, const void* a_, float w)
16895
{
169-
OSL_CUDA::ShaderGlobals* sg_ptr = (OSL_CUDA::ShaderGlobals*)sg_;
170-
const OSL::Color3* wc
171-
= (const OSL::Color3*)__builtin_assume_aligned(w, alignof(float));
96+
a_ = __builtin_assume_aligned(a_, alignof(float));
17297

173-
if (a == NULL) {
98+
ShaderGlobals* sg = (ShaderGlobals*)sg_;
99+
const OSL::ClosureColor* a = (const OSL::ClosureColor*)a_;
100+
if (a == NULL)
174101
return NULL;
175-
}
176-
177-
if (wc->x == 0.0f && wc->y == 0.0f && wc->z == 0.0f) {
102+
if (w == 0.0f)
178103
return NULL;
179-
}
180-
181-
if (wc->x == 1.0f && wc->y == 1.0f && wc->z == 1.0f) {
104+
if (w == 1.0f)
182105
return a;
106+
auto* closure_pool = ((RenderState*)sg->renderstate)->closure_pool;
107+
OSL::ClosureMul* mul
108+
= (OSL::ClosureMul*)closure_pool->allocate(sizeof(OSL::ClosureMul),
109+
alignof(OSL::ClosureMul));
110+
if (mul) {
111+
mul->id = OSL::ClosureColor::MUL;
112+
mul->weight = OSL::Color3(w);
113+
mul->closure = a;
183114
}
184-
185-
// Fix up the alignment
186-
void* ret = ((char*)sg_ptr->renderstate)
187-
+ alignment_offset_calc(sg_ptr->renderstate,
188-
alignof(OSL::ClosureComponent));
189-
sg_ptr->renderstate = closure_mul_allot(ret, *wc, (OSL::ClosureColor*)a);
190-
191-
return ret;
115+
return mul;
192116
}
193117

194-
195-
196118
__device__ void*
197-
osl_mul_closure_float(void* sg_, void* a, float w)
119+
osl_allocate_closure_component(void* sg_, int id, int size)
198120
{
199-
a = __builtin_assume_aligned(a, alignof(float));
200-
201-
OSL_CUDA::ShaderGlobals* sg_ptr = (OSL_CUDA::ShaderGlobals*)sg_;
202-
203-
if (a == NULL || w == 0.0f) {
204-
return NULL;
205-
}
206-
207-
if (w == 1.0f) {
208-
return a;
121+
ShaderGlobals* sg = (ShaderGlobals*)sg_;
122+
auto* closure_pool = ((RenderState*)sg->renderstate)->closure_pool;
123+
// Allocate the component and the mul back to back
124+
const size_t needed = sizeof(OSL::ClosureComponent) + size;
125+
OSL::ClosureComponent* comp
126+
= (OSL::ClosureComponent*)
127+
closure_pool->allocate(needed, alignof(OSL::ClosureComponent));
128+
if (comp) {
129+
comp->id = id;
130+
comp->w = OSL::Color3(1.0f);
209131
}
210-
211-
// Fix up the alignment
212-
void* ret = ((char*)sg_ptr->renderstate)
213-
+ alignment_offset_calc(sg_ptr->renderstate,
214-
alignof(OSL::ClosureComponent));
215-
sg_ptr->renderstate = closure_mul_float_allot(ret, w,
216-
(OSL::ClosureColor*)a);
217-
218-
return ret;
132+
return comp;
219133
}
220134

221-
222-
223135
__device__ void*
224-
osl_add_closure_closure(void* sg_, void* a, void* b)
136+
osl_allocate_weighted_closure_component(void* sg_, int id, int size,
137+
const void* w_)
225138
{
226-
a = __builtin_assume_aligned(a, alignof(float));
227-
b = __builtin_assume_aligned(b, alignof(float));
228-
229-
OSL_CUDA::ShaderGlobals* sg_ptr = (OSL_CUDA::ShaderGlobals*)sg_;
230-
231-
if (a == NULL) {
232-
return b;
233-
}
139+
w_ = __builtin_assume_aligned(w_, alignof(float));
234140

235-
if (b == NULL) {
236-
return a;
141+
ShaderGlobals* sg = (ShaderGlobals*)sg_;
142+
const OSL::Color3* w = (const OSL::Color3*)w_;
143+
if (w->x == 0.0f && w->y == 0.0f && w->z == 0.0f)
144+
return NULL;
145+
auto* closure_pool = ((RenderState*)sg->renderstate)->closure_pool;
146+
// Allocate the component and the mul back to back
147+
const size_t needed = sizeof(OSL::ClosureComponent) + size;
148+
OSL::ClosureComponent* comp
149+
= (OSL::ClosureComponent*)
150+
closure_pool->allocate(needed, alignof(OSL::ClosureComponent));
151+
if (comp) {
152+
comp->id = id;
153+
comp->w = *w;
237154
}
238-
239-
// Fix up the alignment
240-
void* ret = ((char*)sg_ptr->renderstate)
241-
+ alignment_offset_calc(sg_ptr->renderstate,
242-
alignof(OSL::ClosureComponent));
243-
sg_ptr->renderstate = closure_add_allot(ret, (OSL::ClosureColor*)a,
244-
(OSL::ClosureColor*)b);
245-
246-
return ret;
155+
return comp;
247156
}
248157

249158

src/testshade/render_state.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class StackClosurePool {
3333

3434
void reset()
3535
{
36-
ptr = &buffer[0];
37-
*(int*)ptr = 0;
36+
ptr = &buffer[0];
37+
*(int*)ptr = 0;
3838
}
3939

4040
void* allocate(size_t size, size_t alignment)
@@ -43,7 +43,7 @@ class StackClosurePool {
4343
alignment);
4444
ptr = (void*)(p + size);
4545
if (ptr <= &buffer[256])
46-
return p;
46+
return (void*)p;
4747
return nullptr;
4848
}
4949
}

0 commit comments

Comments
 (0)