View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Scene Voxelization

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Migi0027 (肉コーダ)  Members

Posted 26 September 2013 - 05:26 AM

Hi guys!

Recently I have been working on my Voxel Cone Tracer, most of setup is done, but the actual voxelization isn't working right now, which is blocking further progress, or getting some results in my other code.

Here's my attempt:

Generating matrices (Some code may seem stupid, unnecessary, but again, right now my goal is to get it working, nothing else):

#define TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
D3DXVECTOR3 GetCubeMapLookat(int side)
{
switch (side)
{
case 0x8515: // +X
return CEVector3f(1, 0, 0);
break;
case 0x8516: // -X
return CEVector3f(-1, 0, 0);
break;
case 0x8517: // +Y
return CEVector3f(0, 1, 0);
break;
case 0x8518: // -Y
return CEVector3f(0, -1, 0);
break;
case 0x8519: // +Z
return CEVector3f(0, 0, 1);
break;
case 0x851A: // -Z
return CEVector3f(0, 0, -1);
break;
}

// What the beep are you doing here?
return CEVector3f(0, 0, 0);
}
_until(n, 6)
{
until( m_vVoxelisers.size() ) // A vector containing all meshes needed to be voxelized
{ // m_vVoxelisers->src is the mesh
// Create constants, dependent on mSize basically
CEVector3f const mSize(128, 128, 128);
CEVector3f const b_max = CEVector3f(mSize.x / 2.0f,  mSize.y / 2.0f,  mSize.z / 2.0f);
CEVector3f const b_min = CEVector3f(-mSize.x / 2.0f, -mSize.y / 2.0f, -mSize.z / 2.0f);

CEVector3f const scale = CEVector3f(
1.0f / (b_max.x - b_min.x),
1.0f / (b_max.y - b_min.y),
1.0f / (b_max.z - b_min.z)
);

CEVector3f const m_VVoxel_Bias = - b_min;

// convert to a uniform scale so the voxels stay cubes
float const m_FVoxel_Scale = std::min(scale.x, std::min(scale.y, scale.z));

// Base Units
D3DXMATRIX t, s, s2, t2;
D3DXMatrixTranslation(&s, -1.0f, -1.0f, -1.0f);
D3DXMatrixScaling    (&t, 2.0f, 2.0f, 2.0f);
D3DXMatrixScaling    (&s2, m_FVoxel_Scale, m_FVoxel_Scale, m_FVoxel_Scale);
D3DXMatrixTranslation(&t2, m_VVoxel_Bias.x, m_VVoxel_Bias.y, m_VVoxel_Bias.z);

D3DXMATRIX m_MVoxel_WorldUnit =  t * s * s2 * t2;

// Generate view matrix
D3DXMATRIX unitCubeToNDC;
CEVector3f CubeLookatDir = GetCubeMapLookat(TEXTURE_CUBE_MAP_POSITIVE_X + n);
D3DXMatrixLookAtLH(&unitCubeToNDC, &CEVector3f(0, 0, 0), &CubeLookatDir, &CEVector3f(0, 1, 0));

D3DXMatrixTranspose(&unitCubeToNDC, &unitCubeToNDC);
D3DXMatrixTranspose(&m_MVoxel_WorldUnit, &m_MVoxel_WorldUnit);

// m_MVoxel_WorldUnit messes this up somehow
D3DXMATRIX MVPMatrix = m_MVoxel_WorldUnit * unitCubeToNDC;

// Create the storage matrix (helper in 3d texture)
D3DXMATRIX storageTransform;
D3DXMatrixTranspose(&storageTransform, &unitCubeToNDC);

// Send the needed data, mesh wise
m_vVoxelisers[i]->src->SendBuffers(devcon, dev, Camera, ObjectBuffer, Bufferphase);

// Output to the staging texture
MVPMatrix, storageTransform,
&VoxelMap[n],
false, 0);
}
}

bff_PrObject.worldMatrix = world;

bff_PrObject.gi_storageTransform = gi_storage;
bff_PrObject.gi_VPMatrix = gi_VP; // in the other piece of code it's MVP, but it's the same matrix

devcon->UpdateSubresource(pCBuffer[0], 0, 0, &bff_PrObject, NULL, NULL);

devcon->VSSetConstantBuffers(0, 2, pCBuffer);
devcon->PSSetConstantBuffers(0, 2, pCBuffer);

