Renderer.cu 890 Bytes
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
#include "Renderer.h"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
2
#include "../utils/cuda.h"
Nianchen Deng's avatar
Nianchen Deng committed
3

Nianchen Deng's avatar
sync    
Nianchen Deng committed
4
5
/// Dispatch (n_rays, -)
__global__ void cu_render(glm::vec4 *o_colors, glm::vec4 *layeredColors, uint samples, uint nRays) {
Nianchen Deng's avatar
Nianchen Deng committed
6
    glm::uvec3 idx3 = IDX3;
Nianchen Deng's avatar
sync    
Nianchen Deng committed
7
8
    uint rayIdx = idx3.x;
    if (rayIdx >= nRays)
Nianchen Deng's avatar
Nianchen Deng committed
9
10
        return;
    glm::vec4 outColor;
Nianchen Deng's avatar
sync    
Nianchen Deng committed
11
12
    for (int si = samples - 1; si >= 0; --si) {
        glm::vec4 c = layeredColors[rayIdx * samples + si];
Nianchen Deng's avatar
Nianchen Deng committed
13
14
15
16
17
18
        outColor = outColor * (1 - c.a) + c * c.a;
    }
    outColor.a = 1.0f;
    o_colors[idx3.x] = outColor;
}

Nianchen Deng's avatar
sync    
Nianchen Deng committed
19
Renderer::Renderer() {}
Nianchen Deng's avatar
Nianchen Deng committed
20
21

void Renderer::render(sptr<CudaArray<glm::vec4>> o_colors,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
22
23
24
25
26
                      sptr<CudaArray<glm::vec4>> layeredColors) {
    dim3 blkSize(1024);
    dim3 grdSize(ceilDiv(o_colors->n(), blkSize.x));
    CU_INVOKE(cu_render)
    (*o_colors, *layeredColors, layeredColors->n() / o_colors->n(), o_colors->n());
Nianchen Deng's avatar
Nianchen Deng committed
27
28
    CHECK_EX(cudaGetLastError());
}