Example #1
0
    def __init__(self,
                 positions,
                 normals,
                 indices_pos,
                 indices_norm,
                 modelMat=glm.identity(glm.mat4),
                 color=(1.0, 1.0, 1.0)):
        Geometry.__init__(self, modelMat)
        self.m_gpuPos = vki.device_vector_from_numpy(positions)
        self.m_gpuNorm = vki.device_vector_from_numpy(normals)
        self.m_gpuIndPos = vki.device_vector_from_numpy(indices_pos)
        self.m_gpuIndNorm = vki.device_vector_from_numpy(indices_norm)
        self.m_blas = vki.BaseLevelAS(self.m_gpuIndPos, self.m_gpuPos)

        vert0 = positions[0]
        self.m_aabb = np.array(
            [vert0[0], vert0[1], vert0[2], vert0[0], vert0[1], vert0[2]],
            dtype=np.float32)
        for vert in positions:
            self.m_aabb[0] = min(self.m_aabb[0], vert[0])
            self.m_aabb[1] = min(self.m_aabb[1], vert[1])
            self.m_aabb[2] = min(self.m_aabb[2], vert[2])
            self.m_aabb[3] = max(self.m_aabb[3], vert[0])
            self.m_aabb[4] = max(self.m_aabb[4], vert[1])
            self.m_aabb[5] = max(self.m_aabb[5], vert[2])

        self.d_normMat = vki.SVMat4x4(self.m_normMat)
        self.d_color = Spectrum(color)
        self.m_cptr = SVCombine_Create(
            {
                'indices': self.m_gpuIndNorm,
                'normals': self.m_gpuNorm,
                'normalMat': self.d_normMat,
                'color': self.d_color
            }, '''
void closethit(in Comb_#hash# self, int primitiveId, in vec3 barycentrics, inout {HitInfo_Lambert} hitinfo)
{{    
    uint i0 = get_value(self.indices, 3 * primitiveId);
    uint i1 = get_value(self.indices, 3 * primitiveId + 1);
    uint i2 = get_value(self.indices, 3 * primitiveId + 2);

    vec3 norm0 = get_value(self.normals, i0);
    vec3 norm1 = get_value(self.normals, i1);
    vec3 norm2 = get_value(self.normals, i2);

    vec3 normal = norm0 * barycentrics.x + norm1 * barycentrics.y + norm2 * barycentrics.z;
    normal = normalize((self.normalMat * vec4(normal, 0.0)).xyz);   

    hitinfo.lambert.color = self.color;    
    hitinfo.normal = normal;

}}
'''.format(HitInfo_Lambert=Name_HitInfo_Lambert))
Example #2
0
    def render(self, cube, filename = None):        
        colorBuf = vki.Texture2D(self.width, self.height, VK_FORMAT_R8G8B8A8_SRGB)
        colorBuf4x = vki.Texture2D(self.width, self.height, VK_FORMAT_R8G8B8A8_SRGB, samples=4)
        depthBuf4x = vki.Texture2D(self.width, self.height, VK_FORMAT_D32_SFLOAT, isDepth=True, samples=4)       
        
        gpu_matrix = vki.SVMat4x4(self.matrix)
        gpu_map = vki.device_vector_from_numpy(cube.map)
        gpu_dirs = vki.device_vector_from_numpy(cube.dirs)
        self.rp.launch([6*9*6], [colorBuf4x], depthBuf4x, self.bg_color, self.bg_depth, [gpu_matrix, gpu_map, gpu_dirs], resolveBufs=[colorBuf], cubemaps = [self.skin])

        image_out = np.empty((self.height, self.width, 4), dtype=np.uint8)
        colorBuf.download(image_out)

        if not filename is None:
            Image.fromarray(image_out, 'RGBA').save(filename)       

        return image_out
Example #3
0
    def render(self, cube, filename = None, up_face_only=False, up_face_id = 2, cross_mode = False):                
        colorBuf = vki.Texture2D(self.wh, self.wh, VK_FORMAT_R8G8B8A8_SRGB)             
        
        gpu_map = vki.device_vector_from_numpy(cube.map)
        gpu_colors = vki.device_vector_from_numpy(self.colors)

        if cross_mode:
            up_face_only = True

        if not up_face_only:
            up_face_id = -1

        gpu_up_face_id = vki.SVInt32(up_face_id)
        gpu_cross_mode = vki.SVInt32(cross_mode)
        self.rp.launch([(4*3+9)*6], [colorBuf], None, self.bg_color, 1.0, [gpu_map, gpu_colors, gpu_up_face_id, gpu_cross_mode])

        image_out = np.empty((self.wh, self.wh, 4), dtype=np.uint8)
        colorBuf.download(image_out)

        if not filename is None:
            Image.fromarray(image_out, 'RGBA').save(filename) 

        return image_out
