Skip to content

Commit

Permalink
towards sdf
Browse files Browse the repository at this point in the history
  • Loading branch information
borisbat committed Feb 4, 2022
1 parent dff61f6 commit 5b8b844
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 18 deletions.
11 changes: 11 additions & 0 deletions modules/dasGlsl/glsl/glsl_common.das
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct sampler2D
struct image2D
texture2D : uint

struct image3D
texture3D : uint

def texture(sampler:sampler2D;uv:float2)
assert(false,"we should not be here\n")
return float4(0.)
Expand All @@ -39,10 +42,18 @@ def textureSize(sampler:sampler2D;lod:int)
def imageStore(img:image2D;uv:int2;data:float4)
assert(false,"we should not be here\n")

[sideeffects]
def imageStore(img:image3D;uvw:int3;data:float4)
assert(false,"we should not be here\n")

def imageLoad(img:image2D;uv:int2)
assert(false,"we should not be here\n")
return float4(0.)

def imageLoad(img:image3D;uvw:int3)
assert(false,"we should not be here\n")
return float4(0.)

def imageSize(img:image2D)
assert(false,"we should not be here\n")
return int2(0)
Expand Down
9 changes: 5 additions & 4 deletions modules/dasOpenGL/glsl/glsl_opengl.das
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ def private generate_bind_uniform(fnMain:FunctionPtr; var fn:FunctionPtr)
cll_uni.arguments |> emplace(cll_gul)
cll_uni.arguments |> emplace_new() <| new [[ExprConstInt() at=vv.at, value=binding]]
blk.list |>emplace(cll_uni)
elif is_glsl_structure(vv,"image2D")
// bind_sampler_2d(binding, varName.texture2D, format, access)
elif is_glsl_structure(vv,"image2D") || is_glsl_structure(vv,"image3D")
// bind_sampler_any(binding, varName.texture?D, format, access)
let is2D = is_glsl_structure(vv,"image2D")
var binding = find_arg("stage", vv.annotation) ?as tInt ?? 0
var vtex <- new [[ExprField() at=vv.at,
value <- new [[ExprVar() at=vv.at, name:=vv.name]],
name := "texture2D"
name := is2D ? "texture2D" : "texture3D"
]]
var format = find_arg("format", vv.annotation) ?as tString ?? "rgba8"
format = "GL_{to_upper(format)}"
Expand All @@ -104,7 +105,7 @@ def private generate_bind_uniform(fnMain:FunctionPtr; var fn:FunctionPtr)
access = "GL_WRITE_ONLY"
if find_arg("readonly", vv.annotation) is tBool
access = "GL_READ_ONLY"
var cll_uni <- new [[ExprCall() at=vv.at, name:="bind_image_2d"]]
var cll_uni <- new [[ExprCall() at=vv.at, name:="bind_image_any"]]
cll_uni.arguments |> emplace_new() <| new [[ExprConstInt() at=vv.at, value=binding]]
cll_uni.arguments |> emplace(vtex)
cll_uni.arguments |> emplace_new() <| new [[ExprVar() at=vv.at, name:=format]]
Expand Down
11 changes: 10 additions & 1 deletion modules/dasOpenGL/opengl/opengl_boost.das
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def bind_sampler_3d ( unit:int; tex:uint )
glActiveTexture(GL_TEXTURE0 + uint(unit))
glBindTexture(GL_TEXTURE_3D, tex)

def bind_image_2d ( unit:int; tex:uint; format:uint; access:uint=GL_READ_WRITE )
def bind_image_any ( unit:int; tex:uint; format:uint; access:uint=GL_READ_WRITE )
glBindImageTexture(uint(unit), tex, 0, false, 0, access, format)

def bind_ffp()
Expand Down Expand Up @@ -97,6 +97,15 @@ def glUniformAny ( location:int; value:float4 )
def glUniformAny ( location:int; value:float4x4 )
glUniformMatrix4fv(location, value)

def glUniformAny ( location:int; value:int2 )
glUniform2iv(location, 1, unsafe(addr(value.x)))

def glUniformAny ( location:int; value:int3 )
glUniform3iv(location, 1, unsafe(addr(value.x)))

def glUniformAny ( location:int; value:int4 )
glUniform4iv(location, 1, unsafe(addr(value.x)))

def glUniformMatrix4fv ( location:int; value:float4x4 )
unsafe
glUniformMatrix4fv(location, 1, false, addr(value[0][0]))
Expand Down
131 changes: 118 additions & 13 deletions modules/dasOpenGL/points/sdfi.das
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ var [[out]] f_FragColor : float4
var [[uniform]] f_Camera2World : float4x4
var [[uniform]] f_CameraNearFar : float2
var [[uniform]] f_PixelSize : float2
var [[uniform, stage=0]] t_sdf : sampler3D
var [[uniform, stage=1]] t_sdf_color : sampler3D

var [[uniform]] c_dim : int3
var [[uniform]] c_bmin : float3
var [[uniform]] c_step : float
var [[uniform, format=r32f, stage=0]] c_sdf : image3D
var [[uniform, format=rgba8, stage=0]] c_sdf_color : image3D

