-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.hlsl
128 lines (112 loc) · 3.82 KB
/
update.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2020 gyabo <[email protected]>
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
struct ObjectFormat {
float4 pos;
float4 scale;
float4 rotate;
float4 color;
float4 uvinfo;
uint metadata[4];
};
struct VertexFormat {
float4 pos;
float4 uv;
float4 color;
uint matid;
uint reserved[3];
};
RWStructuredBuffer<ObjectFormat> obj : register(u0);
RWStructuredBuffer<VertexFormat> vtx : register(u1);
float2 rotate(float2 p, float a) {
float c = cos(a);
float s = sin(a);
return float2(
p.x * c - p.y * s,
p.x * s + p.y * c);
}
[numthreads(256, 1, 1)]
void CSMain(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{
uint tid = gl_GlobalInvocationID.x;
uint valid = obj[tid].metadata[0];
if(valid == 0)
return;
float4 pos = obj[tid].pos;
float4 scale = obj[tid].scale;
float4 color = obj[tid].color;
float4 uvinfo = obj[tid].uvinfo;
float rotvalue = obj[tid].rotate.x;
uint matid = obj[tid].metadata[1];
float2 basepos[4];
float2 baseuv[4];
float2 uv_div = uvinfo.zw;
float2 uv_unit = 1.0 / uv_div;
float2 uv_offset = uv_unit * uvinfo.xy;
baseuv[0] = (float2( 0, 0) / uv_div) + uv_offset;
baseuv[1] = (float2( 0, 1) / uv_div) + uv_offset;
baseuv[2] = (float2( 1, 0) / uv_div) + uv_offset;
baseuv[3] = (float2( 1, 1) / uv_div) + uv_offset;
//scale
basepos[0] = float2(-1, -1) * scale.xy;
basepos[1] = float2(-1, 1) * scale.xy;
basepos[2] = float2( 1, -1) * scale.xy;
basepos[3] = float2( 1, 1) * scale.xy;
//rotate
basepos[0] = rotate(basepos[0], rotvalue);
basepos[1] = rotate(basepos[1], rotvalue);
basepos[2] = rotate(basepos[2], rotvalue);
basepos[3] = rotate(basepos[3], rotvalue);
//trans
basepos[0] += pos.xy;
basepos[1] += pos.xy;
basepos[2] += pos.xy;
basepos[3] += pos.xy;
//result
float2 aspect = float2(1.0, 1.0);
vtx[tid * 6 + 0].pos = float4(basepos[0] * aspect, 0, 1);
vtx[tid * 6 + 1].pos = float4(basepos[1] * aspect, 0, 1);
vtx[tid * 6 + 2].pos = float4(basepos[2] * aspect, 0, 1);
vtx[tid * 6 + 3].pos = float4(basepos[1] * aspect, 0, 1);
vtx[tid * 6 + 4].pos = float4(basepos[3] * aspect, 0, 1);
vtx[tid * 6 + 5].pos = float4(basepos[2] * aspect, 0, 1);
//uv
vtx[tid * 6 + 0].uv = float4(baseuv[0], 0, 1);
vtx[tid * 6 + 1].uv = float4(baseuv[1], 0, 1);
vtx[tid * 6 + 2].uv = float4(baseuv[2], 0, 1);
vtx[tid * 6 + 3].uv = float4(baseuv[1], 0, 1);
vtx[tid * 6 + 4].uv = float4(baseuv[3], 0, 1);
vtx[tid * 6 + 5].uv = float4(baseuv[2], 0, 1);
//color
vtx[tid * 6 + 0].color = color;
vtx[tid * 6 + 1].color = color;
vtx[tid * 6 + 2].color = color;
vtx[tid * 6 + 3].color = color;
vtx[tid * 6 + 4].color = color;
vtx[tid * 6 + 5].color = color;
//matid
vtx[tid * 6 + 0].matid = matid;
vtx[tid * 6 + 1].matid = matid;
vtx[tid * 6 + 2].matid = matid;
vtx[tid * 6 + 3].matid = matid;
vtx[tid * 6 + 4].matid = matid;
vtx[tid * 6 + 5].matid = matid;
}