Example #4
0
image_in = np.array(Image.open('fei.png').convert('RGBA'))

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)

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);
Example #5
0
import VkInline as vki
import numpy as np
import threading

harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32')
darr = vki.device_vector_from_numpy(harr)

forLoop = vki.For(['arr_in', 'arr_out', 'k'], "inner", '''
void inner(uint idx)
{
    set_value(arr_out, idx, get_value(arr_in, idx)*k);
}
''')


def thread_func():
    darr_out = vki.SVVector('float', 5)
    forLoop.launch_n(5, [darr, darr_out, vki.SVFloat(10.0)])
    print(darr_out.to_host())


a = threading.Thread(target=thread_func)
b = threading.Thread(target=thread_func)
c = threading.Thread(target=thread_func)

a.start()
b.start()
c.start()
c.join()
b.join()
a.join()
Example #6
0
    for ind in shape.mesh.indices:
        lst_vertex_inds += [ind.vertex_index]
        lst_normal_inds += [ind.normal_index]

vertex_inds = np.array(lst_vertex_inds, dtype=np.int32)
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));
Example #7
0
    def __init__(self):
        xorwow_data = np.fromfile(os.path.dirname(__file__) + '/' +
                                  'xor_wow_data.bin',
                                  dtype=np.uint32)
        self.d_xorwow_data = vki.device_vector_from_numpy(xorwow_data)
        self.m_cptr = SVCombine_Create({'data': self.d_xorwow_data}, '''
void matvec_i(int i, uint v_i, in Comb_#hash# initializer, int offset, inout V5 result)
{
    for (int j = 0; j < 32; j++)
        if ((v_i & (1 << j))!=0)
        {
            int k = (i * 32 + j)*5 + offset;            
            result.v0 ^= get_value(initializer.data, k);
            result.v1 ^= get_value(initializer.data, k + 1);
            result.v2 ^= get_value(initializer.data, k + 2);
            result.v3 ^= get_value(initializer.data, k + 3);
            result.v4 ^= get_value(initializer.data, k + 4);
        }
}

void matvec(in V5 vector, in Comb_#hash# initializer, int offset, inout V5 result)
{
    result.v0 = result.v1 = result.v2 = result.v3 = result.v4 = 0;
    matvec_i(0, vector.v0, initializer, offset, result);
    matvec_i(1, vector.v1, initializer, offset, result);
    matvec_i(2, vector.v2, initializer, offset, result);
    matvec_i(3, vector.v3, initializer, offset, result);
    matvec_i(4, vector.v4, initializer, offset, result);                    
}

void state_init(in Comb_#hash# initializer, uint64_t seed, uint64_t subsequence, inout RNGState state)
{
    if (subsequence>= (1<<18) ) subsequence= (1<<18) -1;

    uint s0 = uint(seed) ^ 0xaad26b49U;
    uint s1 = uint(seed >> 32) ^ 0xf7dcefddU;
    uint t0 = 1099087573U * s0;
    uint t1 = 2591861531U * s1;
    state.d = 6615241 + t1 + t0;
    state.v.v0 = 123456789U + t0;
    state.v.v1 = 362436069U ^ t0;
    state.v.v2 = 521288629U + t1;
    state.v.v3 = 88675123U ^ t1;
    state.v.v4 = 5783321U + t0;

    // apply sequence matrix
    V5 result;
    uint64_t p = subsequence;
    int i_mat = 0;

    while (p!=0 && i_mat<7)
    {
        for (uint t = 0; t < (p & 3); t++)
        {
            matvec(state.v, initializer, i_mat*800, result);
            state.v = result;
        }
        p >>= 2;
        i_mat++;
    }
    
    for (uint t = 0; t < (p & 0xF); t++)
    {
        matvec(state.v, initializer, i_mat*800, result);
        state.v = result;
    }
}
''')
Example #8
0
import VkInline as vki
import numpy as np
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);