Skip to content

Commit e85531b

Browse files
committed
13: fix IspcBuilder
1 parent 00ba9a5 commit e85531b

File tree

6 files changed

+992
-28
lines changed

6 files changed

+992
-28
lines changed

BridgeBuilder/6_Ispc/IspcBuilder.cs

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ void GenerateCBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
292292
sb.AppendLine("#include <stdlib.h>");
293293
sb.AppendLine("//Include the header file that the ispc compiler generates");
294294
sb.AppendLine($"#include \"{ Path.GetFileName(ispc_headerFilename) }\"");
295-
295+
sb.AppendLine("using namespace ispc;");
296296
//c_inf.AppendLine("using namespace ispc;");
297297
sb.AppendLine("extern \"C\"{");
298298

@@ -311,7 +311,7 @@ void GenerateCBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
311311
sb.AppendLine("__declspec(dllexport) "); //WINDOWS
312312

313313
sb.Append(met.ToString(IspcBridgeFunctionNamePrefix));
314-
314+
315315
sb.AppendLine("{");
316316
string ret = met.ReturnType.ToString();
317317
if (ret != "void")
@@ -330,10 +330,9 @@ void GenerateCBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
330330
sb.AppendLine(");");
331331
sb.AppendLine("}");
332332
}
333-
else if (nsMember is CodeTypeDeclaration other)
333+
else if (nsMember is CodeTypeDeclaration other && other.Kind == TypeKind.Struct)
334334
{
335-
//
336-
335+
//skip C struct regen
337336
}
338337
else
339338
{
@@ -352,6 +351,7 @@ void GenerateCBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
352351
string file_content = sb.ToString();
353352
File.WriteAllText(outputC_filename, file_content);
354353
}
354+
355355

356356
/// <summary>
357357
/// generate CS binder
@@ -373,9 +373,15 @@ void GenerateCsBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
373373
sb.AppendLine("using System.Runtime.InteropServices;");
374374

