Generators.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "Generators.h"
  2. #include "GenericMandelbrot.h"
  3. #include "Fixed.h"
  4. #include <iostream>
  5. #include <iterator>
  6. using namespace cl;
  7. Platform getPlatform() {
  8. /* Returns the first platform found. */
  9. std::vector<Platform> all_platforms;
  10. Platform::get(&all_platforms);
  11. if (all_platforms.size()==0) {
  12. std::cout << "No platforms found. Check OpenCL installation!\n";
  13. exit(1);
  14. }
  15. return all_platforms[0];
  16. }
  17. Device getDevice(Platform platform, int i, bool display = false) {
  18. /* Returns the deviced specified by the index i on platform.
  19. * If display is true, then all of the platforms are listed.
  20. */
  21. std::vector<Device> all_devices;
  22. platform.getDevices(CL_DEVICE_TYPE_GPU, &all_devices);
  23. if (all_devices.size() == 0) {
  24. std::cout << "No devices found. Check OpenCL installation!\n";
  25. exit(1);
  26. }
  27. if (display) {
  28. for (::size_t j = 0; j < all_devices.size(); j++) {
  29. printf("Device %d: %s\n", int(j), all_devices[j].getInfo<CL_DEVICE_NAME>().c_str());
  30. printf("preferred float width: %d\n", all_devices[j].getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  31. printf("vendor: %s\n", all_devices[j].getInfo<CL_DEVICE_VENDOR>().c_str());
  32. }
  33. }
  34. return all_devices[i];
  35. }
  36. ClGenerator::ClGenerator(void)
  37. {
  38. Platform p = getPlatform();
  39. device = getDevice(p, 0, true);
  40. context = Context{ device };
  41. Program::Sources sources;
  42. std::string kcode;
  43. // TODO check for overflow
  44. if (false && device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() == 4) {
  45. kcode =
  46. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScaleX, float pixelScaleY, int max) {\n"
  47. " int index = get_global_id(0) * 4;\n"
  48. " int x = index % (width);\n"
  49. " int y = index / (width);\n"
  50. " float4 av = (float4)(x * pixelScaleX + xl, (x + 1) * pixelScaleX + xl, (x + 2) * pixelScaleX + xl, (x + 3) * pixelScaleX + xl);\n"
  51. // "(x + 4) * pixelScale + xl, (x + 5) * pixelScale + xl, (x + 6) * pixelScale + xl, (x + 7) * pixelScale + xl);\n"
  52. " float4 bv = (float4)(y * pixelScaleY + yt);\n"
  53. " float4 ca = av;\n"
  54. " float4 cb = bv;\n"
  55. ""
  56. " int4 counter = (int4) 1;"
  57. " float4 threshold = (float4) 16;"
  58. " int n = 0;\n"
  59. " while (n < max) {\n"
  60. " float4 aa = av * av;\n"
  61. " float4 bb = bv * bv;\n"
  62. " float4 ab = av * bv;\n"
  63. " av = aa - bb + ca;\n"
  64. " bv = 2 * ab + cb;\n"
  65. " counter += -(threshold > (aa + bb));\n"
  66. " if(all(threshold < (aa + bb))) break;\n"
  67. " //if (aa + bb > 16) break;\n"
  68. " n++;\n"
  69. " }\n\n"
  70. " A[index] = (float) counter[0];\n"
  71. " A[index + 1] = (float) counter[1];\n"
  72. " A[index + 2] = (float) counter[2];\n"
  73. " A[index + 3] = (float) counter[3];\n"
  74. /* " A[index + 4] = (float) counter[4];\n"
  75. " A[index + 5] = (float) counter[5];\n"
  76. " A[index + 6] = (float) counter[6];\n"
  77. " A[index + 7] = (float) counter[7];\n"*/
  78. // " A[get_global_id(0)] = 1;\n"
  79. "}\n";
  80. }
  81. else {
  82. kcode =
  83. // "#pragma OPENCL EXTENSION cl_khr_fp64 : enable"
  84. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScaleX, float pixelScaleY, int max) {"
  85. " int index = get_global_id(0);\n"
  86. " int x = index % width;"
  87. " int y = index / width;"
  88. " float a = x * pixelScaleX + xl;"
  89. " float b = y * pixelScaleY + yt;"
  90. " float ca = a;"
  91. " float cb = b;"
  92. ""
  93. " int n = 0;"
  94. " while (n < max) {"
  95. " float aa = a * a;"
  96. " float bb = b * b;"
  97. " float ab = a * b;"
  98. " if (aa + bb > 16) break;"
  99. " a = aa - bb + ca;"
  100. " b = 2 * ab + cb;"
  101. " n++;"
  102. " }\n"
  103. // N + 1 - log (log |Z(N)|) / log 2
  104. " A[index] = ((float)n) + 1 - log(log(a * a + b * b) / 2) / log(2.0f);\n"
  105. // " A[index] = ((float)n) + 1 - (a * a + b * b - 16) / (256 - 16);\n"
  106. // " A[get_global_id(0)] = 5;"
  107. "}";
  108. }
  109. sources.push_back({ kcode.c_str(), kcode.length() });
  110. program = Program{ context, sources };
  111. if (program.build({ device }) != CL_SUCCESS) {
  112. std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
  113. exit(1);
  114. }
  115. queue = CommandQueue(context, device);
  116. }
  117. /*Bitmap<RGBColor> ClGenerator::generate(const MandelInfo& info)
  118. {
  119. return enqueueMandelbrot(info.bWidth, info.bHeight, info.view.x, info.view.y, info.view.width).get();
  120. }*/
  121. Bitmap<float> ClGenerator::generateRaw(const MandelInfo& info)
  122. {
  123. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  124. Bitmap<float> bitmap{ info.bWidth, info.bHeight };
  125. Buffer buffer_A(context, CL_MEM_READ_WRITE, bufferSize);
  126. float pixelScaleX = info.view.width / info.bWidth;
  127. float pixelScaleY = info.view.height / info.bHeight;
  128. Kernel iterate = Kernel(program, "iterate");
  129. iterate.setArg(0, buffer_A);
  130. iterate.setArg(1, int(info.bWidth));
  131. iterate.setArg(2, float(info.view.x));
  132. iterate.setArg(3, float(info.view.y));
  133. iterate.setArg(4, float(pixelScaleX));
  134. iterate.setArg(5, float(pixelScaleY));
  135. iterate.setArg(6, int(info.maxIter));
  136. // TODO check for overflow
  137. if (false && device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() == 4) {
  138. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight / 4));
  139. } else {
  140. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  141. }
  142. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  143. return bitmap;
  144. }
  145. std::future<Bitmap<RGBColor>> ClGenerator::enqueueMandelbrot(long width, long height, float x, float y, float fwidth)
  146. {
  147. x = x - fwidth / 2;
  148. y = y - fwidth * height / width / 2;
  149. auto mandelCreator = [width, height, x, y, fwidth, this] () -> Bitmap<RGBColor> {
  150. ::size_t bufferSize = width * height * sizeof(float);
  151. Bitmap<float> bitmap{ width, height };
  152. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  153. //CommandQueue queue(context, device);
  154. //queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, bufferSize, A);
  155. /*float x = -2.3;
  156. float y = -1.5;*/
  157. float pixelScale = fwidth / width;
  158. Kernel iterate = Kernel(program, "iterate");
  159. iterate.setArg(0, buffer_A);
  160. iterate.setArg(1, width);
  161. iterate.setArg(2, x);
  162. iterate.setArg(3, y);
  163. iterate.setArg(4, pixelScale);
  164. queue.enqueueNDRangeKernel(iterate, NullRange, NDRange(width * height), NDRange(32));
  165. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  166. auto converted = bitmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  167. return converted;
  168. };
  169. //return std::future<Bitmap<RGBColor>(mandelCreator(), );
  170. return std::async(/*std::launch::deferred,*/ mandelCreator);
  171. }
  172. /*
  173. std::future<Bitmap<RGBColor>> createMandelbrot()
  174. {
  175. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  176. Bitmap<int> bitmap{1024, 1024};
  177. calculateMandel(bitmap);
  178. return bitmap.map<RGBColor>([](int x) { return RGBColor{ unsigned char(x), unsigned char(x), unsigned char(x) }; });
  179. };
  180. return std::async(mandelCreator);
  181. }
  182. */
  183. std::future<Bitmap<RGBColor>> createHPM()
  184. {
  185. /*auto mandelCreator = [] () -> Bitmap<RGBColor> {
  186. Fixed128 smallFact { 10000ULL, 0 };
  187. Bitmap<float> bitmap{ 128, 128 };
  188. for (::size_t y = 0; y < bitmap.height; y++) {
  189. for (::size_t x = 0; x < bitmap.width; x++) {
  190. Fixed128 a = Fixed128(x) * smallFact;
  191. Fixed128 b = Fixed128(y) * smallFact;
  192. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  193. }
  194. }
  195. return bitmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  196. };*/
  197. double xx = -10.6;
  198. double yy = 4.7;
  199. Fixed128 x = xx;
  200. Fixed128 y = yy;
  201. std::cout << double(-x) << " * " << double(-y) << " = " << double(x * y) << " --> " << (xx * yy) << std::endl;
  202. //exit(0);
  203. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  204. Bitmap<float> bitmap{ 512, 512 };
  205. for (::size_t y = 0; y < bitmap.height; y++) {
  206. for (::size_t x = 0; x < bitmap.width; x++) {
  207. Fixed128 a = x * 2.0 / bitmap.width - 1;
  208. Fixed128 b = y * 2.0 / bitmap.height - 1;
  209. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  210. }
  211. }
  212. return bitmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  213. };
  214. return std::async(mandelCreator);
  215. }