• 13
• 18
• 19
• 27
• 10
• ### Similar Content

• By KarimIO
Hey guys! Three questions about uniform buffers:
1) Is there a benefit to Vulkan and DirectX's Shader State for the Constant/Uniform Buffer? In these APIs, and NOT in OpenGL, you must set which shader is going to take each buffer. Why is this? For allowing more slots?
2) I'm building an wrapper over these graphics APIs, and was wondering how to handle passing parameters. In addition, I used my own json format to describe material formats and shader formats. In this, I can describe which shaders get what uniform buffers. I was thinking of moving to support ShaderLab (Unity's shader format) instead, as this would allow people to jump over easily enough and ease up the learning curve. But ShaderLab does not support multiple Uniform Buffers at all, as I can tell, let alone what parameters go where.
So to fix this, I was just going to send all Uniform Buffers to all shaders. Is this that big of a problem?
3) Do you have any references on how to organize material uniform buffers? I may be optimizing too early, but I've seen people say what a toll this can take.

• Hi, I'm new here and would like to get some help in what i should do first when designing a game? What would you consider to be the best steps to begin designing my game? Give resources with it as well please.

• How do you handle situations where the physics engine produces a 129x129 height field which results in 2M vertexes for every frame. The frame rate suffers a lot, and drops to 2-3 fps.
Is there a way to reduce the traffic or burden over the graphics card? Is the technique of LOD and progressive mesh still apply?
thanks
Jack
• By stale
I've just started learning about tessellation from Frank Luna's DX11 book. I'm getting some very weird behavior when I try to render a tessellated quad patch if I also render a mesh in the same frame. The tessellated quad patch renders just fine if it's the only thing I'm rendering. This is pictured below:
'
However, when I attempt to render the same tessellated quad patch along with the other entities in the scene (which are simple triangle-lists), I get the following error:

I have no idea why this is happening, and google searches have given me no leads at all. I use the following code to render the tessellated quad patch:
for (unsigned int i = 0; i < scene->GetEntityList()->size(); i++) { Entity* entity = scene->GetEntityList()->at(i); if (entity->m_VisualComponent->m_visualType == VisualType::MESH) DrawMeshEntity(entity, cam, sun, point); else if (entity->m_VisualComponent->m_visualType == VisualType::BILLBOARD) DrawBillboardEntity(entity, cam, sun, point); else if (entity->m_VisualComponent->m_visualType == VisualType::TERRAIN) DrawTerrainEntity(entity, cam); } HR(m_swapChain->Present(0, 0)); Any help/advice would be much appreciated!

• I have just started writing shaders recently (a couple of weeks ago.) (Background info in another post here.) I decided to start with a basic phong shader. I understand (I think) the implementation of the diffuse and specular refleections.
Diffuse reflection = (N.L)
Specular reflection = (R.V)^S   where s = specular power.
Reflection vector = 2* (N.L)*N-L
I have a shader put together for fx composer 2.5 that is an attempt at the phong lighting model. (Shader code linked.) I have not added in the texture sample to the pixel shader output yet since I am trying to debug lighting model issues. The diffuse reflection works fine, however, the specular reflection is giving me some trouble.
The problem I am experience is that the specular reflection (shown here) seems to affect the back of the sphere.

When I saturate the specular reflection value I get this: (If saturate clamps input to min=0 and max=1, I do not know where these values on the back are coming from since negative values should just get clamped to 0.)

If I remove the saturate from the specular reflection and add the difuse reflection to the specular reflection I get this.  (I will post an image of my diffuse reflection below to show how it looks on its own. )

Diffuse reflection by itself.

What am I missing here?

Also, I am completely open to feedback on my shader. I am looking to learn anything I can here.  Thank you in advance for your time.

## Recommended Posts

Hiya again guys,

I have a niggling problem with my CSM. The middle an far cascades noticeably "swim" as you move around. It's not really noticeable on the close one but i'm that's probably just due to the filtering and the factor it has such a high quality map to work from.