375375
sb.AppendLine();
376-
sb.AppendLine("using int32_t = System.Int32;");
377-
sb.AppendLine("using uint32_t = System.UInt32;");
378-
sb.AppendLine();
376+
377+
sb.AppendLine(@"
378+
using uint8_t = System.Byte;
379+
using int8_t = System.SByte;
380+
using uint16_t = System.UInt16;
381+
using int16_t = System.Int16;
382+
using int32_t = System.Int32;
383+
using uint32_t = System.UInt32;
384+
");
379385

380386
//
381387
sb.AppendLine("namespace " + Path.GetFileNameWithoutExtension(Path.GetFileName(ispc_headerFilename)) + "{");
@@ -402,17 +408,37 @@ void GenerateCsBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
402408
{
403409
CodeMethodParameter par = met.Parameters[i];
404410
if (i > 0) { sb.Append(","); }
405-
sb.Append(par.ParameterType.ToString());
411+
412+
string parType = CsGetProperParameterType(par.ParameterType);
413+
414+
sb.Append(parType);
406415
sb.Append(" ");
407416
sb.Append(par.ParameterName);
408417
}
409418
sb.Append(")");
410419
sb.AppendLine(";");
411420

412421
}
413-
else if (ns_mb is CodeTypeDeclaration typedecl)
422+
else if (ns_mb is CodeTypeDeclaration typedecl && typedecl.Kind == TypeKind.Struct)
414423
{
424+
//generate struct (cs version) for
425+
sb.AppendLine("public struct " + typedecl.Name + "{");
426+
foreach (CodeMemberDeclaration structMb in typedecl.GetMemberIter())
427+
{
428+
switch (structMb.MemberKind)
429+
{
430+
default: throw new NotSupportedException();
431+
case CodeMemberKind.Field:
432+
{
433+
CodeFieldDeclaration fieldDecl = (CodeFieldDeclaration)structMb;
434+
CsWriteField(sb, fieldDecl);
435+
436+
}
437+
break;
438+
}
415439

440+
}
441+
sb.AppendLine("}");
416442
}
417443
else
418444
{
@@ -432,5 +458,69 @@ void GenerateCsBinder(CodeCompilationUnit cu, string ispc_headerFilename, string
432458
string file_content = sb.ToString();
433459
File.WriteAllText(outputCs_filename, file_content);
434460
}
461+
462+
/// <summary>
463+
/// get parameter type for cs interface
464+
/// </summary>
465+
/// <param name="metParType"></param>
466+
/// <returns></returns>
467+
static string CsGetProperParameterType(CodeTypeReference metParType)
468+
{
469+
470+
if (metParType.Kind == CodeTypeReferenceKind.Array)
471+
{
472+
throw new NotSupportedException();
473+
}
474+
else if (metParType.Kind == CodeTypeReferenceKind.ByRef)
475+
{
476+
//eg. A&
477+
//inner type
478+
CodeByRefTypeReference byRefTypeRef = (CodeByRefTypeReference)metParType;
479+
string innerElemType = CsGetProperParameterType(byRefTypeRef.ElementType);
480+
return innerElemType + "*";
481+
}
482+
else
483+
{
484+
//a simple type
485+
if (metParType.Name == "char")
486+
{
487+
throw new NotSupportedException();
488+
}
489+
}
490+
return metParType.ToString();
491+
}
492+
static void CsWriteField(CodeStringBuilder sb, CodeFieldDeclaration field)
493+
{
494+
495+
sb.Append("public ");
496+
CodeTypeReference fieldType = field.FieldType;
497+
if (fieldType.Kind == CodeTypeReferenceKind.Array)
498+
{
499+
CodeArrayReference arrField = (CodeArrayReference)fieldType;
500+
if (arrField.ElemType.Kind == CodeTypeReferenceKind.Array)
501+
{
502+
503+
CodeArrayReference next = (CodeArrayReference)arrField.ElemType;
504+
//only a[][] , 2 levels
505+
if (next.ElemType.Kind == CodeTypeReferenceKind.Array) { throw new NotSupportedException(); }
506+
507+
int size = arrField.Size * next.Size;
508+
509+
sb.Append("fixed " + next.ElemType.ToString() + " " + field.Name + "[" + size + "];");
510+
}
511+
else
512+
{
513+
//simple array
514+
sb.Append("fixed " + arrField.ElemType.ToString() + " " + field.Name + "[" + arrField.Size + "];");
515+
}
516+
}
517+
else
518+
{
519+
sb.Append(fieldType.ToString());
520+
sb.Append(" ");
521+
sb.Append(field.Name);
522+
sb.AppendLine(";");
523+
}
524+
}
435525
}
436526
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*x31*///AUTOGEN,2020-06-19T08:09:37
2+
/*x32*///Tools: ispc and BridgeBuilder
3+
/*x33*///Src: D:\projects\ispc_experiment\TestLoadLib\bin\Debug/temp/kernels_ispc.h
4+
/*x34*/
5+
/*x35*/using System;
6+
/*x36*/using System.Runtime.InteropServices;
7+
/*x37*/
8+
/*x38*/
9+
using uint8_t = System.Byte;
10+
using int8_t = System.SByte;
11+
using uint16_t = System.UInt16;
12+
using int16_t = System.Int16;
13+
using int32_t = System.Int32;
14+
using uint32_t = System.UInt32;
15+
16+
/*x39*/namespace kernels_ispc{
17+
/*x40*/public static unsafe class NativeMethods{
18+
/*x41*/const string LIB_NAME="kernels.dll";
19+
/*x42*/public struct InputHeader{
20+
public fixed float cameraProj[16];public float cameraNear/*x43*/;
21+
public float cameraFar/*x44*/;
22+
public int32_t framebufferWidth/*x45*/;
23+
public int32_t framebufferHeight/*x46*/;
24+
public int32_t numLights/*x47*/;
25+
public int32_t inputDataChunkSize/*x48*/;
26+
public fixed int32_t inputDataArrayOffsets[16];/*x49*/}
27+
/*x50*/public struct InputDataArrays{
28+
public float* zBuffer/*x51*/;
29+
public uint16_t* normalEncoded_x/*x52*/;
30+
public uint16_t* normalEncoded_y/*x53*/;
31+
public uint16_t* specularAmount/*x54*/;
32+
public uint16_t* specularPower/*x55*/;
33+
public uint8_t* albedo_x/*x56*/;
34+
public uint8_t* albedo_y/*x57*/;
35+
public uint8_t* albedo_z/*x58*/;
36+
public float* lightPositionView_x/*x59*/;
37+
public float* lightPositionView_y/*x60*/;
38+
public float* lightPositionView_z/*x61*/;
39+
public float* lightAttenuationBegin/*x62*/;
40+
public float* lightColor_x/*x63*/;
41+
public float* lightColor_y/*x64*/;
42+
public float* lightColor_z/*x65*/;
43+
public float* lightAttenuationEnd/*x66*/;
44+
/*x67*/}
45+
/*x68*/[DllImport(LIB_NAME,EntryPoint ="my_ComputeZBoundsRow")]
46+
public static extern void ComputeZBoundsRow(int32_t tileY,int32_t tileWidth,int32_t tileHeight,int32_t numTilesX,int32_t numTilesY,float* zBuffer,int32_t gBufferWidth,float cameraProj_33,float cameraProj_43,float cameraNear,float cameraFar,float* minZArray,float* maxZArray)/*x69*/;
47+
/*x70*/[DllImport(LIB_NAME,EntryPoint ="my_IntersectLightsWithTileMinMax")]
48+
public static extern int32_t IntersectLightsWithTileMinMax(int32_t tileStartX,int32_t tileEndX,int32_t tileStartY,int32_t tileEndY,float minZ,float maxZ,int32_t gBufferWidth,int32_t gBufferHeight,float cameraProj_11,float cameraProj_22,int32_t numLights,float* light_positionView_x_array,float* light_positionView_y_array,float* light_positionView_z_array,float* light_attenuationEnd_array,int32_t* tileLightIndices)/*x71*/;
49+
/*x72*/[DllImport(LIB_NAME,EntryPoint ="my_RenderStatic")]
50+
public static extern void RenderStatic(InputHeader* inputHeader,InputDataArrays* inputData,int32_t visualizeLightCount,uint8_t* framebuffer_r,uint8_t* framebuffer_g,uint8_t* framebuffer_b)/*x73*/;
51+
/*x74*/[DllImport(LIB_NAME,EntryPoint ="my_ShadeTile")]
52+
public static extern void ShadeTile(int32_t tileStartX,int32_t tileEndX,int32_t tileStartY,int32_t tileEndY,int32_t gBufferWidth,int32_t gBufferHeight,InputDataArrays* inputData,float cameraProj_11,float cameraProj_22,float cameraProj_33,float cameraProj_43,int32_t* tileLightIndices,int32_t tileNumLights,bool visualizeLightCount,uint8_t* framebuffer_r,uint8_t* framebuffer_g,uint8_t* framebuffer_b)/*x75*/;
53+
/*x76*/[DllImport(LIB_NAME,EntryPoint ="my_SplitTileMinMax")]
54+
public static extern void SplitTileMinMax(int32_t tileMidX,int32_t tileMidY,float* subtileMinZ,float* subtileMaxZ,int32_t gBufferWidth,int32_t gBufferHeight,float cameraProj_11,float cameraProj_22,int32_t* lightIndices,int32_t numLights,float* light_positionView_x_array,float* light_positionView_y_array,float* light_positionView_z_array,float* light_attenuationEnd_array,int32_t* subtileIndices,int32_t subtileIndicesPitch,int32_t* subtileNumLights)/*x77*/;
55+
/*x78*/}
56+
/*x79*/}

