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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
C++RAW
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h"
#include "Runtime/Shaders/ComputeShader.h"
#include "Runtime/Graphics/Texture.h"
#include "Runtime/Math/Vector4.h"
#include "Runtime/Threads/AtomicOps.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Scripting/ScriptingObjectWithIntPtrField.h"
#include "Runtime/Mono/MonoBehaviour.h"
#include "Runtime/Misc/GraphicsScriptingUtility.h"
CSRAW
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections;
namespace UnityEngine
{
// Compute Shader asset.
CLASS ComputeShader : Object
// Find [[ComputeShader]] kernel index.
CUSTOM int FindKernel (string name) {
FastPropertyName fpName = ScriptingStringToProperty(name);
return self->FindKernel (fpName);
}
// Set a float parameter.
CUSTOM void SetFloat (string name, float val) {
FastPropertyName fpName = ScriptingStringToProperty(name);
self->SetValueParam (fpName, 4, &val);
}
// Set an integer parameter.
CUSTOM void SetInt (string name, int val) {
FastPropertyName fpName = ScriptingStringToProperty(name);
self->SetValueParam (fpName, 4, &val);
}
// Set a vector parameter.
CUSTOM void SetVector (string name, Vector4 val) {
FastPropertyName fpName = ScriptingStringToProperty(name);
self->SetValueParam (fpName, 16, &val.x);
}
// Set multiple consecutive float parameters at once.
CSRAW public void SetFloats (string name, params float[] values) {
Internal_SetFloats (name, values);
}
CUSTOM private void Internal_SetFloats (string name, float[] values) {
FastPropertyName fpName = ScriptingStringToProperty(name);
int size = GetScriptingArraySize (values);
const float* arr = Scripting::GetScriptingArrayStart<float> (values);
self->SetValueParam (fpName, size*4, arr);
}
// Set multiple consecutive integer parameters at once.
CSRAW public void SetInts (string name, params int[] values) {
Internal_SetInts (name, values);
}
CUSTOM private void Internal_SetInts (string name, int[] values) {
FastPropertyName fpName = ScriptingStringToProperty(name);
int size = GetScriptingArraySize (values);
const int* arr = Scripting::GetScriptingArrayStart<int> (values);
self->SetValueParam (fpName, size*4, arr);
}
// Set a texture parameter.
CUSTOM void SetTexture (int kernelIndex, string name, Texture texture) {
FastPropertyName fpName = ScriptingStringToProperty(name);
Texture& tex = *texture;
self->SetTextureParam (kernelIndex, fpName, tex.GetTextureID());
}
// Set a [[ComputeBuffer]] parameter.
//
CUSTOM void SetBuffer (int kernelIndex, string name, ComputeBuffer buffer) {
FastPropertyName fpName = ScriptingStringToProperty(name);
self->SetBufferParam (kernelIndex, fpName, buffer->GetBufferHandle());
}
// Execute a compute shader.
CUSTOM void Dispatch (int kernelIndex, int threadsX, int threadsY, int threadsZ)
{
self->DispatchComputeShader (kernelIndex, threadsX, threadsY, threadsZ);
}
END
// [[ComputeBuffer]] type.
CSRAW [Flags]
ENUM ComputeBufferType
// Default [[ComputeBuffer]] type.
Default = 0,
// Raw [[ComputeBuffer]] type.
Raw = 1,
// Append-consume [[ComputeBuffer]] type.
Append = 2,
// [[ComputeBuffer]] with a counter.
Counter = 4,
// [[ComputeBuffer]] used for Graphics.DrawProceduralIndirect.
DrawIndirect = 256,
END
// Data buffer to hold data for compute shaders.
CLASS ComputeBuffer : IDisposable
CSRAW #pragma warning disable 414
CSRAW internal IntPtr m_Ptr;
CSRAW #pragma warning restore 414
// IDisposable implementation, with Release() for explicit cleanup.
~ComputeBuffer()
{
Dispose(false);
}
//*undocumented*
CSRAW public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose (bool disposing)
{
// we don't have any managed references, so
// 'disposing' part of standard IDisposable pattern
// does not apply
// Release native resources
DestroyBuffer (this);
m_Ptr = IntPtr.Zero;
}
CUSTOM private static void InitBuffer (ComputeBuffer buf, int count, int stride, ComputeBufferType type) {
buf.SetPtr(new ComputeBuffer(count, stride, type));
}
CUSTOM private static void DestroyBuffer(ComputeBuffer buf) {
delete buf.GetPtr();
}
///*listonly*
CSRAW public ComputeBuffer (int count, int stride) : this (count, stride, ComputeBufferType.Default)
{
}
// Create a Compute Buffer.
CSRAW public ComputeBuffer (int count, int stride, ComputeBufferType type)
{
m_Ptr = IntPtr.Zero;
InitBuffer (this, count, stride, type);
}
// Release a Compute Buffer.
CSRAW public void Release ()
{
Dispose();
}
// Number of elements in the buffer (RO).
CUSTOM_PROP public int count {
return self->GetCount();
}
// Size of one element in the buffer (RO).
CUSTOM_PROP public int stride {
return self->GetStride();
}
// Set buffer data.
CSRAW
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public void SetData (System.Array data) {
#if !UNITY_FLASH
InternalSetData (data, Marshal.SizeOf(data.GetType().GetElementType()));
#endif
}
CSRAW [System.Security.SecurityCritical] // to prevent accidentally making this public in the future
CUSTOM private void InternalSetData (System.Array data, int elemSize) {
self->SetData (Scripting::GetScriptingArrayStart<char>(data), GetScriptingArraySize(data) * elemSize);
}
// Read buffer data.
CSRAW
[System.Security.SecuritySafeCritical] // due to Marshal.SizeOf
public void GetData (System.Array data) {
#if !UNITY_FLASH
InternalGetData (data, Marshal.SizeOf(data.GetType().GetElementType()));
#endif
}
CSRAW [System.Security.SecurityCritical] // to prevent accidentally making this public in the future
CUSTOM private void InternalGetData (System.Array data, int elemSize) {
self->GetData (Scripting::GetScriptingArrayStart<char>(data), GetScriptingArraySize(data) * elemSize);
}
// Copy counter value of append/consume buffer into another buffer.
CUSTOM static void CopyCount (ComputeBuffer src, ComputeBuffer dst, int dstOffset) {
ComputeBuffer::CopyCount (src.GetPtr(), dst.GetPtr(), dstOffset);
}
END
CSRAW
}
|