浏览代码

support both left and right hand camera space in shaderland (think we'll match cpp side by default)

support both left and right hand camera space in shaderland (think we'll
match cpp side by default)
/main
mmikk 8 年前
当前提交
abff6e01
共有 11 个文件被更改,包括 62 次插入27 次删除
  1. 4
      Assets/ScriptableRenderLoop/fptl/ClusteredUtils.h
  2. 36
      Assets/ScriptableRenderLoop/fptl/FptlLighting.cs
  3. 4
      Assets/ScriptableRenderLoop/fptl/LightBoundsDebug.shader
  4. 3
      Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs
  5. 4
      Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs.hlsl
  6. 4
      Assets/ScriptableRenderLoop/fptl/LightingConvexHullUtils.hlsl
  7. 4
      Assets/ScriptableRenderLoop/fptl/LightingUtils.hlsl
  8. 8
      Assets/ScriptableRenderLoop/fptl/lightlistbuild-bigtile.compute
  9. 12
      Assets/ScriptableRenderLoop/fptl/lightlistbuild-clustered.compute
  10. 4
      Assets/ScriptableRenderLoop/fptl/lightlistbuild.compute
  11. 6
      Assets/ScriptableRenderLoop/fptl/scrbound.compute

4
Assets/ScriptableRenderLoop/fptl/ClusteredUtils.h


int SnapToClusterIdxFlex(float z_in, float suggestedBase, bool logBasePerTile)
{
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float z = z_in;
#else
float z = -z_in;

float dist = (pow(suggestedBase, (float)k) - 1.0) / (userscale * (suggestedBase - 1.0f));
res = dist + g_fNearPlane;
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
return res;
#else
return -res;

36
Assets/ScriptableRenderLoop/fptl/FptlLighting.cs


cmd.SetComputeFloatParams(shadercs, name, data);
}
static Matrix4x4 GetFlipMatrix()
{
Matrix4x4 flip = Matrix4x4.identity;
bool isLeftHand = ((int) LightDefinitions.USE_LEFTHAND_CAMERASPACE)!=0;
if(isLeftHand) flip.SetColumn(2, new Vector4(0.0f, 0.0f, -1.0f, 0.0f));
return flip;
}
static Matrix4x4 WorldToCamera(Camera camera)
{
return GetFlipMatrix() * camera.worldToCameraMatrix;
}
static Matrix4x4 CameraToWorld(Camera camera)
{
return camera.cameraToWorldMatrix * GetFlipMatrix();
}
static Matrix4x4 CameraProjection(Camera camera)
{
return camera.projectionMatrix * GetFlipMatrix();
}
var worldToView = camera.worldToCameraMatrix;
var worldToView = WorldToCamera(camera);
for (int nLight = 0; nLight < visibleLights.Count; nLight++)
{

var lightData = new SFiniteLightData[numVolumes];
var boundData = new SFiniteLightBound[numVolumes];
var worldToView = camera.worldToCameraMatrix;
var worldToView = WorldToCamera(camera);
bool isNegDeterminant = Vector3.Dot(worldToView.GetColumn(0), Vector3.Cross(worldToView.GetColumn(1), worldToView.GetColumn(2)))<0.0f; // 3x3 Determinant.
uint shadowLightIndex = 0;
foreach (var cl in inputs.visibleLights)
{

bound.center = worldToView.MultiplyPoint(lightPos);
bound.boxAxisX.Set(range, 0, 0);
bound.boxAxisY.Set(0, range, 0);
bound.boxAxisZ.Set(0, 0, -range); // transform to camera space (becomes a left hand coordinate frame in Unity since Determinant(worldToView)<0)
bound.boxAxisZ.Set(0, 0, isNegDeterminant ? (-range) : range); // transform to camera space (becomes a left hand coordinate frame in Unity since Determinant(worldToView)<0)
bound.scaleXY.Set(1.0f, 1.0f);
bound.radius = range;

//RenderLighting(camera, inputs, loop);
//
var proj = camera.projectionMatrix;
var proj = CameraProjection(camera);
var temp = new Matrix4x4();
temp.SetRow(0, new Vector4(1.0f, 0.0f, 0.0f, 0.0f));
temp.SetRow(1, new Vector4(0.0f, 1.0f, 0.0f, 0.0f));

var numDirLights = UpdateDirectionalLights(camera, cullResults.visibleLights);
// Push all global params
PushGlobalParams(camera, loop, camera.cameraToWorldMatrix, projscr, invProjscr, numDirLights);
PushGlobalParams(camera, loop, CameraToWorld(camera), projscr, invProjscr, numDirLights);
// do deferred lighting
DoTiledDeferredLighting(camera, loop);

4
Assets/ScriptableRenderLoop/fptl/LightBoundsDebug.shader


coord.xy *= (coordIndex >= 4) ? lightData.scaleXY : float2(1, 1);
float3 viewPos = lightData.center + coord.x * lightData.boxAxisX.xyz + coord.y * lightData.boxAxisY.xyz + coord.z * -lightData.boxAxisZ.xyz;
#if USE_LEFTHAND_CAMERASPACE
// not completely sure why this is necessary since the old stuff pretends camera coordinate system is also left-hand.
viewPos.z = -viewPos.z;
#endif
return UnityViewToClipPos(viewPos);
}

3
Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs


public static int MAX_NR_BIGTILE_LIGHTS_PLUSONE = 512; // may be overkill but the footprint is 2 bits per pixel using uint16.
public static float VIEWPORT_SCALE_Z = 1.0f;
// enable unity's original left-hand shader camera space (right-hand internally in unity).
public static int USE_LEFTHAND_CAMERASPACE = 0;
// flags
public static int IS_CIRCULAR_SPOT_SHAPE = 1;
public static int HAS_COOKIE_TEXTURE = 2;

4
Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs.hlsl


//
// LightDefinitions: static fields
//
#define USE_LEFTHAND_CAMERASPACE (0)
#define MAX_NR_LIGHTS_PER_CAMERA (1024)
#define MAX_NR_BIGTILE_LIGHTS_PLUSONE (512)
#define VIEWPORT_SCALE_Z (1)

4
Assets/ScriptableRenderLoop/fptl/LightingConvexHullUtils.hlsl


p0 = center + (vA + vB - vC); // center + vA is center of face when scaleXY is 1.0
float3 vNout = cross( vB2, 0.5*(vA-vA2) - vC );
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
vNout = -vNout;
#endif

#endif
// enlarge sphere so it overlaps the center of the tile assuming it overlaps the tile to begin with.
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float sphRadius = sphRadiusIn + (sphCen.z+offs)*halfTileSizeAtZDistOne;
#else
float sphRadius = sphRadiusIn - (sphCen.z-offs)*halfTileSizeAtZDistOne;

4
Assets/ScriptableRenderLoop/fptl/LightingUtils.hlsl


//float fCy = g_mScrProjection[2].y;
float fCy = g_mScrProjection[1].z;
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
return fLinDepth*float3( ((v2ScrPos.x-fCx)/fSx), ((v2ScrPos.y-fCy)/fSy), 1.0 );
#else
return fLinDepth*float3( -((v2ScrPos.x+fCx)/fSx), -((v2ScrPos.y+fCy)/fSy), 1.0 );

float GetLinearZFromSVPosW(float posW)
{
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float linZ = posW;
#else
float linZ = -posW;

8
Assets/ScriptableRenderLoop/fptl/lightlistbuild-bigtile.compute


float fSy = g_mScrProjection[1].y;
float fCy = g_mScrProjection[1].z;
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
return fLinDepth*float3( ((v2ScrPos.x-fCx)/fSx), ((v2ScrPos.y-fCy)/fSy), 1.0 );
#else
return fLinDepth*float3( -((v2ScrPos.x+fCx)/fSx), -((v2ScrPos.y+fCy)/fSy), 1.0 );

#ifdef PERFORM_SPHERICAL_INTERSECTION_TESTS
void SphericalIntersectionTests(uint threadID, int iNrCoarseLights, float2 screenCoordinate)
{
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float3 V = GetViewPosFromLinDepth( screenCoordinate, 1.0);
#else
float3 V = GetViewPosFromLinDepth( screenCoordinate, -1.0);

float x = (i&1)==0 ? viTilLL.x : viTilUR.x;
float y = (i&2)==0 ? viTilLL.y : viTilUR.y;
float z = (i&4)==0 ? g_fNearPlane : fTileFarPlane;
#ifndef LEFT_HAND_COORDINATES
#if !USE_LEFTHAND_CAMERASPACE
z = -z;
#endif
return GetViewPosFromLinDepth( float2(x, y), z);

const float3 boxX = lgtDat.boxAxisX.xyz;
const float3 boxY = lgtDat.boxAxisY.xyz;
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip an axis to make it right handed since Determinant(worldToView)<0
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip axis (so it points away from the light direction for a spot-light)
const float3 center = lgtDat.center.xyz;
const float2 scaleXY = lgtDat.scaleXY;

12
Assets/ScriptableRenderLoop/fptl/lightlistbuild-clustered.compute


float fSy = g_mScrProjection[1].y;
float fCy = g_mScrProjection[1].z;
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
return fLinDepth*float3( ((v2ScrPos.x-fCx)/fSx), ((v2ScrPos.y-fCy)/fSy), 1.0 );
#else
return fLinDepth*float3( -((v2ScrPos.x+fCx)/fSx), -((v2ScrPos.y+fCy)/fSy), 1.0 );

#ifdef ENABLE_DEPTH_TEXTURE_BACKPLANE
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float fTileFarPlane = GetLinearDepth(dpt_ma);
#else
float fTileFarPlane = -GetLinearDepth(dpt_ma);

const float3 boxX = lgtDat.boxAxisX.xyz;
const float3 boxY = lgtDat.boxAxisY.xyz;
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip an axis to make it right handed since Determinant(worldToView)<0
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip axis (so it points away from the light direction for a spot-light)
const float3 center = lgtDat.center.xyz;
const float radius = lgtDat.radius;
const float2 scaleXY = lgtDat.scaleXY;

#ifdef PERFORM_SPHERICAL_INTERSECTION_TESTS
int SphericalIntersectionTests(uint threadID, int iNrCoarseLights, float2 screenCoordinate)
{
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float3 V = GetViewPosFromLinDepth( screenCoordinate, 1.0);
#else
float3 V = GetViewPosFromLinDepth( screenCoordinate, -1.0);

float x = (i&1)==0 ? viTilLL.x : viTilUR.x;
float y = (i&2)==0 ? viTilLL.y : viTilUR.y;
float z = (i&4)==0 ? g_fNearPlane : fTileFarPlane;
#ifndef LEFT_HAND_COORDINATES
#if !USE_LEFTHAND_CAMERASPACE
z = -z;
#endif
return GetViewPosFromLinDepth( float2(x, y), z);

const float3 boxX = lgtDat.boxAxisX.xyz;
const float3 boxY = lgtDat.boxAxisY.xyz;
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip an axis to make it right handed since Determinant(worldToView)<0
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip axis (so it points away from the light direction for a spot-light)
const float3 center = lgtDat.center.xyz;
const float2 scaleXY = lgtDat.scaleXY;

4
Assets/ScriptableRenderLoop/fptl/lightlistbuild.compute


float fSy = g_mScrProjection[1].y;
float fCy = g_mScrProjection[1].z;
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
return fLinDepth*float3( ((v2ScrPos.x-fCx)/fSx), ((v2ScrPos.y-fCy)/fSy), 1.0 );
#else
return fLinDepth*float3( -((v2ScrPos.x+fCx)/fSx), -((v2ScrPos.y+fCy)/fSy), 1.0 );

GroupMemoryBarrierWithGroupSync();
#endif
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
float3 V = GetViewPosFromLinDepth( screenCoordinate, 1.0);
#else
float3 V = GetViewPosFromLinDepth( screenCoordinate, -1.0);

6
Assets/ScriptableRenderLoop/fptl/scrbound.compute


const float3 boxX = lgtDat.boxAxisX.xyz;
const float3 boxY = lgtDat.boxAxisY.xyz;
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip an axis to make it right handed since Determinant(worldToView)<0
const float3 boxZ = -lgtDat.boxAxisZ.xyz; // flip axis (so it points away from the light direction for a spot-light)
const float3 center = lgtDat.center.xyz;
const float radius = lgtDat.radius;
const float2 scaleXY = lgtDat.scaleXY;

vMax.xy = bMa ? min(vMax.xy, vMa) : vMax.xy;
}
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
if((center.z-radius)>0.0)
{
float4 vPosF = mul(g_mProjection, float4(0,0,center.z-radius,1));

float4 planeY = EvalPlanePair(float2(pos_view_space.y, pos_view_space.z), r);
#ifdef LEFT_HAND_COORDINATES
#if USE_LEFTHAND_CAMERASPACE
planeX = planeX.zwxy; // need to swap left/right and top/bottom planes when using left hand system
planeY = planeY.zwxy;
#endif

正在加载...
取消
保存