TestLoadLib/Program.cs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ static void Main(string[] args)
1818
//----------
1919
//more easier
2020
//----------
21-
Ispc_SimpleExample();
21+
//Ispc_SimpleExample();
2222
//Ispc_SortExample();
2323
//Ispc_MandelbrotExample();
2424
//Ispc_MandlebrotTaskExample();
25+
Ispc_DeferredShading();
2526
#if DEBUG
26-
// dbugParseHeader(@"D:\projects\ispc-14-dev-windows\examples_build\deferred\kernels_ispc.h");
27+
// dbugParseHeader(@"deferred\kernels_ispc.h");
2728
#endif
2829
}
2930
#if DEBUG
@@ -39,20 +40,21 @@ static void dbugParseHeader(string filename)
3940
static void Ispc_SimpleExample()
4041
{
4142
//from: ispc-14-dev-windows\examples\simple
42-
string dllName = "simple.dll";
43+
string module = "simple";
4344

4445
//TODO: check if we need to rebuild or not
45-
bool rebuild = NeedRebuild("simple");
46+
bool rebuild = NeedRebuild(module);
4647
if (rebuild)
4748
{
4849
IspcBuilder ispcBuilder = new IspcBuilder();
4950
ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
50-
ispcBuilder.IspcFilename = "simple.ispc";
51-
ispcBuilder.AutoCsTargetFile = "..\\..\\AutoGenBinders\\simple.cs";
51+
ispcBuilder.IspcFilename = module + ".ispc";
52+
ispcBuilder.AutoCsTargetFile = $"..\\..\\AutoGenBinders\\{module}.cs";
5253
ispcBuilder.RebuildLibraryAndAPI();
5354

5455
}
5556

57+
string dllName = module + ".dll";
5658
IntPtr dllPtr = LoadLibrary(dllName);
5759

5860
if (dllPtr == IntPtr.Zero) { throw new NotSupportedException(); }
@@ -92,15 +94,15 @@ static void Ispc_SortExample()
9294
{
9395
//from: ispc-14-dev-windows\examples\sort
9496

95-
string dllName = "sort.dll";
97+
string module = "sort";
9698
//TODO: check if we need to rebuild or not
97-
bool rebuild = NeedRebuild("sort");
99+
bool rebuild = NeedRebuild(module);
98100
if (rebuild)
99101
{
100102
IspcBuilder ispcBuilder = new IspcBuilder();
101103
ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
102-
ispcBuilder.IspcFilename = "sort.ispc";
103-
ispcBuilder.AutoCsTargetFile = "..\\..\\AutoGenBinders\\sort.cs";
104+
ispcBuilder.IspcFilename = module + ".ispc";
105+
ispcBuilder.AutoCsTargetFile = $"..\\..\\AutoGenBinders\\{module}.cs";
104106

105107
string currentDir = Directory.GetCurrentDirectory();
106108
ispcBuilder.AdditionalInputItems = new string[]
@@ -111,6 +113,7 @@ static void Ispc_SortExample()
111113

112114
}
113115

116+
string dllName = module + ".dll";
114117
IntPtr dllPtr = LoadLibrary(dllName);
115118

116119
if (dllPtr == IntPtr.Zero) { throw new NotSupportedException(); }
@@ -142,15 +145,15 @@ static void Ispc_SortExample()
142145
static void Ispc_MandelbrotExample()
143146
{
144147
//from: ispc-14-dev-windows\examples\mandelbrot
145-
string dllName = "mandelbrot.dll";
148+
string module = "mandelbrot.dll";
146149
//TODO: check if we need to rebuild or not
147-
bool rebuild = NeedRebuild("mandelbrot");
150+
bool rebuild = NeedRebuild(module);
148151
if (rebuild)
149152
{
150153
IspcBuilder ispcBuilder = new IspcBuilder();
151154
ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
152-
ispcBuilder.IspcFilename = "mandelbrot.ispc";
153-
ispcBuilder.AutoCsTargetFile = "..\\..\\AutoGenBinders\\mandelbrot.cs";
155+
ispcBuilder.IspcFilename = module + ".ispc";
156+
ispcBuilder.AutoCsTargetFile = $"..\\..\\AutoGenBinders\\{module}.cs";
154157

155158
string currentDir = Directory.GetCurrentDirectory();
156159
ispcBuilder.AdditionalInputItems = new string[]
@@ -160,6 +163,7 @@ static void Ispc_MandelbrotExample()
160163
ispcBuilder.RebuildLibraryAndAPI();
161164
}
162165

166+
string dllName = module + ".dll";
163167
IntPtr dllPtr = LoadLibrary(dllName);
164168

165169
if (dllPtr == IntPtr.Zero) { throw new NotSupportedException(); }
@@ -187,15 +191,16 @@ static void Ispc_MandelbrotExample()
187191
static void Ispc_MandlebrotTaskExample()
188192
{
189193
//from: ispc-14-dev-windows\examples\mandelbrot
190-
string dllName = "mandelbrot_task.dll";
194+
191195
//TODO: check if we need to rebuild or not
192-
bool rebuild = NeedRebuild("mandelbrot_task");
196+
string module = "mandelbrot_task";
197+
bool rebuild = NeedRebuild(module);
193198
if (rebuild)
194199
{
195200
IspcBuilder ispcBuilder = new IspcBuilder();
196201
ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
197-
ispcBuilder.IspcFilename = "mandelbrot_task.ispc";
198-
ispcBuilder.AutoCsTargetFile = "..\\..\\AutoGenBinders\\mandelbrot_task.cs";
202+
ispcBuilder.IspcFilename = module + ".ispc";
203+
ispcBuilder.AutoCsTargetFile = $"..\\..\\AutoGenBinders\\{module}.cs";
199204

200205
string currentDir = Directory.GetCurrentDirectory();
201206
ispcBuilder.AdditionalInputItems = new string[]
@@ -205,6 +210,7 @@ static void Ispc_MandlebrotTaskExample()
205210
ispcBuilder.RebuildLibraryAndAPI();
206211
}
207212

213+
string dllName = module + ".dll";
208214
IntPtr dllPtr = LoadLibrary(dllName);
209215

210216
if (dllPtr == IntPtr.Zero) { throw new NotSupportedException(); }
@@ -230,7 +236,33 @@ static void Ispc_MandlebrotTaskExample()
230236
SaveManelbrotImage(buffer, width, height, "test_mandelbrot_task.png");
231237
}
232238

239+
static void Ispc_DeferredShading()
240+
{
241+
//from ispc-14-dev-windows\examples\deferred\kernels.ispc
242+
243+
//TODO: check if we need to rebuild or not
244+
string module = "kernels";
245+
bool rebuild = NeedRebuild(module);
246+
if (rebuild)
247+
{
248+
IspcBuilder ispcBuilder = new IspcBuilder();
249+
ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
250+
ispcBuilder.IspcFilename = module + ".ispc";
251+
ispcBuilder.AutoCsTargetFile = $"..\\..\\AutoGenBinders\\{module}.cs";
252+
253+
string currentDir = Directory.GetCurrentDirectory();
254+
ispcBuilder.AdditionalInputItems = new string[]
255+
{
256+
currentDir + "\\tasksys.cpp",
257+
currentDir + "\\deferred.h"
258+
};
259+
ispcBuilder.RebuildLibraryAndAPI();
260+
}
233261

262+
string dllName = module + ".dll";
263+
IntPtr dllPtr = LoadLibrary(dllName);
264+
if (dllPtr == IntPtr.Zero) { throw new NotSupportedException(); }
265+
}
234266

235267
static void SaveManelbrotImage(int[] buffer, int width, int height, string filename)
236268
{

0 commit comments

Comments
 (0)