#include "noise_perlin.cuh"

Perlin::Perlin() :
    NoiseModule(),
    m_frequency    (DEFAULT_PERLIN_FREQUENCY   ),
    m_lacunarity   (DEFAULT_PERLIN_LACUNARITY  ),
    m_octaveCount  (DEFAULT_PERLIN_OCTAVE_COUNT),
    m_persistence  (DEFAULT_PERLIN_PERSISTENCE ),
    m_seed         (DEFAULT_PERLIN_SEED) {
}

void Perlin::SetFrequency(double frequency) {
    m_frequency = frequency;
}

void Perlin::SetLacunarity(double lacunarity) {
    m_lacunarity = lacunarity;
}

void Perlin::SetOctaveCount(int octaveCount) {
    m_octaveCount = octaveCount;
}

void Perlin::SetPersistence(double persistence) {
    m_persistence = persistence;
}

void Perlin::SetSeed(int seed) {
    m_seed = seed;
}

double Perlin::GetValue(double x, double y, double z) {
    double value = 0.0;
    double signal = 0.0;
    double curPersistence = 1.0;
    double nx, ny, nz;
    int seed;

    x *= m_frequency;
    y *= m_frequency;
    z *= m_frequency;

    for (int curOctave = 0; curOctave < m_octaveCount; curOctave++) {
        
        // Make sure that these floating-point values have the same range as a 32-
        // bit integer so that we can pass them to the coherent-noise functions.
        nx = MakeInt32Range(x);
        ny = MakeInt32Range(y);
        nz = MakeInt32Range(z);

        // Get the coherent-noise value from the input value and add it to the
        // final result.
        seed = (m_seed + curOctave) & 0xffffffff;
        signal = GradientCoherentNoise3D (nx, ny, nz, seed);
        value += signal * curPersistence;

        // Prepare the next octave.
        x *= m_lacunarity;
        y *= m_lacunarity;
        z *= m_lacunarity;
        curPersistence *= m_persistence;
    }

    return value;
}

	.file	"tmpxft_00200fd0_00000000-6_noise_perlin.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2019:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2019:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.globl	_ZN6PerlinC2Ev
	.type	_ZN6PerlinC2Ev, @function