// Set the output texture
devcon->OMSetRenderTargetsAndUnorderedAccessViews(0, NULL, NULL, 1, 1, &t3D->pTexture, 0);

if (!instanced)
devcon->DrawIndexed(bf.IndexCount, 0, 0);
else
devcon->DrawIndexedInstanced(bf.IndexCount, instanceCount, 0, 0, 0);

(The shader it self is laughable, basing the color on y pos, but right now that's because I just want a simple and quick way to generate a result.)

cbuffer ConstantObjectBuffer : register (b0)
{
matrix worldMatrix;

matrix gi_vp;
matrix gi_storage;
};

cbuffer ConstantFrameBuffer : register(b1)
{
matrix viewMatrix;
matrix projectionMatrix;

matrix lightViewMatrix;
matrix lightProjectionMatrix;
float3 sLightPos;

float3 eyepos;

float4 lightvec;
float4 lightcol;

float FogStart;
float FogEnd;
float2 __space;

float3 FogColor;

float SpecularIntensity;
float4 SpecularColor;

float3 viewvec;
float _dpmSetting;

matrix worldToVoxel;
float minVoxelDiameter;
float minVoxelDiameterInv;
};

struct VOut
{
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD;
float3 lNormal : LNORM;

float yPos : UOS;
};

// First RT has been taken
RWTexture3D<float> voxelTex : register(u1);

SamplerState ss;

VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD, float3 instancePosition : INSTANCEPOS)
{
VOut output;

/*if (_instance == 1) Not yet implemented
{
position.x += instancePosition.x;
position.y += instancePosition.y;
position.z += instancePosition.z;
}*/

position.w = 1.0f;
output.texcoord = texcoord;

output.yPos = position.y;

output.position = mul(position, gi_vp);
//output.position = mul(output.position, projectionMatrix);

float diffusebrightness = saturate(dot(mul(worldMatrix, normal), lightvec));
output.lNormal = lightcol * diffusebrightness;

return output;
}

{
float4 col = float4(1, 1, 1, 1);
col.rgb = input.lNormal;

if (input.yPos > 0.1f)
{
col.rgb *= float3(0.3, 1.0f, 0.3f);
}

float4 storagePos = mul(input.position, gi_storage);
storagePos.xyz *= 1.0 / storagePos.w;

float3 voxelPos = float3(float3(128, 128, 128) * (storagePos.xyz * .5 + .5));

col.xyz *= .5;
voxelTex[voxelPos] = col;

return col;
}

I know, this is a bunch of code that I just threw upon you, sorry about that, but it may be useful.

I do also have a pix session right here: (Around 200 MB, the voxelization starts at EID/Call 245, frame 112)

Anyway, thanks for taking your time to get to the bottom!

Thanks, as usual.

-MIGI0027

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #2gboxentertainment  Members

Posted 27 September 2013 - 06:35 AM

I've never used pix before so I don't know how I would be able to help you with that.

Are you just getting a black screen?

Have you tried testing your shader to see if you can output just anything? - like "col.xyz = float3(1,1,1);"

Edited by gboxentertainment, 27 September 2013 - 06:37 AM.

### #3Migi0027 (肉コーダ)  Members

Posted 27 September 2013 - 09:37 AM

Hi gbox.

I've set up a simple experiment using UAVs, and it proved that I could successfully write to the uav used by the voxelizer at the current axis (Cube map).

So I basically set pixel 68,68, 3(depth) to white, and when debugging with pix, that specific pixel in depth slice 3 was white in all 3D Textures.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #4Migi0027 (肉コーダ)  Members

Posted 01 October 2013 - 01:07 PM

Just one quick question, I've successfully been able to render the Debug Grid, generating the cubes on the GPU, with an ok fps (with vct, ~20fps 128*128*128).

But do I actually need to render 128x128x128 cubes, can't I render half of that, as I don't need perfect debug info, what have other people chosen?

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #5gboxentertainment  Members

Posted 02 October 2013 - 03:00 AM

I use 64x64x64. For everything other than reflections, it seems to be the optimum - although it also depends on the size of your scene.

My scene is probably a double-scaled cornell box scene.

Edited by gboxentertainment, 02 October 2013 - 03:01 AM.

### #6Styves  Members

Posted 02 October 2013 - 05:44 AM

If you want to debug the voxel data, you can try using an append buffer. Go over all the voxels in your scene and append a vertex to the buffer at that voxel position, with all the necessary info (also skipping all voxels that have nothing in them). Then run a shader using it as a vertex buffer and expand cubes using the geometry shader.

