我已经使用julia编程语言编写了路径跟踪器,但我认为它很慢

bit*_*ise 2 raytracing julia

我已经更改了帖子,并发布了我的整个代码!有人可以告诉我如何优化它吗?

import Base: *, +, -, /, ^
using Images
const ? = convert(Float64, ?)

#define vector
mutable struct Vec3
    x::Float64
    y::Float64
    z::Float64
end

function +(u::Vec3, v::Vec3)
    Vec3(u.x+v.x, u.y+v.y, u.z+v.z)
end

function -(u::Vec3, v::Vec3)
    Vec3(u.x-v.x, u.y-v.y, u.z-v.z)
end

function /(u::Vec3, v::Float64)
    Vec3(u.x/v, u.y/v, u.z/v)
end

function *(u, v::Vec3)
    if typeof(u) == Float64
        Vec3(u*v.x, u*v.y, u*v.z)
    elseif typeof(u) == Vec3
        Vec3(u.x*v.x, u.y*v.y, u.z*v.z)
    end
end

function ^(u::Vec3, v::Float64)
    Vec3(u.x^v, u.y^v, u.z^v)
end

function dot(u::Vec3, v::Vec3)
    u.x*v.x + u.y*v.y + u.z*v.z
end

function normalize(u::Vec3)
    u/sqrt(dot(u,u))
end

function cross(u::Vec3, v::Vec3)
    Vec3(u.y*v.z - v.y*u.z, u.z*v.x - v.z*u.x, u.x*v.y - v.x*u.y)
end

function gamma(u::Vec3)
    Vec3(u.x^(1/2.2), u.y^(1/2.2), u.z^(1/2.2))
end

function clamp(u::Vec3)
    u.x = u.x <= 1 ? u.x : 1
    u.y = u.y <= 1 ? u.y : 1
    u.z = u.z <= 1 ? u.z : 1
    u
end

#define ray
struct Ray
    s::Vec3
    d::Vec3
end

#define planes
struct xyRect
    z; x1; x2; y1; y2::Float64
    normal; emittance; reflectance::Vec3
    isLight::Bool
end

struct xzRect
    y; x1; x2; z1; z2::Float64
    normal; emittance; reflectance::Vec3
    isLight::Bool
end

struct yzRect
    x; y1; y2; z1; z2::Float64
    normal; emittance; reflectance::Vec3
    isLight::Bool
end

#define sphere
mutable struct Sphere
    radius::Float64
    center; normal; emittance; reflectance::Vec3
    isLight::Bool
end

#define empty object
struct Empty
    normal; emittance; reflectance::Vec3
end

#define surfaces
Surfaces = Union{xyRect, xzRect, yzRect, Sphere}

#define intersection function
function intersect(surface::Surfaces, ray::Ray)
if typeof(surface) == xyRect
    t = (surface.z - ray.s.z)/ray.d.z
    if surface.x1 < ray.s.x + t*ray.d.x < surface.x2 && surface.y1 < ray.s.y + t*ray.d.y < surface.y2 && t > 0
        t
    else
        Inf
    end

elseif typeof(surface) == xzRect
    t = (surface.y - ray.s.y)/ray.d.y
    if surface.x1 < ray.s.x + t*ray.d.x < surface.x2 && surface.z1 < ray.s.z + t*ray.d.z < surface.z2 && t > 0
        t
    else
        Inf
    end

elseif typeof(surface) == yzRect
    t = (surface.x - ray.s.x)/ray.d.x
    if surface.y1 < ray.s.y + t*ray.d.y < surface.y2 && surface.z1 < ray.s.z + t*ray.d.z < surface.z2 && t > 0
        t
    else
        Inf
    end

elseif typeof(surface) == Sphere
    a = dot(ray.d, ray.d)
    b = 2dot(ray.d, ray.s - surface.center)
    c = dot(ray.s - surface.center, ray.s - surface.center) - surface.radius*surface.radius
    ? = b*b - 4*a*c
    if ? > 0
        ? = sqrt(?)
        t1 = 0.5(-b-?)/a
        t2 = 0.5(-b+?)/a
        if t1 > 0
            surface.normal = normalize(ray.s + t1*ray.d - surface.center)
            t1
        elseif t2 > 0
            surface.normal = normalize(ray.s + t2*ray.d - surface.center)
            t2
        else
            Inf
        end
    else
        Inf
    end
end
end
#define nearest function
function nearest(surfaces::Array{Surfaces, 1}, ray::Ray, tMin::Float64)
    hitSurface = Empty(Vec3(0,0,0), Vec3(0,0,0), Vec3(0,0,0))
    for surface in surfaces
        t = intersect(surface, ray)
        if t < tMin
            tMin = t
            hitSurface = surface
        end
    end
    tMin, hitSurface