_ZN6PerlinC2Ev:
.LFB2009:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	call	_ZN11NoiseModuleC2Ev@PLT
	movq	.LC0(%rip), %rax
	movq	$6, 24(%rbx)
	movq	%rax, (%rbx)
	movq	.LC1(%rip), %rax
	movq	%rax, 8(%rbx)
	movq	.LC2(%rip), %rax
	movq	%rax, 16(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2009:
	.size	_ZN6PerlinC2Ev, .-_ZN6PerlinC2Ev
	.globl	_ZN6PerlinC1Ev
	.set	_ZN6PerlinC1Ev,_ZN6PerlinC2Ev
	.align 2
	.globl	_ZN6Perlin12SetFrequencyEd
	.type	_ZN6Perlin12SetFrequencyEd, @function
_ZN6Perlin12SetFrequencyEd:
.LFB2011:
	.cfi_startproc
	endbr64
	movsd	%xmm0, (%rdi)
	ret
	.cfi_endproc
.LFE2011:
	.size	_ZN6Perlin12SetFrequencyEd, .-_ZN6Perlin12SetFrequencyEd
	.align 2
	.globl	_ZN6Perlin13SetLacunarityEd
	.type	_ZN6Perlin13SetLacunarityEd, @function
_ZN6Perlin13SetLacunarityEd:
.LFB2012:
	.cfi_startproc
	endbr64
	movsd	%xmm0, 8(%rdi)
	ret
	.cfi_endproc
.LFE2012:
	.size	_ZN6Perlin13SetLacunarityEd, .-_ZN6Perlin13SetLacunarityEd
	.align 2
	.globl	_ZN6Perlin14SetOctaveCountEi
	.type	_ZN6Perlin14SetOctaveCountEi, @function
_ZN6Perlin14SetOctaveCountEi:
.LFB2013:
	.cfi_startproc
	endbr64
	movl	%esi, 24(%rdi)
	ret
	.cfi_endproc
.LFE2013:
	.size	_ZN6Perlin14SetOctaveCountEi, .-_ZN6Perlin14SetOctaveCountEi
	.align 2
	.globl	_ZN6Perlin14SetPersistenceEd
	.type	_ZN6Perlin14SetPersistenceEd, @function
_ZN6Perlin14SetPersistenceEd:
.LFB2014:
	.cfi_startproc
	endbr64
	movsd	%xmm0, 16(%rdi)
	ret
	.cfi_endproc
.LFE2014:
	.size	_ZN6Perlin14SetPersistenceEd, .-_ZN6Perlin14SetPersistenceEd
	.align 2
	.globl	_ZN6Perlin7SetSeedEi
	.type	_ZN6Perlin7SetSeedEi, @function
_ZN6Perlin7SetSeedEi:
.LFB2015:
	.cfi_startproc
	endbr64
	movl	%esi, 28(%rdi)
	ret
	.cfi_endproc
.LFE2015:
	.size	_ZN6Perlin7SetSeedEi, .-_ZN6Perlin7SetSeedEi
	.align 2
	.globl	_ZN6Perlin8GetValueEddd
	.type	_ZN6Perlin8GetValueEddd, @function
_ZN6Perlin8GetValueEddd:
.LFB2016:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movaps	%xmm0, %xmm3
	xorps	%xmm4, %xmm4
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdi, %rbx
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	movsd	(%rdi), %xmm0
	movsd	.LC0(%rip), %xmm5
	mulsd	%xmm0, %xmm1
	mulsd	%xmm0, %xmm3
	mulsd	%xmm2, %xmm0
	movq	%xmm1, %r14
	movsd	%xmm0, 8(%rsp)
.L10:
	cmpl	%ebp, 24(%rbx)
	jle	.L14
	movaps	%xmm3, %xmm0
	movq	%rbx, %rdi
	movsd	%xmm4, 40(%rsp)
	movsd	%xmm5, 32(%rsp)
	movsd	%xmm3, 24(%rsp)
	call	_ZN11NoiseModule14MakeInt32RangeEd@PLT
	movq	%rbx, %rdi
	movq	%xmm0, %r15
	movq	%r14, %xmm0
	call	_ZN11NoiseModule14MakeInt32RangeEd@PLT
	movq	%rbx, %rdi
	movsd	%xmm0, 16(%rsp)
	movsd	8(%rsp), %xmm0
	call	_ZN11NoiseModule14MakeInt32RangeEd@PLT
	movl	28(%rbx), %esi
	movsd	16(%rsp), %xmm1
	movq	%rbx, %rdi
	movaps	%xmm0, %xmm2
	movq	%r15, %xmm0
	addl	%ebp, %esi
	incl	%ebp
	call	_ZN11NoiseModule23GradientCoherentNoise3DEdddi@PLT
	movsd	32(%rsp), %xmm5
	movsd	40(%rsp), %xmm4
	movq	%r14, %xmm6
	movsd	24(%rsp), %xmm3
	mulsd	%xmm5, %xmm0
	mulsd	16(%rbx), %xmm5
	addsd	%xmm0, %xmm4
	movsd	8(%rbx), %xmm0
	mulsd	%xmm0, %xmm6
	mulsd	%xmm0, %xmm3
	mulsd	8(%rsp), %xmm0
	movq	%xmm6, %r14
	movsd	%xmm0, 8(%rsp)
	jmp	.L10
.L14:
	addq	$56, %rsp
	.cfi_def_cfa_offset 40
	movaps	%xmm4, %xmm0
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2016:
	.size	_ZN6Perlin8GetValueEddd, .-_ZN6Perlin8GetValueEddd
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2042:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2042:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1072693248
	.align 8
.LC1:
	.long	0
	.long	1073741824
	.align 8
.LC2:
	.long	0
	.long	1071644672
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#pragma once
#include <hip/hip_runtime.h>


#include "noise_module_base.cuh"

// Define Perlin Module default parameters
#define DEFAULT_PERLIN_FREQUENCY 1.0
#define DEFAULT_PERLIN_LACUNARITY 2.0
#define DEFAULT_PERLIN_OCTAVE_COUNT 6
#define DEFAULT_PERLIN_PERSISTENCE 0.5
#define DEFAULT_PERLIN_SEED 0
#define PERLIN_MAX_OCTAVE 30

class Perlin: public NoiseModule {
public:
    CUDA_CALLABLE Perlin();
    CUDA_CALLABLE void SetFrequency(double frequency);
    CUDA_CALLABLE void SetLacunarity(double lacunarity);
    CUDA_CALLABLE void SetPersistence(double persistence);
    CUDA_CALLABLE void SetOctaveCount(int octaveCount);
    CUDA_CALLABLE void SetSeed(int seed);

    CUDA_CALLABLE double GetValue(double x, double y, double z);

protected:
    double m_frequency, m_lacunarity, m_persistence;
    int m_octaveCount, m_seed;
};

	.text
	.file	"noise_perlin.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
