forked from walbourn/directx-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.hlsl
58 lines (50 loc) · 1.83 KB
/
Render.hlsl
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
//--------------------------------------------------------------------------------------
// File: Render.hlsl
//
// The shaders for rendering tessellated mesh and base mesh
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License (MIT).
//--------------------------------------------------------------------------------------
cbuffer cbPerObject : register( b0 )
{
row_major matrix g_mWorldViewProjection : packoffset( c0 );
}
// The tessellated vertex structure
struct TessedVertex
{
uint BaseTriID; // Which triangle of the base mesh this tessellated vertex belongs to?
float2 bc; // Barycentric coordinates with regard to the base triangle
};
Buffer<float4> g_base_vb_buffer : register(t0); // Base mesh vertex buffer
StructuredBuffer<TessedVertex> g_TessedVertices : register(t1); // Tessellated mesh vertex buffer
float4 bary_centric(float4 v1, float4 v2, float4 v3, float2 bc)
{
return (1 - bc.x - bc.y) * v1 + bc.x * v2 + bc.y * v3;
}
float4 RenderVS( uint vertid : SV_VertexID ) : SV_POSITION
{
TessedVertex input = g_TessedVertices[vertid];
// Get the positions of the three vertices of the base triangle
float4 v[3];
[unroll]
for (int i = 0; i < 3; ++ i)
{
uint vert_id = input.BaseTriID * 3 + i;
v[i] = g_base_vb_buffer[vert_id];
}
// Calculate the position of this tessellated vertex from barycentric coordinates and then project it
return mul(bary_centric(v[0], v[1], v[2], input.bc), g_mWorldViewProjection);
}
struct BaseVertex
{
float4 pos : POSITION;
};
float4 RenderBaseVS( BaseVertex input ) : SV_POSITION
{
return mul( input.pos, g_mWorldViewProjection );
}
float4 RenderPS() : SV_TARGET
{
return float4( 1.0f, 1.0f, 0.0f, 1.0f );
}