Anyway I have 3 cascades and i'm using a bounding box for each. They all overlap with they near clip and this works fairly nicely but I was looking at the csm sample that some nicely ported over to monogame. The problem is I can't get the sphere based bounding boxes working at all for me. My debug rendertargets for all the cascades are just blank. If I switch my camera to be the shadow light camera (the sun) it seems fine (and it works using my bounding box method so i think that's fine). I THINK i'm just calculating the bounding box (using a sphere) incorrectly some how.

public void GenerateCSMOrthoSlice(float pfarClip)
{
Vector3[] frustumCornersWS = new Vector3[8];
Vector3[] frustumCornersLS = new Vector3[8];
BoundingFrustum viewFrustum = new BoundingFrustum(_Camera.CameraView * Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _Camera._aspectRatio, 10, pfarClip));
frustumCornersWS = viewFrustum.GetCorners();

Vector3 frustumCentroid = new Vector3(0, 0, 0);
for (int i = 0; i < 8; i++)
frustumCentroid += frustumCornersWS;
frustumCentroid /= 8;

lightsView = Matrix.Identity;
lightsViewProjectionMatrix = Matrix.Identity;

ShadowLightPos = frustumCentroid + (SunlightDirection * 100);

Vector3 mins = frustumCornersLS[0];
Vector3 maxes = frustumCornersLS[0];
for (int i = 0; i < 8; i++)
{
if (frustumCornersLS.X > maxes.X)
maxes.X = frustumCornersLS.X;
else if (frustumCornersLS.X < mins.X)
mins.X = frustumCornersLS.X;
if (frustumCornersLS.Y > maxes.Y)
maxes.Y = frustumCornersLS.Y;
else if (frustumCornersLS.Y < mins.Y)
mins.Y = frustumCornersLS.Y;
if (frustumCornersLS.Z > maxes.Z)
maxes.Z = frustumCornersLS.Z;
else if (frustumCornersLS.Z < mins.Z)
mins.Z = frustumCornersLS.Z;
}

float diagonalLength = (frustumCornersWS[0] - frustumCornersWS[6]).Length();
diagonalLength += 2;    //Without this, the shadow map isn't big enough in the world.
float worldsUnitsPerTexel = diagonalLength / (float)4096;

Vector3 vBorderOffset = (new Vector3(diagonalLength, diagonalLength, diagonalLength) - (maxes - mins)) * 0.5f;
maxes += vBorderOffset;
mins -= vBorderOffset;

mins /= worldsUnitsPerTexel;
mins.X = (float)Math.Floor(mins.X);
mins.Y = (float)Math.Floor(mins.Y);
mins.Z = (float)Math.Floor(mins.Z);
mins *= worldsUnitsPerTexel;

maxes /= worldsUnitsPerTexel;
maxes.X = (float)Math.Floor(maxes.X);
maxes.Y = (float)Math.Floor(maxes.Y);
maxes.Z = (float)Math.Floor(maxes.Z);
maxes *= worldsUnitsPerTexel;

ShadowLightProjection = Matrix.CreateOrthographicOffCenter(mins.X, maxes.X, mins.Y, maxes.Y, -maxes.Z - 500f, -mins.Z);
}

What I tried (which doesn't work at all) is:

public void GenerateCSMOrthoSlice(float pfarClip)
{

Vector3 minExtents = Vector3.Zero;
Vector3 maxExtents = Vector3.Zero;
Vector3[] frustumCornersWS = new Vector3[8];
Vector3[] frustumCornersLS = new Vector3[8];
BoundingFrustum viewFrustum = new BoundingFrustum(_Camera.CameraView * Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _Camera._aspectRatio, 10, pfarClip));
frustumCornersWS = viewFrustum.GetCorners();

Vector3 frustumCentroid = new Vector3(0, 0, 0);
for (int i = 0; i < 8; i++)
frustumCentroid += frustumCornersWS;
frustumCentroid /= 8;

{

// This needs to be constant for it to be stable
var upDir = Vector3.Up;

// Calculate the radius of a bounding sphere surrounding the frustum corners
for (var i = 0; i < 8; ++i)
{
var dist = (_frustumCorners - frustumCentroid).Length();
}

minExtents = -maxExtents;
}

lightsView = Matrix.Identity;
lightsViewProjectionMatrix = Matrix.Identity;

//Vector3 sunlightdirection = new Vector3(0.21f, 0.11f, -0.5f);
ShadowLightPos = frustumCentroid + (SunlightDirection * 100);

Vector3 mins = frustumCornersLS[0];
Vector3 maxes = frustumCornersLS[0];
for (int i = 0; i < 8; i++)
{
if (frustumCornersLS.X > maxes.X)
maxes.X = frustumCornersLS.X;
else if (frustumCornersLS.X < mins.X)
mins.X = frustumCornersLS.X;
if (frustumCornersLS.Y > maxes.Y)
maxes.Y = frustumCornersLS.Y;
else if (frustumCornersLS.Y < mins.Y)
mins.Y = frustumCornersLS.Y;
if (frustumCornersLS.Z > maxes.Z)
maxes.Z = frustumCornersLS.Z;
else if (frustumCornersLS.Z < mins.Z)
mins.Z = frustumCornersLS.Z;
}

float diagonalLength = (frustumCornersWS[0] - frustumCornersWS[6]).Length();
diagonalLength += 2;    //Without this, the shadow map isn't big enough in the world.
float worldsUnitsPerTexel = diagonalLength / (float)4096;

Vector3 vBorderOffset = (new Vector3(diagonalLength, diagonalLength, diagonalLength) - (maxes - mins)) * 0.5f;
maxes += vBorderOffset;
mins -= vBorderOffset;

mins /= worldsUnitsPerTexel;
mins.X = (float)Math.Floor(mins.X);
mins.Y = (float)Math.Floor(mins.Y);
mins.Z = (float)Math.Floor(mins.Z);
mins *= worldsUnitsPerTexel;

maxes /= worldsUnitsPerTexel;
maxes.X = (float)Math.Floor(maxes.X);
maxes.Y = (float)Math.Floor(maxes.Y);
maxes.Z = (float)Math.Floor(maxes.Z);
maxes *= worldsUnitsPerTexel;

ShadowLightProjection = Matrix.CreateOrthographicOffCenter(mins.X, maxes.X, mins.Y, maxes.Y, -maxes.Z - 500f, -mins.Z);

{
ShadowLightProjection = Matrix.CreateOrthographicOffCenter(minExtents.X, minExtents.Y, maxExtents.X, maxExtents.Y, 0.0f, pfarClip);

// Create the rounding matrix, by projecting the world-space origin and determining
// the fractional offset in texel space
var shadowOrigin = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);

var roundOffset = roundedOrigin - shadowOrigin;
roundOffset = roundOffset * (2.0f / 4096);
roundOffset.Z = 0.0f;
roundOffset.W = 0.0f;

}