[compute_program(local_size_x=8,local_size_y=8,local_size_z=8)]
def map_effect
let xyz = int3(gl_GlobalInvocationID.xyz)
if xyz.x<c_dim.x || xyz.y<c_dim.y || xyz.z<c_dim.z
let p = c_bmin + float3(xyz) * c_step
let c = map_(p)
imageStore(c_sdf, xyz, c) // only x get written
imageStore(c_sdf_color, xyz, c.yzww) // yzww get written

[vertex_program]
def vs_main
Expand All @@ -38,29 +55,102 @@ def checker ( var p:float3 )
p *= 4.0
return ((int(p.x)^int(p.z)) & 1) != 0 ? float3(0.3,0.3,0.3) : float3(0.6,0.6,0.6)

def map ( pos:float3 )
var t3d : uint
var t3d_color : uint

let GEN_IN_SOFTWARE = true
let border = 0.1

def gen_map_texture ( var bmin,bmax:float3; step:float=0.025 )
bmin -= float3(border)
bmax += float3(border)
let idim = int3 ( (bmax-bmin)/step )
print("idim = {idim}\n")
var h : array<float>
var gb : array<uint>
h |> resize(idim.x*idim.y*idim.z)
gb |> resize(idim.x*idim.y*idim.z)

if GEN_IN_SOFTWARE
for z in 0..idim.z
let oz = z*idim.y*idim.x
for y in 0..idim.y
let oyz = oz + y*idim.x
for x in 0..idim.x
let p = bmin + float3(x,y,z) * step // + float3(step*0.5)
let c = map_(p)
h[oyz + x] = c.x
gb[oyz + x] = pack_float_to_byte(c.yzww * float4(255.0))

glGenTextures(1, safe_addr(t3d))
glBindTexture(GL_TEXTURE_3D, t3d)
glTexImage3D(GL_TEXTURE_3D, 0, int(GL_R32F), idim.x, idim.y, idim.z, 0, GL_RED, GL_FLOAT, unsafe(addr(h[0])))
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE )
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE )
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE )
glBindTexture(GL_TEXTURE_3D, 0u)

glGenTextures(1, safe_addr(t3d_color))
glBindTexture(GL_TEXTURE_3D, t3d_color)
glTexImage3D(GL_TEXTURE_3D, 0, int(GL_RGBA), idim.x, idim.y, idim.z, 0, GL_RGBA, GL_UNSIGNED_BYTE, unsafe(addr(gb[0])))
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE )
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE )
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE )
glBindTexture(GL_TEXTURE_3D, 0u)

if !GEN_IN_SOFTWARE

let map_program = create_compute_shader_program(@@map_effect)

glUseProgram(map_program)
// c_destTex := texture

c_sdf := t3d
c_sdf_color := t3d_color

map_effect_bind_uniform(map_program)
let disp_size = uint3(idim+int3(7)/8)
glDispatchCompute(disp_size.x, disp_size.y, disp.size_z)
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)
glUseProgram(0u)


t_sdf.texture3D := t3d
t_sdf_color.texture3D := t3d_color

