def thread_func(): darr_out = vki.SVVector('float', 5) darr_out2 = vki.SVVector('int', 5) forLoop.launch_n(5, [darr, darr_out, vki.SVFloat(10.0)]) forLoop.launch_n(5, [darr2, darr_out2, vki.SVInt32(5)]) print(darr_out.to_host()) print(darr_out2.to_host())
import numpy as np import VkInline as vki import FeiRaysInline as fri count = 1 << 18 d_states = vki.SVVector('RNGState', count) initializer = fri.RNGInitializer() initializer.InitRNGVector(d_states) states = np.empty((count, 6), dtype=np.uint32) d_states.m_buf.to_host(states.__array_interface__['data'][0]) print(states)
kernel = vki.Computer(['dst', 'src', 'n'], ''' shared {0} s_buf[{1}]; void main() {{ uint tid = gl_LocalInvocationID.x; uint i = gl_GlobalInvocationID.x; if (i<n) s_buf[tid] = get_value(src, i); barrier(); for (uint s = {1}/2; s>0; s>>=1) {{ if (tid < s && i+s<n) s_buf[tid] += s_buf[tid + s]; barrier(); }} if (tid==0) set_value(dst, gl_WorkGroupID.x, s_buf[tid]); }} '''.format('int',str(BLOCK_SIZE))) dst = darr while dst.size()>1: src = dst n = src.size() blocks = int((n + BLOCK_SIZE - 1) / BLOCK_SIZE) dst = vki.SVVector("int", blocks) kernel.launch(blocks, BLOCK_SIZE, [dst, src, vki.SVUInt32(n)]) print(dst.to_host()[0])
import VkInline as vki import numpy as np # interface with numpy harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32') darr = vki.device_vector_from_numpy(harr) harr2 = np.array([6,7,8,9,10], dtype='int32') darr2 = vki.device_vector_from_numpy(harr2) # test launching non-templated for forLoop = vki.For(['arr_in','arr_out','k'], "inner", ''' void inner(uint idx) { set_value(arr_out, idx, get_value(arr_in, idx)*k); } ''') darr_out = vki.SVVector('float', 5) forLoop.launch(0, 5, [darr, darr_out, vki.SVFloat(10.0)]) print (darr_out.to_host()) darr_out = vki.SVVector('int', 5) forLoop.launch_n(5, [darr2, darr_out, vki.SVInt32(5)]) print (darr_out.to_host())
from PIL import Image import glm width = 800 height = 400 aabb_unit_sphere = np.array([-1.0, -1.0, -1.0, 1.0, 1.0, 1.0], dtype=np.float32) d_aabb_unit_sphere = vki.device_vector_from_numpy(aabb_unit_sphere) blas_unit_sphere = vki.BaseLevelAS(gpuAABB=d_aabb_unit_sphere) transform = glm.identity(glm.mat4) transform = glm.translate(transform, glm.vec3(0.0, 0.0, -1.0)) transform = glm.scale(transform, glm.vec3(0.5, 0.5, 0.5)) tlas = vki.TopLevelAS([[(blas_unit_sphere, transform)]]) darr_out = vki.SVVector('vec3', width * height) raytracer = vki.RayTracer(['arr_out', 'width', 'height'], ''' struct Payload { float t; vec3 color; }; layout(location = 0) rayPayloadEXT Payload payload; void main() { int x = int(gl_LaunchIDEXT.x); int y = int(gl_LaunchIDEXT.y); if (x>=width || y>height) return;
import VkInline as vki import numpy as np from PIL import Image image_in = np.array(Image.open('fei.png').convert('RGBA')) # print(image_in.shape, image_in.dtype) VK_FORMAT_R8G8B8A8_SRGB = 43 width = image_in.shape[1] height = image_in.shape[0] tex2d = vki.Texture2D(width, height, VK_FORMAT_R8G8B8A8_SRGB) tex2d.upload(image_in) darr = vki.SVVector('vec4', width * height) kernel = vki.Computer(['width', 'height', 'arr'], ''' void main() { uint x = gl_GlobalInvocationID.x; uint y = gl_GlobalInvocationID.y; if (x >= width || y>=height) return; float u = (float(x)+0.5)/float(width); float v = (float(y)+0.5)/float(height); vec4 rgba = texture(arr_tex2d[0], vec2(u,v)); set_value(arr, x+y*width, rgba); }
def trace(self, scene, num_iter=100, interval=-1): if self.m_rng_states == None: print("Initializing RNG states..") self.m_rng_states = vki.SVVector('RNGState', self.m_batch_size) initializer = RNGInitializer() initializer.InitRNGVector(self.m_rng_states) scene.update() sunlight = '' estimate_light = '' for key in scene.m_obj_lists: sublist = scene.m_obj_lists[key] if sublist['is_light_source']: estimate_light += self.template_estimate_light.format( name_list=sublist['name']) if sublist['name'] == 'sun_lights': sunlight = self.sunlight miss = self.payload + self.template_miss.format( sunlight=sunlight) + self.template_update_payload.format( estimate_light='') print("Doing ray-tracing..") lst_param_names = ['camera', 'states', 'sky', 'pdf_lights'] for key in scene.m_obj_lists: sublist = scene.m_obj_lists[key] lst_param_names += [sublist['name']] hit_shaders = [] for key in scene.m_obj_lists: sublist = scene.m_obj_lists[key] if sublist['is_geometry']: closest_hit = self.payload + sublist[ 'closest_hit'] + self.template_update_payload.format( estimate_light=estimate_light) intersection = sublist['intersection'] hit_shaders += [ vki.HitShaders(closest_hit=closest_hit, intersection=intersection) ] ray_tracer = vki.RayTracer(lst_param_names, self.raygen, [miss], hit_shaders) if interval == -1: interval = num_iter lst_params = [ self.m_camera, self.m_rng_states, scene.m_sky, scene.m_pdf_lights ] + scene.m_lst_obj_lsts i = 0 while i < num_iter: end = i + interval if end > num_iter: end = num_iter ray_tracer.launch(self.m_batch_size, lst_params, [scene.m_tlas], tex2ds=scene.m_tex2d_list, tex3ds=scene.m_tex3d_list, cubemaps=scene.m_cubemap_list, times_submission=end - i) self.m_camera.m_film.inc_times_exposure(end - i) i = end if i < num_iter: print('%.2f%%' % (i / num_iter * 100.0))