-
Notifications
You must be signed in to change notification settings - Fork 14
/
AVX.cs
84 lines (72 loc) · 2.24 KB
/
AVX.cs
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
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class AVX : MonoBehaviour
{
[DllImport("avx")]
private static extern IntPtr GenerateTexture(int width, int height);
[DllImport("avx")]
private static extern void FreeMemory(IntPtr ptr);
Material _Material;
Texture2D _Texture;
int Width {get; set;} = 1024;
int Height {get; set;} = 1024;
void Start()
{
_Material = GetComponent<MeshRenderer>().sharedMaterial;
_Texture = new Texture2D(Width, Height, TextureFormat.RGBA32, false, false);
_Material.mainTexture = _Texture;
}
void Update()
{
IntPtr pixelDataPtr = GenerateTexture(Width, Height);
_Texture.LoadRawTextureData(pixelDataPtr, Width * Height * 4);
_Texture.Apply();
FreeMemory(pixelDataPtr);
}
void OnDestroy()
{
Destroy(_Texture);
}
}
/*
// Compile DLL and put in Assets/Plugins
// cl /LD /O2 /arch:AVX avx.c
#include <immintrin.h>
#include <stdlib.h>
typedef unsigned char byte;
int RGBAToInt(byte r, byte g, byte b, byte a)
{
return (a << 24) | (b << 16) | (g << 8) | r;
}
__declspec(dllexport) byte* GenerateTexture(int width, int height)
{
byte* bytes = (byte*)malloc(width * height * 4);
byte b = (byte)0;
byte a = (byte)255;
float w = (float)width;
float h = (float)height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x+=8)
{
int p0 = RGBAToInt((byte)((x + 0) / w * 255), (byte)(y / h * 255), b, a);
int p1 = RGBAToInt((byte)((x + 1) / w * 255), (byte)(y / h * 255), b, a);
int p2 = RGBAToInt((byte)((x + 2) / w * 255), (byte)(y / h * 255), b, a);
int p3 = RGBAToInt((byte)((x + 3) / w * 255), (byte)(y / h * 255), b, a);
int p4 = RGBAToInt((byte)((x + 4) / w * 255), (byte)(y / h * 255), b, a);
int p5 = RGBAToInt((byte)((x + 5) / w * 255), (byte)(y / h * 255), b, a);
int p6 = RGBAToInt((byte)((x + 6) / w * 255), (byte)(y / h * 255), b, a);
int p7 = RGBAToInt((byte)((x + 7) / w * 255), (byte)(y / h * 255), b, a);
int i = x + width * y;
__m256i pixels = _mm256_set_epi32(p7, p6, p5, p4, p3, p2, p1, p0);
_mm256_storeu_si256((__m256i*)&bytes[i * 4], pixels);
}
}
return bytes;
}
__declspec(dllexport) void FreeMemory(byte* pointer)
{
free(pointer);
}
*/