Generators.cpp 7.8 KB

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