height = image_in.shape[0] tex2d = vki.Texture2D(width, height, VK_FORMAT_R8G8B8A8_SRGB) tex2d.upload(image_in) colorBuf = vki.Texture2D(width, height, VK_FORMAT_R8G8B8A8_SRGB) positions = np.array([[0.0, -0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5]], dtype=np.float32) gpuPos = vki.device_vector_from_numpy(positions) colors = np.array([[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]], dtype=np.float32) gpuColors = vki.device_vector_from_numpy(colors) rp = vki.Rasterizer(['pos', 'col']) rp.add_draw_call( vki.DrawCall( ''' layout (location = 0) out vec2 vUV; void main() { vec2 grid = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); vec2 vpos = grid * vec2(2.0f, 2.0f) + vec2(-1.0f, -1.0f); gl_Position = vec4(vpos, 1.0f, 1.0f); vUV = grid; } ''', ''' layout (location = 0) in vec2 vUV; layout (location = 0) out vec4 outColor;
normal_inds = np.array(lst_normal_inds, dtype=np.int32) VK_FORMAT_R8G8B8A8_SRGB = 43 VK_FORMAT_D32_SFLOAT = 126 width = 640 height = 480 colorBuf = vki.Texture2D(width, height, VK_FORMAT_R8G8B8A8_SRGB) depthBuf = vki.Texture2D(width, height, VK_FORMAT_D32_SFLOAT, isDepth=True) gpuPos = vki.device_vector_from_numpy(positions) gpuNormals = vki.device_vector_from_numpy(normals) gpuInd_pos = vki.device_vector_from_numpy(vertex_inds) gpuInd_norm = vki.device_vector_from_numpy(normal_inds) rp = vki.Rasterizer( ['arr_pos', 'arr_norm', 'ind_pos', 'ind_norm', 'mat_pos', 'mat_norm']) rp.add_draw_call( vki.DrawCall( ''' layout (location = 0) out vec3 vNorm; void main() { vec3 pos = get_value(arr_pos, get_value(ind_pos, gl_VertexIndex)); vec3 norm = get_value(arr_norm, get_value(ind_norm, gl_VertexIndex)); vec4 pos_trans = mat_pos*vec4(pos, 1.0); pos_trans.y = -pos_trans.y; pos_trans.z = (pos_trans.z + pos_trans.w) / 2.0; gl_Position = pos_trans; vec4 norm_trans = mat_norm*vec4(norm, 0.0); vNorm = norm_trans.xyz;
def __init__(self, fn_skin = "skin_default.png", backface = False): self.width = 640 self.height = 480 proj_mat = glm.perspective(glm.radians(45.0), self.width/self.height, 0.1, 1000.0) view_mat = glm.lookAt(glm.vec3(10.0,10.0,10.0), glm.vec3(0.0,0.0,0.0), glm.vec3(0.0, 1.0, 0.0)) self.matrix = proj_mat*view_mat self.bg_color = [1.0, 1.0, 1.0, 1.0] self.bg_depth = 1.0 options = {} if backface: self.bg_depth = 0.0 options['depth_compare_op'] = VK_COMPARE_OP_GREATER skin_in = np.array(Image.open(fn_skin).convert('RGBA')) self.skin = vki.Cubemap(skin_in.shape[1], skin_in.shape[0]//6, VK_FORMAT_R8G8B8A8_SRGB) self.skin.upload(skin_in) self.rp = vki.Rasterizer(["matrix", "map", "dirs"], type_locked=True) self.rp.add_draw_call(vki.DrawCall( ''' const vec3 positions[18] = { vec3(3.0, 3.0, 3.0), vec3(3.0, 3.0, -3.0), vec3(3.0, -3.0, 3.0), vec3(-3.0, 3.0, -3.0), vec3(-3.0, 3.0, 3.0), vec3(-3.0, -3.0, -3.0), vec3(-3.0, 3.0, -3.0), vec3(3.0, 3.0, -3.0), vec3(-3.0, 3.0, 3.0), vec3(-3.0, -3.0, 3.0), vec3(3.0, -3.0, 3.0), vec3(-3.0, -3.0, -3.0), vec3(-3.0, 3.0, 3.0), vec3(3.0, 3.0, 3.0), vec3(-3.0, -3.0, 3.0), vec3(3.0, 3.0, -3.0), vec3(-3.0, 3.0, -3.0), vec3(3.0, -3.0, -3.0) }; const vec2 indices[24] = { vec2(0.0,0.0), vec2(0.0,1.0), vec2(1.0,1.0), vec2(1.0,1.0), vec2(1.0,0.0), vec2(0.0,0.0), vec2(1.0,0.0), vec2(0.0,0.0), vec2(0.0,1.0), vec2(0.0,1.0), vec2(1.0,1.0), vec2(1.0,0.0), vec2(1.0,1.0), vec2(1.0,0.0), vec2(0.0,0.0), vec2(0.0,0.0), vec2(0.0,1.0), vec2(1.0,1.0), vec2(0.0,1.0), vec2(1.0,1.0), vec2(1.0,0.0), vec2(1.0,0.0), vec2(0.0,0.0), vec2(0.0,1.0) }; layout (location = 0) out vec3 pos; void main() { uint id_vert = gl_VertexIndex % 6; uint id_sq_out = gl_VertexIndex / 6; uint id_in_face_out = id_sq_out % 9; uint id_face_out = id_sq_out / 9; uint id_x_out = id_in_face_out % 3; uint id_y_out = id_in_face_out / 3; vec3 o_out = positions[id_face_out*3]; vec3 ox_out = positions[id_face_out*3+1] - o_out; vec3 oy_out = positions[id_face_out*3+2] - o_out; vec2 idv_out = indices[id_vert]; vec3 pos_out = o_out + ox_out *(float(id_x_out)+idv_out.x)/3.0 + oy_out *(float(id_y_out)+idv_out.y)/3.0; vec4 wpos = vec4(pos_out, 1.0); vec4 ppos = matrix * wpos; ppos.y = -ppos.y; ppos.z = (ppos.z + ppos.w) / 2.0; gl_Position = ppos; uint id_sq_in = get_value(map, id_sq_out); uint id_in_face_in = id_sq_in % 9; uint id_face_in = id_sq_in / 9; uint id_x_in = id_in_face_in % 3; uint id_y_in = id_in_face_in / 3; uint dir_in = get_value(dirs, id_sq_out); vec3 o_in = positions[id_face_in*3]; vec3 ox_in = positions[id_face_in*3+1] - o_in; vec3 oy_in = positions[id_face_in*3+2] - o_in; vec2 idv_in = indices[6*dir_in + id_vert]; vec3 pos_in = o_in + ox_in *(float(id_x_in)+idv_in.x)/3.0 + oy_in *(float(id_y_in)+idv_in.y)/3.0; pos = pos_in; } ''', ''' layout (location = 0) in vec3 pos; layout (location = 0) out vec4 outColor; void main() { vec3 dir = normalize(pos); outColor = texture(arr_cubemap[0], dir); } ''', options=options))
def __init__(self, colors = [[0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.2, 0.0], [0.2, 0.2, 0.2]]): self.wh = 512 self.bg_color = [0.0,0.0,0.0,1.0] self.colors = np.array(colors, dtype = np.float32) self.rp = vki.Rasterizer(["map", "colors", "up_face_id", "cross_mode"], type_locked=True) self.rp.add_draw_call(vki.DrawCall( ''' const vec2 positions[6] = { vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0), vec2(0.0, 0.0) }; layout (location = 0) flat out uint v_id_face_in; layout (location = 1) flat out uint v_id_in_face_in; void main() { uint id_vert = gl_VertexIndex % 6; uint id_sq_out = gl_VertexIndex / 6; uint id_in_face_out, id_face_out; vec2 k = positions[id_vert]; vec2 pos; if (id_sq_out<9) { id_in_face_out = id_sq_out; id_face_out = 2; uint id_x_out = id_in_face_out % 3; uint id_y_out = id_in_face_out / 3; pos.x = -0.77 + 0.52*float(id_x_out) + k.x*0.5; pos.y = -0.77 + 0.52*float(id_y_out) + k.y*0.5; } else if(id_sq_out<12) { id_in_face_out = id_sq_out - 9; id_face_out = 0; pos.x = 0.78 + k.x*0.05; pos.y = -0.77 + 0.52*float(2 - id_in_face_out) + k.y*0.5; } else if(id_sq_out<15) { id_in_face_out = id_sq_out - 12; id_face_out = 1; pos.x = -0.83 + k.x*0.05; pos.y = -0.77 + 0.52*float(id_in_face_out) + k.y*0.5; } else if(id_sq_out<18) { id_in_face_out = id_sq_out - 15; id_face_out = 4; pos.x = -0.77 + 0.52*float(id_in_face_out) + k.x*0.5; pos.y = 0.78 + k.y*0.05; } else { id_in_face_out = id_sq_out - 18; id_face_out = 5; pos.x = -0.77 + 0.52*float(2 - id_in_face_out) + k.x*0.5; pos.y = -0.83 + k.y*0.05; } gl_Position = vec4(pos, 0.5, 1.0); id_sq_out = id_face_out * 9 + id_in_face_out; uint id_sq_in = get_value(map, id_sq_out); v_id_face_in = id_sq_in / 9; v_id_in_face_in = id_sq_in % 9; } ''', ''' layout (location = 0) flat in uint v_id_face_in; layout (location = 1) flat in uint v_id_in_face_in; layout (location = 0) out vec4 outColor; void main() { if (up_face_id!=-1 && v_id_face_in != up_face_id) { outColor = vec4(get_value(colors,6),1.0); } else if (cross_mode!=0 && v_id_in_face_in/3!=1 && v_id_in_face_in%3!=1) { outColor = vec4(get_value(colors,6),1.0); } else { outColor = vec4(get_value(colors,v_id_face_in),1.0); } } '''))
class Film(vki.ShaderViewable): def __init__(self, width, height): self.m_width = width self.m_height = height self.m_svwidth = vki.SVInt32(width) self.m_svheight = vki.SVInt32(height) self.m_svbuf = vki.SVBuffer('vec3', width * height) self.m_cptr = SVCombine_Create( { 'data': self.m_svbuf, 'width': self.m_svwidth, 'height': self.m_svheight }, ''' vec3 read_pixel(in Comb_#hash# img, int x, int y) { return img.data[x + y*img.width].v; } void write_pixel(in Comb_#hash# img, int x, int y, in vec3 v) { img.data[x + y*img.width].v = v; } void incr_pixel(in Comb_#hash# img, int x, int y, in vec3 v) { vec3 col = read_pixel(img, x, y); write_pixel(img, x, y, col + v); } void incr_pixel(in Comb_#hash# img, int x, int y, in Spectrum col) { incr_pixel(img, x, y, to_xyz(col)); } void incr_pixel_atomic(in Comb_#hash# img, int x, int y, in vec3 v) { atomicAdd(img.data[x + y*img.width].v.x, v.x); atomicAdd(img.data[x + y*img.width].v.y, v.y); atomicAdd(img.data[x + y*img.width].v.z, v.z); } void incr_pixel_atomic(in Comb_#hash# img, int x, int y, in Spectrum col) { incr_pixel_atomic(img, x, y, to_xyz(col)); } ''') self.m_times_exposure = 0.0 def inc_times_exposure(self, count=1.0): self.m_times_exposure += count film2srgb = vki.Rasterizer(['film', 'times_expo', 'boost'], type_locked=True) film2srgb.add_draw_call( vki.DrawCall( ''' layout (location = 0) out vec2 vUV; void main() { vec2 grid = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); vec2 vpos = grid * vec2(2.0f, 2.0f) + vec2(-1.0f, -1.0f); gl_Position = vec4(vpos, 1.0f, 1.0f); vUV = grid; } ''', ''' layout (location = 0) in vec2 vUV; layout (location = 0) out vec4 outColor; void main() { int x = int(vUV.x * float(film.width)); int y = int(vUV.y * float(film.height)); vec3 xyz = read_pixel(film, x, y) * boost / times_expo; vec3 rgb = clamp(xyz2rgb(xyz), 0.0, 1.0); outColor = vec4(rgb, 1.0); } ''')) def download_srgb(self, boost=1.0): sv_times_expo = vki.SVFloat(self.m_times_exposure) sv_boost = vki.SVFloat(boost) colorBuf = vki.Texture2D(self.m_width, self.m_height, VK_FORMAT_R8G8B8A8_SRGB) self.film2srgb.launch([3], [colorBuf], None, [0.5, 0.5, 0.5, 1.0], 1.0, [self, sv_times_expo, sv_boost]) srgb = np.empty((self.m_height, self.m_width, 4), dtype=np.uint8) colorBuf.download(srgb) return srgb