Skip to content

Commit

Permalink
lib+blender: Hook up interactive status, clean up debug prints
Browse files Browse the repository at this point in the history
I decided to revert some of the earlier changes, I'll figure out the
resolution adjustment later and keep it like this for now for better
responsiveness.
The status string now indicates sample progress, and an error with
CrayDrawData was fixed.
  • Loading branch information
vkoskiv committed Jan 2, 2024
1 parent 95fc56d commit 1aa2020
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
60 changes: 25 additions & 35 deletions bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,16 @@ def on_status_update(renderer_cb_info, args):
print("Stopping c-ray")
cr_renderer.stop()


def status_update_interactive(renderer_cb_info, args):
tag_redraw, tag_update, cr_renderer = args
cr_renderer.pass_ready = True
tag_redraw, update_stats, self = args
ct.pythonapi.PyCapsule_GetPointer.restype = ct.c_void_p
ct.pythonapi.PyCapsule_GetPointer.argtypes = [ct.py_object, ct.c_char_p]
ptr = ct.pythonapi.PyCapsule_GetPointer(renderer_cb_info, "cray.renderer_cb_info".encode())
info = ct.cast(ptr, ct.POINTER(c_ray.cr_cb_info)).contents
if info.finished_passes == self.cr_renderer.prefs.samples:
update_stats("Rendering done", "")
else:
update_stats("Sample {}".format(info.finished_passes), "")
tag_redraw()

class CrayRender(bpy.types.RenderEngine):
Expand All @@ -182,7 +188,6 @@ class CrayRender(bpy.types.RenderEngine):
cr_interactive_running = False
cr_renderer = None
old_mtx = None
pass_ready = False

def __init__(self):
print("c-ray initialized")
Expand Down Expand Up @@ -340,34 +345,26 @@ def render(self, depsgraph):
self.display_bitmap(bm)

def view_draw(self, context, depsgraph):
print("view_draw_start")
mtx = context.region_data.view_matrix.inverted()
if not self.old_mtx or mtx != self.old_mtx:
print("self.old_mtx != mtx, tagging update")
self.tag_update()
self.old_mtx = mtx
if self.pass_ready == True:
dimensions = (context.region.width, context.region.height)
gpu.state.blend_set('ALPHA_PREMULT')
self.bind_display_space_shader(depsgraph.scene)
if not self.draw_data or self.draw_data.dimensions != dimensions:
print("Regenerating draw_data")
bm = self.cr_renderer.get_result()
if not bm:
print("No bitmap yet")
return
self.draw_data = CrayDrawData(dimensions, bm)

self.draw_data.draw()
self.unbind_display_space_shader()
gpu.state.blend_set('NONE')

self.pass_ready = False

print("view_draw_end")
dimensions = (context.region.width, context.region.height)
gpu.state.blend_set('ALPHA_PREMULT')
self.bind_display_space_shader(depsgraph.scene)
if not self.draw_data or self.draw_data.dimensions != dimensions:
print("Regenerating draw_data")
bm = self.cr_renderer.get_result()
if not bm:
print("No bitmap yet")
return
self.draw_data = CrayDrawData(dimensions, bm)

self.draw_data.draw()
self.unbind_display_space_shader()
gpu.state.blend_set('NONE')

def view_update(self, context, depsgraph):
print("view_update_start")
if not self.cr_renderer:
self.cr_renderer = c_ray.renderer()
self.cr_renderer.prefs.asset_path = ""
Expand All @@ -380,7 +377,6 @@ def view_update(self, context, depsgraph):
self.cr_renderer.prefs.tile_y = depsgraph.scene.c_ray.tile_size
self.cr_renderer.prefs.bounces = depsgraph.scene.c_ray.bounces
self.cr_renderer.prefs.node_list = depsgraph.scene.c_ray.node_list
print("depsgraph_len: {}".format(len(depsgraph.object_instances)))
self.cr_renderer.prefs.is_iterative = 1
cr_cam = self.cr_scene.cameras['Camera']
mtx = context.region_data.view_matrix.inverted()
Expand All @@ -398,14 +394,12 @@ def view_update(self, context, depsgraph):
cr_cam.set_param(c_ray.cam_param.blender_coord, 1)

if self.cr_interactive_running == True:
print("bg thread already running, sending restart")
self.cr_renderer.restart()
else:
print("Kicking off background renderer")
self.cr_renderer.callbacks.on_interactive_pass_finished = (status_update_interactive, (self.tag_redraw, self.tag_update, self))
self.cr_renderer.callbacks.on_interactive_pass_finished = (status_update_interactive, (self.tag_redraw, self.update_stats, self))
self.cr_renderer.start_interactive()
self.cr_interactive_running = True
print("view_update_end")

def display_bitmap(self, bm):
# Get float array from libc-ray
Expand Down Expand Up @@ -458,20 +452,16 @@ def __del__(self):
print("No self.pixels")

def draw(self):
start = time.time()
# FIXME: I have no idea how to create a GPUTexture that points to my raw float array on the C side.
# So for now, just generate a new texture from the data on every redraw instead.
width, height = self.dimensions

texture = None
try:
texture = gpu.types.GPUTexture((width, height), format='RGBA32F', data=self.pixels)
except ValueError:
print("Texture creation didn't work. width: {}, height: {}".format(width, height))

if texture:
draw_texture_2d(texture, (0, 0), texture.width, texture.height)
end = time.time()
print("texture create + draw took {} seconds".format(end - start))

def register():
from . import properties
Expand Down
1 change: 1 addition & 0 deletions bindings/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class cr_cb_info(ct.Structure):
("avg_per_ray_us", ct.c_double),
("samples_per_sec", ct.c_int64),
("eta_ms", ct.c_int64),
("finished_passes", ct.c_size_t),
("completion", ct.c_double),
("paused", ct.c_bool),
]
Expand Down
1 change: 1 addition & 0 deletions include/c-ray/c-ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct cr_renderer_cb_info {
double avg_per_ray_us;
int64_t samples_per_sec;
int64_t eta_ms;
size_t finished_passes;
double completion;
bool paused;
};
Expand Down
1 change: 0 additions & 1 deletion src/lib/api/c-ray.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ void cr_renderer_restart_interactive(struct cr_renderer *ext) {
r->state.workers.items[i].totalSamples = 0;
}
mutex_release(r->state.current_set->tile_mutex);
logr(info, "Renderer restarted\n");
}

struct cr_bitmap *cr_renderer_get_result(struct cr_renderer *ext) {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/datatypes/tile.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ struct render_tile *tile_next_interactive(struct renderer *r, struct tile_set *s
} else {
r->state.finishedPasses++;
struct cr_renderer_cb_info cb_info = { 0 };
cb_info.finished_passes = r->state.finishedPasses - 1;
struct callback cb = r->state.callbacks[cr_cb_on_interactive_pass_finished];
if (cb.fn) cb.fn(&cb_info, cb.user_data);
logr(info, "finished passes: %zu\n", r->state.finishedPasses);
set->finished = 0;
goto again;
}
Expand All @@ -64,7 +64,6 @@ struct render_tile *tile_next_interactive(struct renderer *r, struct tile_set *s
mutex_release(set->tile_mutex);
return NULL;
}
logr(info, "Sleeping 300ms, finishedPasses: %zu\n", r->state.finishedPasses);
timer_sleep_ms(300);
goto again;
}
Expand Down

0 comments on commit 1aa2020

Please sign in to comment.