def map_ ( pos:float3 )
var res = xCol ( 1e10, 0.0 )
res = xCol(sdPlane(pos.xyz-float3( 0.0,0.00, 0.0)), checker(pos) )
// [[-2.25, 0, -0.25]] [[-1.75, 0.5, 0.25]]
res = opU ( res, xCol(sdSphere(pos-float3(-2.0,0.25, 0.0),0.25),26.9) )
// [[-0.35, 0, -3.5]] [[0.35, 0.6, 1.5]]
if sdBox( pos-float3(0.0,0.3,-1.0),float3(0.35,0.3,2.5) ) < res.x
res = opU( res, xCol(sdBoundingBox( pos-float3( 0.0,0.25, 0.0), float3(0.3,0.25,0.2), 0.025 ), 16.9) )
res = opU( res, xCol(sdTorus( (pos-float3( 0.0,0.30, 1.0)).xzy, float2(0.25,0.05) ), 25.0) )
res = opU( res, xCol(sdCone( pos-float3( 0.0,0.45,-1.0), float2(0.6,0.8),0.45 ), 55.0) )
res = opU( res, xCol(sdCappedCone( pos-float3( 0.0,0.25,-2.0), 0.25, 0.25, 0.1 ), 13.67) )
res = opU( res, xCol(sdSolidAngle( pos-float3( 0.0,0.00,-3.0), float2(3,4)/5.0, 0.4 ), 49.13) )
// [[-0.65, 0, -3.5]] [[1.35, 0.6, 1.5]][]
if sdBox( pos-float3(1.0,0.3,-1.0),float3(0.35,0.3,2.5) ) < res.x
res = opU( res, xCol(sdCappedTorus((pos-float3( 1.0,0.30, 1.0))*float3(1,-1,1), float2(0.866025,-0.5), 0.25, 0.05), 8.5 ) )
res = opU( res, xCol(sdBox( pos-float3( 1.0,0.25, 0.0), float3(0.3,0.25,0.1) ), 3.0 ) )
res = opU( res, xCol(sdCapsule( pos-float3( 1.0,0.00,-1.0),float3(-0.1,0.1,-0.1), float3(0.2,0.4,0.2), 0.1 ), 31.0 ) )
res = opU( res, xCol(sdCylinder( pos-float3( 1.0,0.25,-2.0), float2(0.15,0.25) ), 8.0 ) )
res = opU( res, xCol(sdHexPrism( pos-float3( 1.0,0.2,-3.0), float2(0.2,0.05) ), 18.4 ) )
// [[-1.35, 0, -3.5]] [[0.65, 0.7, 1.5]]
if sdBox( pos-float3(-1.0,0.35,-1.0),float3(0.35,0.35,2.5)) < res.x
res = opU( res, xCol(sdPyramid( pos-float3(-1.0,-0.6,-3.0), 1.0 ), 13.56 ) )
res = opU( res, xCol(sdOctahedron( pos-float3(-1.0,0.15,-2.0), 0.35 ), 23.56 ) )
res = opU( res, xCol(sdTriPrism( pos-float3(-1.0,0.15,-1.0), float2(0.3,0.05) ), 43.5 ) )
// disabling elipsoid for now
// res = opU( res, xCol(sdEllipsoid( pos-float3(-1.0,0.25, 0.0), float3(0.2, 0.25, 0.05) ), 43.17 ) )
res = opU( res, xCol(sdRhombus( (pos-float3(-1.0,0.34, 1.0)).xzy, 0.15, 0.25, 0.04, 0.08 ), 17.0 ) )
// float3(2.0,0.3,-1.0),float3(0.35,0.3,2.5)
// [[1.65, 0, -3.5]] [[2.35, 0.6, 1.5]]
if sdBox( pos-float3(2.0,0.3,-1.0),float3(0.35,0.3,2.5) ) < res.x
res = opU( res, xCol(sdOctogonPrism(pos-float3( 2.0,0.2,-3.0), 0.2, 0.05), 51.8 ) )
res = opU( res, xCol(sdCylinder( pos-float3( 2.0,0.15,-2.0), float3(0.1,-0.1,0.0), float3(-0.2,0.35,0.1), 0.08), 31.2 ) )
Expand All @@ -69,13 +159,24 @@ def map ( pos:float3 )
res = opU( res, xCol(sdRoundCone( pos-float3( 2.0,0.20, 1.0), 0.2, 0.1, 0.3 ), 37.0 ) )
return res

def map_h ( pos:float3 )
var bmin = float3(-2.25, 0.0, -3.5) - float3(border)
var bmax = float3(2.35, 0.7, 1.5) + float3(border)
let tx = (pos - bmin) / (bmax - bmin)
return texture(t_sdf, tx).x

def map_c ( pos:float3 )
var bmin = float3(-2.25, 0.0, -3.5) - float3(border)
var bmax = float3(2.35, 0.7, 1.5) + float3(border)
let tx = (pos - bmin) / (bmax - bmin)
return texture(t_sdf_color, tx)

def calcSoftshadow ( ro,rd:float3; mint,tmax:float; tech:int )
var res = 1.0
var t = mint
var ph = 1e10
for i in 0..32
let h = map ( ro + rd*t ).x
let h = map_h ( ro + rd*t )
if tech==0
res = min(res, 10.0*h/t)
else
Expand All @@ -91,29 +192,30 @@ def calcSoftshadow ( ro,rd:float3; mint,tmax:float; tech:int )

def calcNormal ( pos:float3 )
let e = float2(1.0,-1.0)*0.5773*0.0005
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x )
return normalize( e.xyy*map_h( pos + e.xyy ) +
e.yyx*map_h( pos + e.yyx ) +
e.yxy*map_h( pos + e.yxy ) +
e.xxx*map_h( pos + e.xxx ) )

def castRay ( ro,rd:float3 )
var tmin = 0.05
var tmax = 25.0
var res = float4(-1,-1,-1,-1)
var res = -1.
var t = tmin
for i in 0..MAX_CAST_STEPS
let h = map(ro+rd*t)
if abs(h.x)<(0.001*t)
res = float4(t, h.yzw)
t += h.x
return res
let h = map_h(ro+rd*t)
if abs(h)<(0.001*t)
res = t
t += h
let c = map_c(ro+rd*t)
return float4(t,c.x,c.y,c.z)

def calcAO ( pos,nor:float3 )
var occ = 0.0
var sca = 1.0
for i in 0..5
let h = 0.001 + 0.15*float(i)/4.0
let d = map( pos + h*nor ).x
let d = map_h( pos + h*nor )
occ += (h-d)*sca
sca *= 0.95
return saturate(1.0-1.5*occ)
Expand Down Expand Up @@ -220,6 +322,9 @@ def main
glfwDestroyWindow(window)
glfwMakeContextCurrent(window)
create_gl_objects()

gen_map_texture ( float3(-2.25, 0.0, -3.5), float3(2.35, 0.7, 1.5) )

var camera_position = float3(0,3,15)
var camera_rotation_xy = float2(0,0)
var cursor_position_xy = glfwGetCursorPos(window)
Expand Down

0 comments on commit 5b8b844

Please sign in to comment.