-
Notifications
You must be signed in to change notification settings - Fork 12
/
Mundus.Shader.Texture.pas
85 lines (72 loc) · 2.27 KB
/
Mundus.Shader.Texture.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
unit Mundus.Shader.Texture;
interface
uses
Classes,
Types,
SysUtils,
Graphics,
Mundus.Shader,
Mundus.Math,
Mundus.Types,
Mundus.Texture,
Mundus.ValueBuffer;
type
TTexturePSInput = record
UV: TFloat2;
Padding: TFloat2;
end;
TTextureShader = class sealed(TShader<TTexturePSInput>)
private
FUVs: ^TFloat2;
FTexture: TTexture;
public
procedure BindBuffer(const ABuffer: PValueBuffers); override;
procedure Vertex(const AWorld, AProjection: TMatrix4x4; var AVertex: TFloat4; const AVInput: TVertexShaderInput; const AAttributeBuffer: TShader<TTexturePSInput>.PAttributeType); override; final;
procedure Fragment(const APixel: PRGB32; const PSInput: TShader<TTexturePSInput>.PAttributeType); override; final;
class function GetRasterizer: TRasterizer; override; final;
end;
implementation
uses
Math,
Mundus.Math.Interpolation,
Mundus.Rasterizer;
{ TTextureShader }
{$PointerMath ON}
procedure TTextureShader.BindBuffer(const ABuffer: PValueBuffers);
begin
inherited;
FUVs := @ABuffer.Float2Array[ABuffer.Float2Array.GetBinding('UV0')][0];
FTexture := ABuffer.Texture[ABuffer.Texture.GetBinding('Tex0')];
end;
procedure TTextureShader.Fragment(const APixel: PRGB32; const PSInput: TShader<TTexturePSInput>.PAttributeType);
var
LTexX, LTextY: Integer;
const
sizeofsingle = sizeof(single);
begin
//truncate from single to int without using trunc which is a function and sloooow
asm
mov eax, [PSInput]
movss xmm0, [eax]
//SizeOf(SIngle) results in $31 instead of $4 wtf delphi?
movss xmm1, [eax + SizeOfSingle]
cvttss2si eax, xmm0
mov LTexX, eax
cvttss2si eax, xmm1
mov LTextY, eax
end;
FTexture.Sample(LTexX, LTextY, APixel);
end;
class function TTextureShader.GetRasterizer: TRasterizer;
begin
Result := TRasterizer(@TRasterizerFactory.RasterizeTriangle<TTexturePSInput, TTextureShader, TDepthWrite>);
end;
procedure TTextureShader.Vertex(const AWorld, AProjection: TMatrix4x4;
var AVertex: TFloat4; const AVInput: TVertexShaderInput;
const AAttributeBuffer: TShader<TTexturePSInput>.PAttributeType);
begin
AVertex := AProjection.Transform(AVertex);
AAttributeBuffer.UV.U := FUVs[AVInput.VertexID].U * FTexture.MaxX;
AAttributeBuffer.UV.V := FUVs[AVInput.VertexID].V * FTexture.MaxY;
end;
end.