### #7Migi0027 (肉コーダ)  Members

Posted 02 October 2013 - 08:30 AM

If you want to debug the voxel data, you can try using an append buffer. Go over all the voxels in your scene and append a vertex to the buffer at that voxel position, with all the necessary info (also skipping all voxels that have nothing in them). Then run a shader using it as a vertex buffer and expand cubes using the geometry shader.

That's exactly what I'm doing

But thanks anyways!

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #8Migi0027 (肉コーダ)  Members

Posted 11 October 2013 - 11:04 AM

Hmm, could it be because of how OpenGL handles matrices and how Direct3D does?

I'll try reversing the multiplication order. (matrix * vector instead of vector * matrix).

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #9Migi0027 (肉コーダ)  Members

Posted 11 October 2013 - 02:18 PM

Nope, didn't work.

Sorry to bug you, but does anyone have an idea? I'm kind of stuck.

-MIGI0027

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #10gboxentertainment  Members

Posted 11 October 2013 - 09:44 PM

So are you able to display anything now? What do you see - are all the voxels in your debug grid black/white?

In terms of matrices, OpenGL uses column-major - so maybe you could try transposing your matrices.

### #11Migi0027 (肉コーダ)  Members

Posted 12 October 2013 - 01:07 AM

When debugging over all of the textures, only some few depth slices actually have been written to, but it's usually just one pixel, which doesn't give any sense.

Gbox, did you implement your own implementation or did you follow this? : http://www.geeks3d.com/20121214/voxel-cone-tracing-global-illumination-in-opengl-4-3/

If the 2nd, can you share what matrices do you transpose?

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #12gboxentertainment  Members

Posted 12 October 2013 - 01:52 AM

vec4 storagePos = transpose(gsout.oProj)*gsout.ndcPos;
storagePos.xyz *= 1.0/storagePos.w;

Btw, how are you actually doing your voxelization? I'm using the geometry shader with the dominant-axis method. i.e:

#version 430

layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;

in VSOutput
{
vec4 ndcPos;
vec4 fNorm;
vec2 fTexCoord;
vec4 worldPos;
} vsout[];

out GSOutput
{
vec4 ndcPos;
vec4 fNorm;
vec2 fTexCoord;
vec4 worldPos;
mat4 oProj;
float density;
} gsout;

uniform mat4 voxSpace;
uniform mat4 invVoxSpace;

void main()
{
gsout.oProj = mat4(0);
vec4 n = normalize(vsout[0].fNorm);

for(int i = 0; i<gl_in.length(); i++)
{
vec4 voxPos = invVoxSpace*vsout[i].ndcPos;
gsout.fNorm = n;

float maxC = max(abs(n.x), max(abs(n.y), abs(n.z)));
float x,y,z;
x = abs(n.x) < maxC ? 0 : 1;
y = abs(n.y) < maxC ? 0 : 1;
z = abs(n.z) < maxC ? 0 : 1;

vec4 axis = vec4(x,y,z,1);

if(axis == vec4(1,0,0,1))
{
gsout.oProj[0] = vec4(0,0,-1,0);
gsout.oProj[1] = vec4(0,-1,0,0);
gsout.oProj[2] = vec4(-1,0,0,0);
gsout.oProj[3] = vec4(0,0,0,1);
gsout.density = (voxPos.x - floor(voxPos.x));
}
else if(axis == vec4(0,1,0,1))
{
gsout.oProj[0] = vec4(1,0,0,0);
gsout.oProj[1] = vec4(0,0,1,0);
gsout.oProj[2] = vec4(0,-1,0,0);
gsout.oProj[3] = vec4(0,0,0,1);
gsout.density = (voxPos.y - floor(voxPos.y));
}
else if(axis == vec4(0,0,1,1))
{
gsout.oProj[0] = vec4(1,0,0,0);
gsout.oProj[1] = vec4(0,-1,0,0);
gsout.oProj[2] = vec4(0,0,-1,0);
gsout.oProj[3] = vec4(0,0,0,1);
gsout.density = (voxPos.z - floor(voxPos.z));
}

gl_Position = gsout.oProj*voxSpace*gl_in[i].gl_Position;
gsout.ndcPos = gsout.oProj*voxSpace*vsout[i].ndcPos;
gsout.fTexCoord = vsout[i].fTexCoord;
gsout.worldPos = vsout[i].worldPos;