-
Notifications
You must be signed in to change notification settings - Fork 12
/
Mundus.Shader.DepthColor.pas
62 lines (50 loc) · 1.53 KB
/
Mundus.Shader.DepthColor.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
unit Mundus.Shader.DepthColor;
interface
uses
Classes,
Types,
Mundus.Types,
Mundus.Shader,
Mundus.Math,
Graphics,
SysUtils;
type
TDepthPSInput = TFloat4;
PDepthPSInput = ^TDepthPSInput;
TDepthColorShader = class sealed(TShader<TDepthPSInput>)
public
procedure Vertex(const AWorld, AProjection: TMatrix4x4; var AVertex: TFloat4; const AVInput: TVertexShaderInput; const AAttributeBuffer: TShader<TDepthPSInput>.PAttributeType); override; final;
procedure Fragment(const APixel: PRGB32; const PSInput: TShader<TDepthPSInput>.PAttributeType); override; final;
class function GetRasterizer: TRasterizer; override;
end;
implementation
uses
Mundus.Rasterizer;
{ TDepthColorShader }
{ TDepthColorShader }
procedure TDepthColorShader.Fragment(const APixel: PRGB32;
const PSInput: TShader<TDepthPSInput>.PAttributeType);
asm
//load input
movups xmm2, [PSInput]
//convert Single to DWord
cvttps2dq xmm2, xmm2
//Pack DWord to Word
packusdw xmm2, xmm2
//Pack Word to Byte
packuswb xmm2, xmm2
//write final color values
PEXTRD [APixel], xmm2, 0
end;
class function TDepthColorShader.GetRasterizer: TRasterizer;
begin
Result := TRasterizer(@TRasterizerFactory.RasterizeTriangle<TDepthPSInput, TDepthColorShader, TNoDepth>);
end;
procedure TDepthColorShader.Vertex(const AWorld, AProjection: TMatrix4x4;
var AVertex: TFloat4; const AVInput: TVertexShaderInput;
const AAttributeBuffer: TShader<TDepthPSInput>.PAttributeType);
begin
inherited;
AAttributeBuffer.R := 255-255*AVertex.Z/AVertex.W;
end;
end.