end
#cosine weighted sampling of hemisphere
function hemiRand(n::Vec3)
    ?1 = rand()
    ?2 = rand()
    x = cos(2?*?2)*sqrt(?1)
    y = sin(2?*?2)*sqrt(?1)
    z = sqrt(1-?1)
    r = normalize(Vec3(2rand()-1, 2rand()-1, 2rand()-1))
    b = cross(n,r)
    t = cross(n,b)
    Vec3(x*t.x + y*b.x + z*n.x, x*t.y + y*b.y + z*n.y, x*t.z + y*b.z + z*n.z)
end
#trace the path
function trace(surfaces::Array{Surfaces, 1}, ray::Ray, depth::Int64, maxDepth::Int64)
    if depth >= maxDepth
        return Vec3(0,0,0)
    end
    t, material = nearest(surfaces, ray, Inf)
    if typeof(material) == Empty
        return Vec3(0,0,0)
    end
    if material.isLight == true
        return material.emittance
    end
    ? = material.reflectance
    BRDF = ?/?
    n = material.normal
    R = hemiRand(n)
    In = trace(surfaces, Ray(ray.s + t*ray.d, R), depth+1, maxDepth)
    return ?*BRDF*In
end
#define camera
struct Camera
    eye; v_up; N::Vec3
    fov; aspect; distance::Float64
end    
#render function
function render(surfaces::Array{Surfaces,1},camera::Camera,xRes::Int64,yRes::Int64,numSamples::Int64,maxDepth::Int64)
    n = normalize(camera.N)
    e = camera.eye
    c = e - camera.distance*n
    ? = camera.fov*(?/180)
    H = 2*camera.distance*tan(?/2)
    W = H*camera.aspect
    u = normalize(cross(camera.v_up,n))
    v = cross(n,u)
    img = zeros(3, xRes, yRes)
    pixHeight = H/yRes
    pixWidth = W/xRes
    L = c - 0.5*W*u - 0.5*H*v
    for i=1:xRes
        for j=1:yRes
            cl = Vec3(0,0,0)
            for s=1:numSamples
                pt = L + (i-rand())*pixWidth*u + (yRes-j+rand())*pixHeight*v
                cl = cl + trace(surfaces, Ray(e, pt-e), 0, maxDepth)
            end
            cl = gamma(clamp(cl/convert(Float64, numSamples)))
            img[:,j,i] = [cl.x, cl.y, cl.z]
        end
    end
    img
end
#the scene
p1 = xzRect(1.,0.,1.,-1.,0.,Vec3(0,-1,0),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
p2 = xzRect(0.,0.,1.,-1.,0.,Vec3(0,1,0),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
p3 = xyRect(-1.,0.,1.,0.,1.,Vec3(0,0,1),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
p4 = yzRect(0.,0.,1.,-1.,0.,Vec3(1,0,0),Vec3(0,0,0),Vec3(0.75,0.25,0.25),false)
p5 = yzRect(1.,0.,1.,-1.,0.,Vec3(-1,0,0),Vec3(0,0,0),Vec3(0.25,0.25,0.75),false)
p6 = xzRect(0.999,0.35,0.65,-0.65,-0.35,Vec3(0,-1,0),Vec3(18,18,18),Vec3(0,0,0),true)
s1 = Sphere(0.15,Vec3(0.3,0.15,-0.6),Vec3(0,0,0),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
surfs = Surfaces[p1,p2,p3,p4,p5,p6,s1]
cam = Camera(Vec3(0.5,0.5,2),Vec3(0,1,0),Vec3(0,0,1),28.07,1,2)
@time image = render(surfs, cam, 400, 400, 1, 4);
colorview(RGB, image)
Run Code Online (Sandbox Code Playgroud)

我需要知道为什么我的代码不好并且很慢。我是一名初学者程序员,但经验不足。我的路径跟踪器场景包含7个对象,其最大深度为4,并且要花费2秒钟以上的时间才能生成大小为400 * 400的图像。我认为应该不会这么慢,因为我的CPU是核心i74770。很抱歉更改我的帖子。

Kri*_*son 6

首先,

struct yzRect
x; y1; y2; z1; z2::Float64
normal; emittance; reflectance::Vec3
isLight::Bool
end
Run Code Online (Sandbox Code Playgroud)

最终仅将类型应用于每行的最后一个变量:

julia> fieldtypes(yzRect)
(Any, Any, Any, Any, Float64, Any, Any, Vec3, Bool)
Run Code Online (Sandbox Code Playgroud)

因此Julia基本上不会知道您的结构中会减慢速度的任何类型。

另外,您的值Vec3实际上应该是不变的,然后在您要“修改” Vector时创建它的新实例。

可能还有更多问题,但其中有两个突出。

通过阅读https://docs.julialang.org/en/v1/manual/performance-tips/index.html和应用中存在的指导方针强烈推荐分析性能时。