Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
Cargo.lock
41 changes: 41 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.19)

set(CURRENT_PROJECT "OpenSimplex2Interface")

option(OPENSIMPLEX_C "Enable/Disable build of the old C implementation" OFF)
option(OPENSIMPLEX_RUST "Enable/Disable build of the main Rust implementation" ON)


add_library(${CURRENT_PROJECT} INTERFACE)

if (OPENSIMPLEX_RUST)

include(FetchContent)

FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.5.2

)
FetchContent_MakeAvailable(Corrosion)
corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml ALL_FEATURES)

target_link_libraries(${CURRENT_PROJECT} INTERFACE "opensimplex2")
target_include_directories(${CURRENT_PROJECT} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

endif()

if (OPENSIMPLEX_C)

set(SOURCE_CPP
"${CMAKE_CURRENT_SOURCE_DIR}/_old/c/OpenSimplex2F.c"
"${CMAKE_CURRENT_SOURCE_DIR}/_old/c/OpenSimplex2F.h"
)

add_library(${CURRENT_PROJECT}C ${SOURCE_CPP})
target_include_directories(${CURRENT_PROJECT}C PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/_old/")
target_compile_options(${CURRENT_PROJECT}C PRIVATE -Wall -Wextra --pedantic)
target_link_libraries(${CURRENT_PROJECT} INTERFACE ${CURRENT_PROJECT}C)

endif()
19 changes: 19 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "opensimplex2"
version = "1.1.0"
edition = "2021"
rust-version = "1.70.0"

description = "Port of OpenSimplex2"
authors = ["KdotJPG"]
Expand All @@ -19,3 +20,21 @@ include = [
[lib]
path = "rust/lib.rs"
crate-type = ["rlib", "staticlib", "cdylib"]

[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
panic = "abort"

[features]
default = []
std = []

[lints]
clippy.excessive_precision = "allow"
clippy.identity_op = "allow"
clippy.too_many_arguments = "allow"
clippy.approx_constant = "allow"
clippy.suboptimal_flops = "allow"

39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,55 @@ Gradient vector tables were also revisited to improve probability symmetry in bo
Note: area-generators have been moved to [their original repository](https://github.com/KdotJPG/Noise-VertexQueue-AreaGen).

## C FFI usage

### Monual usage
Install a [Rust toolchain](https://www.rust-lang.org/tools/install) if needed.

Build debug or release artifacts with:
```
cargo build
# or
cargo build --release
cargo build --release --features std
```
Library files will be in `./target/{debug,release}/`. Copy header from [`./rust/OpenSimplex2.h`](./rust/OpenSimplex2.h).

### CMake

To include the library in your own CMake project, simply add the subdirectory and link the OpenSimplex2 interface target:

```cmake
add_subdirectory(submodules/OpenSimplex2)
target_link_libraries(${CURRENT_PROJECT} PUBLIC OpenSimplex2Interface)
```

The OpenSimplex2 interface provides two implementation backends (C and Rust).
By default, only the Rust backend is compiled.
To control which backend should be used, configure the following options:

* OPENSIMPLEX_C — OFF by default
* OPENSIMPLEX_RUST — ON by default

You can enable both backends simultaneously if needed.
To override the default options, use:

```cmake
option(OPENSIMPLEX_C "Enable/Disable build of the old C implementation" OFF)
option(OPENSIMPLEX_RUST "Enable/Disable build of the main Rust implementation" ON)
```

### Including in C/C++ Projects

``` cpp
#include <c/OpenSimplex2F.h> // if the OPENSIMPLEX_C option enabled
#include <rust/OpenSimplex2.h> // if the OPENSIMPLEX_RUST option enabled
```

**Note:**
The C and Rust implementations use different function names to call the noise generators.

## Changelog

* Rust implementation optimisations and CMake Inteface implementation (Nov 12, 2025)
* Tuned up this `README.md`. (Mar 26, 2022)
* Re-wrote functions to be instancelessly seedable and less dependent on lookup tables. Re-organized repository. Renamed `OpenSimplex2F` to just `OpenSimplex2` in file/class names. (Jan 16, 2022)
* Shortened lookup table for Simplex/OpenSimplex2(F) 4D (July 5, 2020)
Expand Down
23 changes: 23 additions & 0 deletions examples/seed_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::time::Instant;

fn main() {
const SEED: i64 = 0;

let _ = opensimplex2::fast::noise2(SEED, 0.0, 0.0);

for _ in 0..10 {
let iteration_time = Instant::now();
let mut res = 0.0; // To not optimize away the loop

for x in 0..8000 {
for y in 0..8000 {
res += opensimplex2::fast::noise2(SEED, x as f64, y as f64);
}
}

println!(
"Rust Impl 2D: {} msec (Res: {res})",
iteration_time.elapsed().as_millis()
);
}
}
10 changes: 9 additions & 1 deletion rust/OpenSimplex2.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once
#ifndef OPENSIMPLEX2_H
#define OPENSIMPLEX2_H

#ifdef __cplusplus
extern "C" {
#endif

extern float opensimplex2_fast_noise2(long long seed, double x, double y);
extern float opensimplex2_fast_noise2_ImproveX(long long seed, double x, double y);
Expand All @@ -22,4 +26,8 @@ extern float opensimplex2_smooth_noise4_ImproveXYZ(long long seed, double x, dou
extern float opensimplex2_smooth_noise4_ImproveXY_ImproveZW(long long seed, double x, double y, double z, double w);
extern float opensimplex2_smooth_noise4_Fallback(long long seed, double x, double y, double z, double w);

#ifdef __cplusplus
}
#endif

#endif
Loading