Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an example of generating a Unity Texture? #53

Open
mfagerlund opened this issue Mar 16, 2019 · 4 comments
Open

Is there an example of generating a Unity Texture? #53

mfagerlund opened this issue Mar 16, 2019 · 4 comments

Comments

@mfagerlund
Copy link

I'd like to use Cekirdekler to generate textures from kernels that I generate at runtime, is there any minimal demo showing how to do this?

cheers,
m

@tugrul512bit
Copy link
Owner

Sorry for this late response,

If you are asking about pure OpenGL textures, I don't have enough experience with them. But if you just need some array with specific format, then I can help you. Even hello world example can be adjusted to generate an array with some format. I will add some here.

@tugrul512bit
Copy link
Owner

tugrul512bit commented Mar 21, 2019

For example, below code should generate 4096 element arrays with zero on all elements by initializing them as float4 (4-element float vectors) inside kernel (using 1024 threads spanning all GPUs, each thread initializing 1 float4 for a(f), 1 float4 for b(g)).

ClPlatforms platforms = ClPlatforms.all();
var selectedDevices = platforms.devicesWithMostComputeUnits().gpus(true);
selectedDevices.logInfo();
ClNumberCruncher gpu = new ClNumberCruncher(selectedDevices, @"
     __kernel void algorithmTest(__global float4 * a, __global float4 * b)
     { 
          int i=get_global_id(0);
          a[i]=(float4)(0.0f,0.0f,0.0f,0.0f);
          b[i]=(float4)(0.0f,0.0f,0.0f,0.0f);
     }
");
ClArray<float> f = new ClArray<float>(4096);
f.numberOfElementsPerWorkItem = 4;
f.writeOnly=true;
f.zeroCopy=true;
f.write = true;
f.read = false;
f.readOnly = false;
ClArray<float> g = new ClArray<float>(4096);
g.numberOfElementsPerWorkItem = 4;
g.writeOnly=true;
g.zeroCopy=true;
g.write = true;
g.read = false;
g.readOnly = false;
gpu.performanceFeed = true;

// parameter "64" is the minimum allowed thread trade between GPUs so that the balance the workload
f.nextParam(g).compute(gpu, 1, "algorithmTest", 4096/4, 64);
// here f[index] and g[index] must have zero

@tugrul512bit
Copy link
Owner

tugrul512bit commented Mar 21, 2019

Also here is how I tried producing vertices (as height-map of a sphere)

https://github.com/tugrul512bit/unityTestMeshDeformation/blob/master/gpgpu_test/Assets/Kamera.cs
(kernel is from line 236 to 245)

In there, to use "structs" of Unity, such as

public Vector3[] verticesBase

you can use

xyzGPU = ClArray<byte>.wrapArrayOfStructs(verticesBase);

it wraps around whole structs and treats all items in them as floats or whatever items in kernel as you like. Using byte type is just for treating as a data transmission buffer on host side. In kernel, you can use their elements by casting to different type on proper alignment points. You shouldn't dereference a non-multiple of 4 address as a float nor int.

@mfagerlund
Copy link
Author

mfagerlund commented Mar 22, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants