├── README.md ├── kernelFile2.cl ├── kernelFile3.cl └── TextureFinder.cpp /README.md: -------------------------------------------------------------------------------- 1 | # TextureFinderOpenCL 2 | -------------------------------------------------------------------------------- /kernelFile2.cl: -------------------------------------------------------------------------------- 1 | int getTextureType(int x, int y, int z); 2 | 3 | int compatibleRotation(int generatedType, int rotation, int blockFace); 4 | 5 | kernel void kernelFile(global int *faces, global int *facecount, global int *found, global int *xlength, global int *zlength, global int *ymin) 6 | { 7 | if(found[1] == 0){ 8 | int foundHere=1; 9 | int i; 10 | for(i=0; i < (*facecount); i++) 11 | { 12 | int tex = getTextureType(get_global_id(0) + faces[i*5] - ((*xlength)/2), get_global_id(1) + (*ymin) + faces[i*5 + 1], get_global_id(2) + faces[i*5+2]- ((*zlength)/2)); 13 | if(compatibleRotation(tex, faces[i*5 + 4], faces[i*5 + 3])==0) 14 | { 15 | foundHere=0; 16 | break; 17 | } 18 | } 19 | 20 | if(foundHere==1) 21 | { 22 | found[0] = get_global_id(0)- ((*xlength)/2); 23 | found[1] = get_global_id(1) + (*ymin); 24 | found[2] = get_global_id(2)- ((*zlength)/2); 25 | 26 | } 27 | } 28 | } 29 | 30 | int getTextureType(int x, int y, int z) 31 | { 32 | long i = (long)(x * 3129871) ^ (long)z * (long)116129781 ^ (long)y; 33 | i = i * i * (long)42317861 + i * 11; 34 | int temp = (((int)i)>>16); 35 | 36 | if(temp < 0) temp*=-1; 37 | 38 | return (temp%16); 39 | } 40 | 41 | int compatibleRotation(int generatedType, int rotation, int blockFace)//blockFace 0-5 42 | { 43 | if(generatedType == 0) 44 | { 45 | if (rotation==3) return 1; 46 | } 47 | if(generatedType == 1) 48 | { 49 | if ((rotation==3 && (blockFace==0||blockFace==4))|| 50 | (rotation==2 && (blockFace==2))|| 51 | (rotation==1 && (blockFace==1||blockFace==5))|| 52 | (rotation==0 && (blockFace==3))) return 1; 53 | } 54 | if(generatedType == 2) 55 | { 56 | if ((rotation==3 && (blockFace==0||blockFace==1))|| 57 | (rotation==1 && (blockFace!=0&&blockFace!=1))) return 1; 58 | } 59 | if(generatedType == 3) 60 | { 61 | if ((rotation==3 && (blockFace==1||blockFace==4))|| 62 | (rotation==2 && (blockFace==3))|| 63 | (rotation==1 && (blockFace==0||blockFace==5))|| 64 | (rotation==0 && (blockFace==2))) return 1; 65 | } 66 | if(generatedType == 4) 67 | { 68 | if ((rotation==3 && (blockFace!=0&&blockFace!=1))|| 69 | (rotation==2 && (blockFace==1))|| 70 | (rotation==0 && (blockFace==0))) return 1; 71 | } 72 | if(generatedType == 5) 73 | { 74 | if ((rotation==3 && (blockFace==2))|| 75 | (rotation==2 && (blockFace==5))|| 76 | (rotation==1 && (blockFace==3))|| 77 | (rotation==0 && (blockFace==0||blockFace==1||blockFace==4))) return 1; 78 | } 79 | if(generatedType == 6) 80 | { 81 | if ((rotation==1 && (blockFace!=0&&blockFace!=1))|| 82 | (rotation==2 && (blockFace==1))|| 83 | (rotation==0 && (blockFace==0))) return 1; 84 | } 85 | if(generatedType == 7) 86 | { 87 | if ((rotation==3 && (blockFace==2))|| 88 | (rotation==2 && (blockFace==4||blockFace==0||blockFace==1))|| 89 | (rotation==1 && (blockFace==3))|| 90 | (rotation==0 && (blockFace==5))) return 1; 91 | } 92 | if(generatedType == 8) 93 | { 94 | if ((rotation==1 && (blockFace==0||blockFace==1))|| 95 | (rotation==3 && (blockFace!=0&&blockFace!=1))) return 1; 96 | } 97 | if(generatedType == 9) 98 | { 99 | if ((rotation==3 && (blockFace==1||blockFace==5))|| 100 | (rotation==2 && (blockFace==3))|| 101 | (rotation==1 && (blockFace==0||blockFace==4))|| 102 | (rotation==0 && (blockFace==2))) return 1; 103 | } 104 | if(generatedType == 10) 105 | { 106 | if (rotation==1) return 1; 107 | } 108 | if(generatedType == 11) 109 | { 110 | if ((rotation==3 && (blockFace==0||blockFace==5))|| 111 | (rotation==2 && (blockFace==2))|| 112 | (rotation==1 && (blockFace==1||blockFace==4))|| 113 | (rotation==0 && (blockFace==3))) return 1; 114 | } 115 | if(generatedType == 12) 116 | { 117 | if ((rotation==3 && (blockFace!=0&&blockFace!=1))|| 118 | (rotation==2 && (blockFace==0))|| 119 | (rotation==0 && (blockFace==1))) return 1; 120 | } 121 | if(generatedType == 13) 122 | { 123 | if ((rotation==3 && (blockFace==3))|| 124 | (rotation==2 && (blockFace==0||blockFace==1||blockFace==4))|| 125 | (rotation==1 && (blockFace==2))|| 126 | (rotation==0 && (blockFace==5))) return 1; 127 | } 128 | if(generatedType == 14) 129 | { 130 | if ((rotation==1 && (blockFace!=0&&blockFace!=1))|| 131 | (rotation==2 && (blockFace==0))|| 132 | (rotation==0 && (blockFace==1))) return 1; 133 | } 134 | if(generatedType == 15) 135 | { 136 | if ((rotation==3 && (blockFace==3))|| 137 | (rotation==2 && (blockFace==5))|| 138 | (rotation==1 && (blockFace==2))|| 139 | (rotation==0 && (blockFace==4||blockFace==0||blockFace==1))) return 1; 140 | } 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /kernelFile3.cl: -------------------------------------------------------------------------------- 1 | int getTextureType(int x, int y, int z); 2 | 3 | int compatibleRotation(int generatedType, int rotation, int blockFace); 4 | 5 | kernel void kernelFile(global int *faces, global int *facecount, global int *found, global int *xlength, global int *zlength, global int *ymin) 6 | { 7 | if(found[1] == 0){ 8 | int foundHere=1; 9 | int i; 10 | for(i=0; i < (*facecount); i++) 11 | { 12 | int tex = getTextureType(get_global_id(0) + faces[i*5] - ((*xlength)/2), get_global_id(1) + (*ymin) + faces[i*5+1], get_global_id(2) + faces[i*5+2]- ((*zlength)/2)); 13 | if(compatibleRotation(tex, faces[i*5 + 4], faces[i*5 + 3])==0) 14 | { 15 | foundHere=0; 16 | break; 17 | } 18 | } 19 | 20 | if(foundHere==1) 21 | { 22 | found[0] = get_global_id(0)- ((*xlength)/2); 23 | found[1] = get_global_id(1) + (*ymin); 24 | found[2] = get_global_id(2)- ((*zlength)/2); 25 | 26 | } 27 | } 28 | } 29 | 30 | int getTextureType(int x, int y, int z) 31 | { 32 | long i = (long)(x * 3129871) ^ (long)z * (long)116129781 ^ (long)y; 33 | i = i * i * (long)42317861 + i * 11; 34 | i = i >> 16; 35 | 36 | long seed = (i ^ 0x5DEECE66D) & ((((long)1) << 48) - 1); 37 | seed = (seed * 0x5DEECE66D + 0xB) & ((((long)1) << 48) - 1); 38 | seed = (seed * 0x5DEECE66D + 0xB) & ((((long)1) << 48) - 1); 39 | 40 | int retlong = (int)(((unsigned long)seed) >> 16); 41 | 42 | if(retlong < 0) retlong*=-1; 43 | 44 | return (retlong%16); 45 | } 46 | 47 | int compatibleRotation(int generatedType, int rotation, int blockFace)//blockFace 0-5 48 | { 49 | if(generatedType == 0) 50 | { 51 | if (rotation==3) return 1; 52 | } 53 | if(generatedType == 1) 54 | { 55 | if ((rotation==3 && (blockFace==0||blockFace==4))|| 56 | (rotation==2 && (blockFace==2))|| 57 | (rotation==1 && (blockFace==1||blockFace==5))|| 58 | (rotation==0 && (blockFace==3))) return 1; 59 | } 60 | if(generatedType == 2) 61 | { 62 | if ((rotation==3 && (blockFace==0||blockFace==1))|| 63 | (rotation==1 && (blockFace!=0&&blockFace!=1))) return 1; 64 | } 65 | if(generatedType == 3) 66 | { 67 | if ((rotation==3 && (blockFace==1||blockFace==4))|| 68 | (rotation==2 && (blockFace==3))|| 69 | (rotation==1 && (blockFace==0||blockFace==5))|| 70 | (rotation==0 && (blockFace==2))) return 1; 71 | } 72 | if(generatedType == 4) 73 | { 74 | if ((rotation==3 && (blockFace!=0&&blockFace!=1))|| 75 | (rotation==2 && (blockFace==1))|| 76 | (rotation==0 && (blockFace==0))) return 1; 77 | } 78 | if(generatedType == 5) 79 | { 80 | if ((rotation==3 && (blockFace==2))|| 81 | (rotation==2 && (blockFace==5))|| 82 | (rotation==1 && (blockFace==3))|| 83 | (rotation==0 && (blockFace==0||blockFace==1||blockFace==4))) return 1; 84 | } 85 | if(generatedType == 6) 86 | { 87 | if ((rotation==1 && (blockFace!=0&&blockFace!=1))|| 88 | (rotation==2 && (blockFace==1))|| 89 | (rotation==0 && (blockFace==0))) return 1; 90 | } 91 | if(generatedType == 7) 92 | { 93 | if ((rotation==3 && (blockFace==2))|| 94 | (rotation==2 && (blockFace==4||blockFace==0||blockFace==1))|| 95 | (rotation==1 && (blockFace==3))|| 96 | (rotation==0 && (blockFace==5))) return 1; 97 | } 98 | if(generatedType == 8) 99 | { 100 | if ((rotation==1 && (blockFace==0||blockFace==1))|| 101 | (rotation==3 && (blockFace!=0&&blockFace!=1))) return 1; 102 | } 103 | if(generatedType == 9) 104 | { 105 | if ((rotation==3 && (blockFace==1||blockFace==5))|| 106 | (rotation==2 && (blockFace==3))|| 107 | (rotation==1 && (blockFace==0||blockFace==4))|| 108 | (rotation==0 && (blockFace==2))) return 1; 109 | } 110 | if(generatedType == 10) 111 | { 112 | if (rotation==1) return 1; 113 | } 114 | if(generatedType == 11) 115 | { 116 | if ((rotation==3 && (blockFace==0||blockFace==5))|| 117 | (rotation==2 && (blockFace==2))|| 118 | (rotation==1 && (blockFace==1||blockFace==4))|| 119 | (rotation==0 && (blockFace==3))) return 1; 120 | } 121 | if(generatedType == 12) 122 | { 123 | if ((rotation==3 && (blockFace!=0&&blockFace!=1))|| 124 | (rotation==2 && (blockFace==0))|| 125 | (rotation==0 && (blockFace==1))) return 1; 126 | } 127 | if(generatedType == 13) 128 | { 129 | if ((rotation==3 && (blockFace==3))|| 130 | (rotation==2 && (blockFace==0||blockFace==1||blockFace==4))|| 131 | (rotation==1 && (blockFace==2))|| 132 | (rotation==0 && (blockFace==5))) return 1; 133 | } 134 | if(generatedType == 14) 135 | { 136 | if ((rotation==1 && (blockFace!=0&&blockFace!=1))|| 137 | (rotation==2 && (blockFace==0))|| 138 | (rotation==0 && (blockFace==1))) return 1; 139 | } 140 | if(generatedType == 15) 141 | { 142 | if ((rotation==3 && (blockFace==3))|| 143 | (rotation==2 && (blockFace==5))|| 144 | (rotation==1 && (blockFace==2))|| 145 | (rotation==0 && (blockFace==4||blockFace==0||blockFace==1))) return 1; 146 | } 147 | return 0; 148 | } 149 | -------------------------------------------------------------------------------- /TextureFinder.cpp: -------------------------------------------------------------------------------- 1 | 2 | #define CL_USE_DEPRECATED_OPENCL_1_2_APIS 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int *rotate90deg(int *formation, int facecount); 10 | 11 | 12 | int main() 13 | { 14 | std::vector platforms; 15 | cl::Platform::get(&platforms); 16 | 17 | _ASSERT(platforms.size() > 0); 18 | 19 | auto platform = platforms.front(); 20 | std::vector devices; 21 | 22 | platform.getDevices(CL_DEVICE_TYPE_GPU, &devices); 23 | 24 | _ASSERT(devices.size() > 0); 25 | 26 | auto device = devices.front(); 27 | 28 | const int version = 3; //2 == 1.12 and before, 3 == 1.13 and 1.14 29 | 30 | std::string fileName; 31 | 32 | if (version == 2) 33 | fileName = "kernelFile2.cl"; 34 | else 35 | fileName = "kernelFile3.cl"; 36 | 37 | std::ifstream kernelFile(fileName); 38 | std::string src(std::istreambuf_iterator(kernelFile), (std::istreambuf_iterator())); 39 | 40 | cl::Program::Sources sources(1, std::make_pair(src.c_str(), src.length() + 1)); 41 | 42 | 43 | 44 | cl::Context context(device); 45 | cl::Program program(context, sources); 46 | 47 | auto err = program.build("-cl-std=CL1.2"); 48 | 49 | std::cout << std::to_string(err)< formation = 55 | {{0, 0, 0, 4, 3, 56 | 1, 0, 0, 4, 3, 57 | 2, 0, 0, 4, 3, 58 | 3, 0, 0, 4, 3, 59 | 4, 0, 0, 4, 3, 60 | 5, 0, 0, 4, 3, 61 | 6, 0, 0, 4, 3, 62 | 7, 0, 0, 4, 1, 63 | 64 | 0, 1, 0, 4, 2, 65 | 1, 1, 0, 4, 1, 66 | 2, 1, 0, 4, 0, 67 | 3, 1, 0, 4, 3, 68 | 4, 1, 0, 4, 0, 69 | 5, 1, 0, 4, 1, 70 | 6, 1, 0, 4, 3, 71 | 7, 1, 0, 4, 1, 72 | 73 | 0, 2, 0, 4, 1, 74 | 1, 2, 0, 4, 2, 75 | 2, 2, 0, 4, 3, 76 | 3, 2, 0, 4, 3, 77 | 4, 2, 0, 4, 2, 78 | 5, 2, 0, 4, 1, 79 | 6, 2, 0, 4, 3, 80 | 7, 2, 0, 4, 3, 81 | 82 | 0, 3, 0, 4, 3, 83 | 1, 3, 0, 4, 3, 84 | 2, 3, 0, 4, 1, 85 | 3, 3, 0, 4, 3, 86 | 4, 3, 0, 4, 1, 87 | 5, 3, 0, 4, 0, 88 | 6, 3, 0, 4, 2, 89 | 7, 3, 0, 4, 1 }} ;//x, y, z, faceid, rotation 90 | 91 | 92 | //0=top, 1=bottom, 2=west, 3=east, 4=south, 5=north 93 | 94 | bool allrot = true; 95 | 96 | 97 | std::array loc = {0, 0, 0} ; 98 | int xlength = 100000; 99 | int zlength = 100000; 100 | int yrangesize = 1; 101 | int ymin = 56; 102 | 103 | cl::Buffer facesbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * arraysize, formation.data()); 104 | cl::Buffer facecountbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &facecount); 105 | cl::Buffer resultbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * 3, loc.data()); 106 | cl::Buffer xlengthbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &xlength); 107 | cl::Buffer zlengthbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &zlength); 108 | cl::Buffer yminbuf(context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int), &ymin); 109 | 110 | 111 | cl::Kernel kernel(program, "kernelFile"); 112 | kernel.setArg(0, facesbuf); 113 | kernel.setArg(1, facecountbuf); 114 | kernel.setArg(2, resultbuf); 115 | kernel.setArg(3, xlengthbuf); 116 | kernel.setArg(4, zlengthbuf); 117 | kernel.setArg(5, yminbuf); 118 | 119 | cl::CommandQueue queue(context, device); 120 | std::cout<