open-simplex-noise-no-allocate.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. diff -u orig/open-simplex-noise.c misc/open-simplex-noise.c
  2. --- orig/open-simplex-noise.c 2018-09-14 11:11:40.049810000 +0200
  3. +++ misc/open-simplex-noise.c 2018-09-14 11:09:39.726457000 +0200
  4. @@ -13,6 +13,11 @@
  5. * of any particular randomization library, so results
  6. * will be the same when ported to other languages.
  7. */
  8. +
  9. +// -- GODOT start --
  10. +// Modified to work without allocating memory, also removed some unused function.
  11. +// -- GODOT end --
  12. +
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. @@ -34,11 +39,12 @@
  17. #define DEFAULT_SEED (0LL)
  18. -struct osn_context {
  19. +// -- GODOT start --
  20. +/*struct osn_context {
  21. int16_t *perm;
  22. int16_t *permGradIndex3D;
  23. -};
  24. -
  25. +};*/
  26. +// -- GODOT end --
  27. #define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))
  28. /*
  29. @@ -126,7 +132,9 @@
  30. int xi = (int) x;
  31. return x < xi ? xi - 1 : xi;
  32. }
  33. -
  34. +
  35. +// -- GODOT start --
  36. +/*
  37. static int allocate_perm(struct osn_context *ctx, int nperm, int ngrad)
  38. {
  39. if (ctx->perm)
  40. @@ -154,18 +162,21 @@
  41. memcpy(ctx->perm, p, sizeof(*ctx->perm) * nelements);
  42. for (i = 0; i < 256; i++) {
  43. - /* Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array. */
  44. + // Since 3D has 24 gradients, simple bitmask won't work, so precompute modulo array.
  45. ctx->permGradIndex3D[i] = (int16_t)((ctx->perm[i] % (ARRAYSIZE(gradients3D) / 3)) * 3);
  46. }
  47. return 0;
  48. }
  49. +*/
  50. +// -- GODOT end --
  51. /*
  52. * Initializes using a permutation array generated from a 64-bit seed.
  53. * Generates a proper permutation (i.e. doesn't merely perform N successive pair
  54. * swaps on a base array). Uses a simple 64-bit LCG.
  55. */
  56. -int open_simplex_noise(int64_t seed, struct osn_context **ctx)
  57. +// -- GODOT start --
  58. +int open_simplex_noise(int64_t seed, struct osn_context *ctx)
  59. {
  60. int rc;
  61. int16_t source[256];
  62. @@ -174,20 +185,9 @@
  63. int16_t *permGradIndex3D;
  64. int r;
  65. - *ctx = (struct osn_context *) malloc(sizeof(**ctx));
  66. - if (!(*ctx))
  67. - return -ENOMEM;
  68. - (*ctx)->perm = NULL;
  69. - (*ctx)->permGradIndex3D = NULL;
  70. -
  71. - rc = allocate_perm(*ctx, 256, 256);
  72. - if (rc) {
  73. - free(*ctx);
  74. - return rc;
  75. - }
  76. -
  77. - perm = (*ctx)->perm;
  78. - permGradIndex3D = (*ctx)->permGradIndex3D;
  79. + perm = ctx->perm;
  80. + permGradIndex3D = ctx->permGradIndex3D;
  81. +// -- GODOT end --
  82. for (i = 0; i < 256; i++)
  83. source[i] = (int16_t) i;
  84. @@ -206,6 +206,8 @@
  85. return 0;
  86. }
  87. +// -- GODOT start --
  88. +/*
  89. void open_simplex_noise_free(struct osn_context *ctx)
  90. {
  91. if (!ctx)
  92. @@ -220,6 +222,8 @@
  93. }
  94. free(ctx);
  95. }
  96. +*/
  97. +// -- GODOT end --
  98. /* 2D OpenSimplex (Simplectic) Noise. */
  99. double open_simplex_noise2(struct osn_context *ctx, double x, double y)
  100. diff -u orig/open-simplex-noise.h misc/open-simplex-noise.h
  101. --- orig/open-simplex-noise.h 2018-09-14 11:11:19.659807000 +0200
  102. +++ misc/open-simplex-noise.h 2018-09-14 11:10:05.006460000 +0200
  103. @@ -35,11 +35,18 @@
  104. extern "C" {
  105. #endif
  106. -struct osn_context;
  107. +// -- GODOT start --
  108. +// Modified to work without allocating memory, also removed some unused function.
  109. -int open_simplex_noise(int64_t seed, struct osn_context **ctx);
  110. +struct osn_context {
  111. + int16_t perm[256];
  112. + int16_t permGradIndex3D[256];
  113. +};
  114. +
  115. +int open_simplex_noise(int64_t seed, struct osn_context *ctx);
  116. +//int open_simplex_noise_init_perm(struct osn_context *ctx, int16_t p[], int nelements);
  117. +// -- GODOT end --
  118. void open_simplex_noise_free(struct osn_context *ctx);
  119. -int open_simplex_noise_init_perm(struct osn_context *ctx, int16_t p[], int nelements);
  120. double open_simplex_noise2(struct osn_context *ctx, double x, double y);
  121. double open_simplex_noise3(struct osn_context *ctx, double x, double y, double z);
  122. double open_simplex_noise4(struct osn_context *ctx, double x, double y, double z, double w);