//
}

##### Share on other sites

I'm no MonoGame expert, but looking at your code it looks like you mixed up the parameters to Matrix.CreateOrthographicOffCenter() in the stabilized version.

##### Share on other sites

Thanks, yes. Well spotted. Unfortunately that had no effect.

Shimmery shadows have been bugging me for ages but I can't seem to figure out how to use the sphere method of making my cascades

##### Share on other sites

If anyone could give  me some code/pseudo code for finding the bounding sphere of my shadow camera that would do. I was reading shaderx7 on this subject. Which was interedting but the math is over my head. I don't mind the wasted space. By the time my game is finished games will be able to support one giant texture lol

Thanks

##### Share on other sites

Here is my code that is used in production (So I know it works ;-) )

	V4 vMin = vCorners[0];
V4 vMax = vCorners[0];
for(int j = 1; j<8; j++) {
vMin = VMin(vMin, vCorners[j]);
vMax = VMax(vMax, vCorners[j]);
}
V4 vSize = vMax - vMin;
float fRadius = VLen3(vSize).x() * 0.5f;

// Snap center to shadow texel
// This is done by transforming center of CSM frustum into light post projection (texel space) and
// perform snapping in this space.
M44 mCameraToLight = MInverseAffine(mLightToCamera);
V4 vCenterCamera = VLerp(vMin, vMax, VSplat(0.5f));
V4 vCenterLight = VTransform43(mCameraToLight, vCenterCamera);
vCenterTexel = VProject(vCenterTexel);
vSnap = VFloor(vSnap);
V4 vSnapOffset = VLoadZero() - vSnap;
mSnapTrans.vTrans = VXY01(vSnapOffset);


Hope it makes sense.

BTW: Looking at your code I think the issue is the way you apply the offset directly to the matrix instead of doing a multiplication with a translation matrix (Like I do in the last line)

Henning

Oh, and I now use the quantization trick instead of the code above to get more precision (http://dev.theomader.com/stable-csm/)

Edited by semler

##### Share on other sites

Thanks, your help is much appreciated. I'm not familiar with that language but i'll see if I can work it out (I'm just a c# xna/monogame guy)

##### Share on other sites

This ia  video of the problem. I'm not if i'm barking up the wrong tree. But shadows are very "jiggly" on some objects. I wanted to try the sphere based approach. Which from my understanding is making a bounding sphere from the shadow camera view frustrum then creating my usual orthographic projection for my cascade from that. Am I undestanding it correctly?

Here's a video of the annoying problem I have. Shortly into the video i turn on the rendertarget display so you can see the 3 cascade render targets on the right. As you can see theres a lot of jiggle going on. Depth Bias doesn't seem to make any difference really.

##### Share on other sites

I'm a complete noob here but i see that you rotate your shadow maps with camera orientation and this way it's impossible to avoid jittering. I think you have to keep the shadow map orientation constant in worldspace and only adapt it to the position of the camera ignoring player view direction. So when you rotate but don't move, you would see the corners of cascade transitions fixed to the floor.  Does this make sense?

##### Share on other sites

I think you're probably correct/ That's what I was referring to with using a sphere from the camera frustrum to build the bounding box for my orthographic projection for the shadow like.

I think i'm missing something though. I've found some examples here and there but I'm just not getting it. Maybe some pseudo code would help me.

Edited by skyemaidstone

##### Share on other sites

Semlers linked blog contains more articles and code, however...

Looking at code snippets often raises more questions than answers: What conventions are used here (is this a camera to world matrix or world to camera? what multiplication order do they use? etc. etc.) - mostly you have to care for the deatils yourself and after you undrestood the concept it's only the details that's left.

I recommend you simplyfy the problem instead so cou can solve it more easily in your own:

1. Ignore frustum and any bounds and make each cascade just centered to the texel closest to the camera. (visualizing texel grid on a simple ground plane should help to set this up. And if this works all math is known.)

2. If you get this to work robustly so the cascades move with the camera without jitter the main problem is solved and you can start to optimize it for the frustum, which shouldn't be that hard at this point.