2012-01-18 11:47:31 +00:00
|
|
|
#ifndef VS_TME
|
|
|
|
#define VS_TME 1
|
|
|
|
#define VS_FST 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GS_IIP
|
|
|
|
#define GS_IIP 0
|
2012-01-27 11:56:49 +00:00
|
|
|
#define GS_PRIM 2
|
2012-01-18 11:47:31 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
#ifndef PS_BATCH_SIZE
|
|
|
|
#define PS_BATCH_SIZE 2048
|
|
|
|
#define PS_FPSM PSM_PSMCT32
|
|
|
|
#define PS_ZPSM PSM_PSMZ16
|
|
|
|
#endif
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
#define PSM_PSMCT32 0
|
|
|
|
#define PSM_PSMCT24 1
|
|
|
|
#define PSM_PSMCT16 2
|
|
|
|
#define PSM_PSMCT16S 10
|
|
|
|
#define PSM_PSMT8 19
|
|
|
|
#define PSM_PSMT4 20
|
|
|
|
#define PSM_PSMT8H 27
|
|
|
|
#define PSM_PSMT4HL 36
|
|
|
|
#define PSM_PSMT4HH 44
|
|
|
|
#define PSM_PSMZ32 48
|
|
|
|
#define PSM_PSMZ24 49
|
|
|
|
#define PSM_PSMZ16 50
|
|
|
|
#define PSM_PSMZ16S 58
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
struct VS_INPUT
|
|
|
|
{
|
|
|
|
float2 st : TEXCOORD0;
|
2012-01-27 11:56:49 +00:00
|
|
|
float4 c : COLOR0;
|
2012-01-18 11:47:31 +00:00
|
|
|
float q : TEXCOORD1;
|
2012-01-27 11:56:49 +00:00
|
|
|
uint2 p : POSITION0;
|
|
|
|
uint z : POSITION1;
|
2012-01-18 11:47:31 +00:00
|
|
|
uint2 uv : TEXCOORD2;
|
|
|
|
float4 f : COLOR1;
|
|
|
|
};
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
struct VS_OUTPUT
|
|
|
|
{
|
|
|
|
float4 p : SV_Position;
|
|
|
|
float2 z : TEXCOORD0;
|
|
|
|
float4 t : TEXCOORD1;
|
|
|
|
float4 c : COLOR0;
|
|
|
|
};
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
struct GS_OUTPUT
|
2012-01-13 18:10:05 +00:00
|
|
|
{
|
2012-01-18 11:47:31 +00:00
|
|
|
float4 p : SV_Position;
|
|
|
|
float2 z : TEXCOORD0;
|
|
|
|
float4 t : TEXCOORD1;
|
|
|
|
float4 c : COLOR0;
|
|
|
|
uint id : SV_PrimitiveID;
|
2012-01-13 18:10:05 +00:00
|
|
|
};
|
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
cbuffer VSConstantBuffer : register(c0)
|
|
|
|
{
|
|
|
|
float4 VertexScale;
|
|
|
|
float4 VertexOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
cbuffer PSConstantBuffer : register(c0)
|
|
|
|
{
|
|
|
|
uint2 WriteMask;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FragmentLinkItem
|
|
|
|
{
|
2012-01-28 10:07:17 +00:00
|
|
|
uint c, z, id, next;
|
2012-01-27 11:56:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RWByteAddressBuffer VideoMemory : register(u0);
|
|
|
|
RWStructuredBuffer<FragmentLinkItem> FragmentLinkBuffer : register(u1);
|
|
|
|
RWByteAddressBuffer StartOffsetBuffer : register(u2);
|
|
|
|
//RWTexture2D<uint> VideoMemory : register(u2); // 8192 * 512 R8_UINT
|
|
|
|
|
|
|
|
Buffer<int2> FZRowOffset : register(t0);
|
|
|
|
Buffer<int2> FZColOffset : register(t1);
|
|
|
|
Texture2D<float4> Palette : register(t2);
|
|
|
|
Texture2D<float4> Texture : register(t3);
|
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
VS_OUTPUT vs_main(VS_INPUT input)
|
|
|
|
{
|
|
|
|
VS_OUTPUT output;
|
|
|
|
|
|
|
|
output.p = float4(input.p, 0.0f, 0.0f) * VertexScale - VertexOffset;
|
2012-01-27 11:56:49 +00:00
|
|
|
output.z = float2(input.z & 0xffff, input.z >> 16); // TODO: min(input.z, 0xffffff00) ?
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
if(VS_TME)
|
|
|
|
{
|
|
|
|
if(VS_FST)
|
|
|
|
{
|
|
|
|
output.t.xy = input.uv;
|
|
|
|
output.t.w = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output.t.xy = input.st;
|
|
|
|
output.t.w = input.q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output.t.xy = 0;
|
|
|
|
output.t.w = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.c = input.c;
|
|
|
|
output.t.z = input.f.r;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if GS_PRIM == 0
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
[maxvertexcount(1)]
|
|
|
|
void gs_main(point VS_OUTPUT input[1], inout PointStream<GS_OUTPUT> stream, uint id : SV_PrimitiveID)
|
2012-01-13 18:10:05 +00:00
|
|
|
{
|
2012-01-18 11:47:31 +00:00
|
|
|
GS_OUTPUT output;
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
output.p = input[0].p;
|
|
|
|
output.z = input[0].z;
|
|
|
|
output.t = input[0].t;
|
|
|
|
output.c = input[0].c;
|
|
|
|
output.id = id;
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
stream.Append(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif GS_PRIM == 1
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
[maxvertexcount(2)]
|
|
|
|
void gs_main(line VS_OUTPUT input[2], inout LineStream<GS_OUTPUT> stream, uint id : SV_PrimitiveID)
|
|
|
|
{
|
2012-01-27 11:56:49 +00:00
|
|
|
[unroll]
|
2012-01-18 11:47:31 +00:00
|
|
|
for(int i = 0; i < 2; i++)
|
2012-01-13 18:10:05 +00:00
|
|
|
{
|
2012-01-18 11:47:31 +00:00
|
|
|
GS_OUTPUT output;
|
|
|
|
|
|
|
|
output.p = input[i].p;
|
|
|
|
output.z = input[i].z;
|
|
|
|
output.t = input[i].t;
|
|
|
|
output.c = input[i].c;
|
|
|
|
output.id = id;
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
#if GS_IIP == 0
|
2012-01-18 11:47:31 +00:00
|
|
|
if(i != 1) output.c = input[1].c;
|
2012-01-28 10:07:17 +00:00
|
|
|
#endif
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
stream.Append(output);
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
#elif GS_PRIM == 2
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
[maxvertexcount(3)]
|
|
|
|
void gs_main(triangle VS_OUTPUT input[3], inout TriangleStream<GS_OUTPUT> stream, uint id : SV_PrimitiveID)
|
|
|
|
{
|
2012-01-27 11:56:49 +00:00
|
|
|
[unroll]
|
2012-01-18 11:47:31 +00:00
|
|
|
for(int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
GS_OUTPUT output;
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
output.p = input[i].p;
|
|
|
|
output.z = input[i].z;
|
|
|
|
output.t = input[i].t;
|
|
|
|
output.c = input[i].c;
|
|
|
|
output.id = id;
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
#if GS_IIP == 0
|
2012-01-27 11:56:49 +00:00
|
|
|
if(i != 2) output.c = input[2].c;
|
2012-01-28 10:07:17 +00:00
|
|
|
#endif
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
stream.Append(output);
|
2012-01-13 18:10:05 +00:00
|
|
|
}
|
2012-01-18 11:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif GS_PRIM == 3
|
|
|
|
|
|
|
|
[maxvertexcount(4)]
|
|
|
|
void gs_main(line VS_OUTPUT input[2], inout TriangleStream<GS_OUTPUT> stream, uint id : SV_PrimitiveID)
|
|
|
|
{
|
|
|
|
GS_OUTPUT lt, rb, lb, rt;
|
|
|
|
|
|
|
|
lt.p = input[0].p;
|
|
|
|
lt.z = input[1].z;
|
|
|
|
lt.t.xy = input[0].t.xy;
|
|
|
|
lt.t.zw = input[1].t.zw;
|
|
|
|
lt.c = input[0].c;
|
|
|
|
lt.id = id;
|
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
#if GS_IIP == 0
|
2012-01-18 11:47:31 +00:00
|
|
|
lt.c = input[1].c;
|
2012-01-28 10:07:17 +00:00
|
|
|
#endif
|
2012-01-18 11:47:31 +00:00
|
|
|
|
|
|
|
rb.p = input[1].p;
|
|
|
|
rb.z = input[1].z;
|
|
|
|
rb.t = input[1].t;
|
|
|
|
rb.c = input[1].c;
|
|
|
|
rb.id = id;
|
|
|
|
|
|
|
|
lb = lt;
|
|
|
|
lb.p.y = rb.p.y;
|
|
|
|
lb.t.y = rb.t.y;
|
|
|
|
|
|
|
|
rt = rb;
|
|
|
|
rt.p.y = lt.p.y;
|
|
|
|
rt.t.y = lt.t.y;
|
2012-01-13 18:10:05 +00:00
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
stream.Append(lt);
|
|
|
|
stream.Append(lb);
|
|
|
|
stream.Append(rt);
|
|
|
|
stream.Append(rb);
|
2012-01-13 18:10:05 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
uint CompressColor32(float4 f)
|
2012-01-18 11:47:31 +00:00
|
|
|
{
|
|
|
|
uint4 c = (uint4)(f * 0xff) << uint4(0, 8, 16, 24);
|
|
|
|
|
|
|
|
return c.r | c.g | c.b | c.a;
|
|
|
|
}
|
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
uint DecompressColor16(uint c)
|
|
|
|
{
|
|
|
|
uint r = (c & 0x001f) << 3;
|
|
|
|
uint g = (c & 0x03e0) << 6;
|
|
|
|
uint b = (c & 0x7c00) << 9;
|
|
|
|
uint a = (c & 0x8000) << 15;
|
|
|
|
|
|
|
|
return r | g | b | a;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint ReadPixel(uint addr)
|
|
|
|
{
|
|
|
|
return VideoMemory.Load(addr) >> ((addr & 2) << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WritePixel(uint addr, uint value, uint psm)
|
2012-01-18 11:47:31 +00:00
|
|
|
{
|
2012-01-27 11:56:49 +00:00
|
|
|
uint tmp;
|
|
|
|
|
|
|
|
switch(psm)
|
|
|
|
{
|
|
|
|
case PSM_PSMCT32:
|
|
|
|
case PSM_PSMZ32:
|
|
|
|
case PSM_PSMCT24:
|
|
|
|
case PSM_PSMZ24:
|
|
|
|
VideoMemory.Store(addr, value);
|
|
|
|
break;
|
|
|
|
case PSM_PSMCT16:
|
|
|
|
case PSM_PSMCT16S:
|
|
|
|
case PSM_PSMZ16:
|
|
|
|
case PSM_PSMZ16S:
|
|
|
|
tmp = (addr & 2) << 3;
|
|
|
|
value = ((value << tmp) ^ VideoMemory.Load(addr)) & (0x0000ffff << tmp);
|
|
|
|
VideoMemory.InterlockedXor(addr, value, tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
void ps_main0(GS_OUTPUT input)
|
|
|
|
{
|
2012-01-18 11:47:31 +00:00
|
|
|
uint x = (uint)input.p.x;
|
|
|
|
uint y = (uint)input.p.y;
|
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
uint tail = FragmentLinkBuffer.IncrementCounter();
|
2012-01-27 11:56:49 +00:00
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
uint index = (y << 11) + x;
|
2012-01-27 11:56:49 +00:00
|
|
|
uint next = 0;
|
|
|
|
|
|
|
|
StartOffsetBuffer.InterlockedExchange(index * 4, tail, next);
|
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
FragmentLinkItem item;
|
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
// TODO: preprocess color (tfx, alpha test), z-test
|
|
|
|
|
|
|
|
item.c = CompressColor32(input.c);
|
|
|
|
item.z = (uint)(input.z.y * 0x10000 + input.z.x);
|
|
|
|
item.id = input.id;
|
2012-01-28 10:07:17 +00:00
|
|
|
item.next = next;
|
2012-01-27 11:56:49 +00:00
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
FragmentLinkBuffer[tail] = item;
|
2012-01-27 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ps_main1(GS_OUTPUT input)
|
|
|
|
{
|
|
|
|
uint2 pos = (uint2)input.p.xy;
|
|
|
|
|
|
|
|
// sort fragments
|
|
|
|
|
2012-01-28 10:07:17 +00:00
|
|
|
uint StartOffsetIndex = (pos.y << 11) + pos.x;
|
2012-01-27 11:56:49 +00:00
|
|
|
|
|
|
|
int index[PS_BATCH_SIZE];
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
uint next = StartOffsetBuffer.Load(StartOffsetIndex * 4);
|
|
|
|
|
|
|
|
StartOffsetBuffer.Store(StartOffsetIndex * 4, 0);
|
|
|
|
|
|
|
|
[allow_uav_condition]
|
2012-01-28 10:07:17 +00:00
|
|
|
while(next != 0)
|
2012-01-27 11:56:49 +00:00
|
|
|
{
|
|
|
|
index[count++] = next;
|
|
|
|
|
|
|
|
next = FragmentLinkBuffer[next].next;
|
|
|
|
}
|
|
|
|
|
|
|
|
int N2 = 1 << (int)(ceil(log2(count)));
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
[allow_uav_condition]
|
2012-01-28 10:07:17 +00:00
|
|
|
for(int i = count; i < N2; i++)
|
|
|
|
{
|
2012-01-27 11:56:49 +00:00
|
|
|
index[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[allow_uav_condition]
|
|
|
|
for(int k = 2; k <= N2; k = 2 * k)
|
|
|
|
{
|
|
|
|
[allow_uav_condition]
|
|
|
|
for(int j = k >> 1; j > 0 ; j = j >> 1)
|
|
|
|
{
|
|
|
|
[allow_uav_condition]
|
|
|
|
for(int i = 0; i < N2; i++)
|
|
|
|
{
|
|
|
|
uint i_id = FragmentLinkBuffer[index[i]].id;
|
|
|
|
|
|
|
|
int ixj = i ^ j;
|
2012-01-28 10:07:17 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
if(ixj > i)
|
|
|
|
{
|
|
|
|
uint ixj_id = FragmentLinkBuffer[index[ixj]].id;
|
|
|
|
|
|
|
|
if((i & k) == 0 && i_id > ixj_id)
|
|
|
|
{
|
|
|
|
int temp = index[i];
|
|
|
|
index[i] = index[ixj];
|
|
|
|
index[ixj] = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((i & k) != 0 && i_id < ixj_id)
|
|
|
|
{
|
|
|
|
int temp = index[i];
|
|
|
|
index[i] = index[ixj];
|
|
|
|
index[ixj] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
uint2 addr = (uint2)(FZRowOffset[pos.y] + FZColOffset[pos.x]) << 1;
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
uint dc = ReadPixel(addr.x);
|
|
|
|
uint dz = ReadPixel(addr.y);
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
uint sc = dc;
|
|
|
|
uint sz = dz;
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
[allow_uav_condition]
|
|
|
|
while(--count >= 0)
|
2012-01-28 10:07:17 +00:00
|
|
|
{
|
2012-01-27 11:56:49 +00:00
|
|
|
FragmentLinkItem f = FragmentLinkBuffer[index[count]];
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
// TODO
|
2012-01-18 11:47:31 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
if(sz < f.z)
|
|
|
|
{
|
|
|
|
sc = f.c;
|
|
|
|
sz = f.z;
|
|
|
|
}
|
|
|
|
}
|
2012-01-28 10:07:17 +00:00
|
|
|
|
2012-01-27 11:56:49 +00:00
|
|
|
uint c = sc; // (dc & ~WriteMask.x) | (sc & WriteMask.x);
|
|
|
|
uint z = 0;//sz; //(dz & ~WriteMask.y) | (sz & WriteMask.y);
|
|
|
|
|
|
|
|
WritePixel(addr.x, c, PS_FPSM);
|
|
|
|
WritePixel(addr.y, z, PS_ZPSM);
|
2012-01-18 11:47:31 +00:00
|
|
|
}
|