Generators.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_ALL, &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. }
  31. return all_devices[i];
  32. }
  33. ClGenerator::ClGenerator(void)
  34. {
  35. Platform p = getPlatform();
  36. device = getDevice(p, 0, false);
  37. context = Context{ device };
  38. Program::Sources sources;
  39. std::string kcode =
  40. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScale, int max) {"
  41. " int index = get_global_id(0);\n"
  42. " int x = index % width;"
  43. " int y = index / width;"
  44. " float a = x * pixelScale + xl;"
  45. " float b = y * pixelScale + yt;"
  46. " float ca = a;"
  47. " float cb = b;"
  48. ""
  49. " int n = 0;"
  50. " while (n < max) {"
  51. " float aa = a * a;"
  52. " float bb = b * b;"
  53. " float ab = a * b;"
  54. " if (aa + bb > 16) break;"
  55. " a = aa - bb + ca;"
  56. " b = 2 * ab + cb;"
  57. " n++;"
  58. " }\n"
  59. " A[index] = ((float)n) + 1 - (a * a + b * b - 16) / (256 - 16);\n"
  60. // " A[get_global_id(0)] = 5;"
  61. "}";
  62. std::string kcode_alt =
  63. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScale, int max) {\n"
  64. " int index = get_global_id(0) * 8;\n"
  65. " int x = index % (width);\n"
  66. " int y = index / (width);\n"
  67. " float8 av = (float8)(x * pixelScale + xl, (x + 1) * pixelScale + xl, (x + 2) * pixelScale + xl, (x + 3) * pixelScale + xl,"
  68. "(x + 4) * pixelScale + xl, (x + 5) * pixelScale + xl, (x + 6) * pixelScale + xl, (x + 7) * pixelScale + xl);\n"
  69. " float8 bv = (float8)(y * pixelScale + yt);\n"
  70. " float8 ca = av;\n"
  71. " float8 cb = bv;\n"
  72. ""
  73. " int8 counter = (int8)0;"
  74. " float8 threshold = (float8)16;"
  75. " int n = 0;\n"
  76. " while (n < max) {\n"
  77. " float8 aa = av * av;\n"
  78. " float8 bb = bv * bv;\n"
  79. " float8 ab = av * bv;\n"
  80. " av = aa - bb + ca;\n"
  81. " bv = 2 * ab + cb;\n"
  82. " counter += -(threshold > (aa + bb));\n"
  83. " if(all(threshold < (aa + bb))) break;\n"
  84. " //if (aa + bb > 16) break;\n"
  85. " n++;\n"
  86. " }\n\n"
  87. " A[index] = (float) counter[0];\n"
  88. " A[index + 1] = (float) counter[1];\n"
  89. " A[index + 2] = (float) counter[2];\n"
  90. " A[index + 3] = (float) counter[3];\n"
  91. " A[index + 4] = (float) counter[4];\n"
  92. " A[index + 5] = (float) counter[5];\n"
  93. " A[index + 6] = (float) counter[6];\n"
  94. " A[index + 7] = (float) counter[7];\n"
  95. // " A[get_global_id(0)] = 1;\n"
  96. "}\n";
  97. sources.push_back({ kcode.c_str(), kcode.length() });
  98. program = Program{ context, sources };
  99. if (program.build({ device }) != CL_SUCCESS) {
  100. std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
  101. exit(1);
  102. }
  103. queue = CommandQueue(context, device);
  104. }
  105. /*Bitmap<RGBColor> ClGenerator::generate(const MandelInfo& info)
  106. {
  107. return enqueueMandelbrot(info.bWidth, info.bHeight, info.view.x, info.view.y, info.view.width).get();
  108. }*/
  109. Bitmap<float> ClGenerator::generateRaw(const MandelInfo& info)
  110. {
  111. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  112. Bitmap<float> bitmap{ info.bWidth, info.bHeight };
  113. Buffer buffer_A(context, CL_MEM_READ_WRITE, bufferSize);
  114. float pixelScale = info.view.width / info.bWidth;
  115. Kernel iterate = Kernel(program, "iterate");
  116. iterate.setArg(0, buffer_A);
  117. iterate.setArg(1, int(info.bWidth));
  118. iterate.setArg(2, float(info.view.x));
  119. iterate.setArg(3, float(info.view.y));
  120. iterate.setArg(4, float(pixelScale));
  121. iterate.setArg(5, int(info.maxIter));
  122. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  123. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  124. return bitmap;
  125. }
  126. std::future<Bitmap<RGBColor>> ClGenerator::enqueueMandelbrot(long width, long height, float x, float y, float fwidth)
  127. {
  128. x = x - fwidth / 2;
  129. y = y - fwidth * height / width / 2;
  130. auto mandelCreator = [width, height, x, y, fwidth, this] () -> Bitmap<RGBColor> {
  131. ::size_t bufferSize = width * height * sizeof(float);
  132. Bitmap<float> bitmap{ width, height };
  133. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  134. //CommandQueue queue(context, device);
  135. //queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, bufferSize, A);
  136. /*float x = -2.3;
  137. float y = -1.5;*/
  138. float pixelScale = fwidth / width;
  139. Kernel iterate = Kernel(program, "iterate");
  140. iterate.setArg(0, buffer_A);
  141. iterate.setArg(1, width);
  142. iterate.setArg(2, x);
  143. iterate.setArg(3, y);
  144. iterate.setArg(4, pixelScale);
  145. queue.enqueueNDRangeKernel(iterate, NullRange, NDRange(width * height), NDRange(32));
  146. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  147. 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) }; });
  148. return converted;
  149. };
  150. //return std::future<Bitmap<RGBColor>(mandelCreator(), );
  151. return std::async(/*std::launch::deferred,*/ mandelCreator);
  152. }
  153. /*
  154. std::future<Bitmap<RGBColor>> createMandelbrot()
  155. {
  156. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  157. Bitmap<int> bitmap{1024, 1024};
  158. calculateMandel(bitmap);
  159. return bitmap.map<RGBColor>([](int x) { return RGBColor{ unsigned char(x), unsigned char(x), unsigned char(x) }; });
  160. };
  161. return std::async(mandelCreator);
  162. }
  163. */
  164. std::future<Bitmap<RGBColor>> createHPM()
  165. {
  166. /*auto mandelCreator = [] () -> Bitmap<RGBColor> {
  167. Fixed128 smallFact { 10000ULL, 0 };
  168. Bitmap<float> bitmap{ 128, 128 };
  169. for (::size_t y = 0; y < bitmap.height; y++) {
  170. for (::size_t x = 0; x < bitmap.width; x++) {
  171. Fixed128 a = Fixed128(x) * smallFact;
  172. Fixed128 b = Fixed128(y) * smallFact;
  173. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  174. }
  175. }
  176. 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) }; });
  177. };*/
  178. double xx = -10.6;
  179. double yy = 4.7;
  180. Fixed128 x = xx;
  181. Fixed128 y = yy;
  182. std::cout << double(-x) << " * " << double(-y) << " = " << double(x * y) << " --> " << (xx * yy) << std::endl;
  183. //exit(0);
  184. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  185. Bitmap<float> bitmap{ 512, 512 };
  186. for (::size_t y = 0; y < bitmap.height; y++) {
  187. for (::size_t x = 0; x < bitmap.width; x++) {
  188. Fixed128 a = x * 2.0 / bitmap.width - 1;
  189. Fixed128 b = y * 2.0 / bitmap.height - 1;
  190. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  191. }
  192. }
  193. 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) }; });
  194. };
  195. return std::async(mandelCreator);
  196. }