#include "kernel_shared.cuh"
#include "kernel_render.cuh"
#include "cuda_globals.cuh"
#include "globals.cuh"
#include "const.cuh"

static uchar4* d_displayBufferCuda = NULL;

__device__ uchar4 COLORS[] = {
    {0,   0,   0,   255}, // CELL_EMPTY
    {0,   0,   255, 255}, // CELL_ELECTRON_HEAD
    {255, 0,   0,   255}, // CELL_ELECTRON_TAIL
    {255, 255, 0,   255}, // CELL_CONDUCTOR
};

__global__ void drawCell(int width, char* field, uchar4* drawBuffer)
{
    int offset = getFieldOffsetAt(0, 0, width);
    drawBuffer[offset] = COLORS[field[offset]];
}

void runDrawCell(void)
{
    cudaGraphicsMapResources(1, &displayBufferCuda, 0);
    size_t num_bytes;
    cudaGraphicsResourceGetMappedPointer((void**)&d_displayBufferCuda, &num_bytes, displayBufferCuda);
    drawCell<<<numBlocks, threadsPerBlock>>>(width, d_outfield, d_displayBufferCuda);
    cudaGraphicsUnmapResources(1, &displayBufferCuda, 0);

    cudaDeviceSynchronize();
}

	.file	"tmpxft_0035979f_00000000-6_kernel_render.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2012:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2012:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4
	.type	_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4, @function
_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4:
.LFB2034:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movl	%edi, 28(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z8drawCelliPcP6uchar4(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L2:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4, .-_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4
	.globl	_Z8drawCelliPcP6uchar4
	.type	_Z8drawCelliPcP6uchar4, @function
_Z8drawCelliPcP6uchar4:
.LFB2035:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4
	.cfi_endproc
.LFE2035:
	.size	_Z8drawCelliPcP6uchar4, .-_Z8drawCelliPcP6uchar4
	.globl	_Z11runDrawCellv
	.type	_Z11runDrawCellv, @function
_Z11runDrawCellv:
.LFB2009:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	displayBufferCuda(%rip), %rbx
	xorl	%edx, %edx
	movl	$1, %edi
	movq	%rbx, %rsi
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	cudaGraphicsMapResources@PLT
	movq	displayBufferCuda(%rip), %rdx
	movq	%rsp, %rsi
	leaq	_ZL19d_displayBufferCuda(%rip), %rdi
	call	cudaGraphicsResourceGetMappedPointer@PLT
	movl	8+threadsPerBlock(%rip), %ecx
	xorl	%r9d, %r9d
	movq	threadsPerBlock(%rip), %rdx
	movq	numBlocks(%rip), %rdi
	movl	8+numBlocks(%rip), %esi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L9
	movq	_ZL19d_displayBufferCuda(%rip), %rdx
	movq	d_outfield(%rip), %rsi
	movl	width(%rip), %edi
	call	_Z36__device_stub__Z8drawCelliPcP6uchar4iPcP6uchar4
.L9:
	xorl	%edx, %edx
	movq	%rbx, %rsi
	movl	$1, %edi
	call	cudaGraphicsUnmapResources@PLT
	call	cudaDeviceSynchronize@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2009:
	.size	_Z11runDrawCellv, .-_Z11runDrawCellv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z8drawCelliPcP6uchar4"
.LC1:
	.string	"COLORS"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z8drawCelliPcP6uchar4(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC1(%rip), %rdx
	movl	$16, %r9d
	leaq	_ZL6COLORS(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2037:
	.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
	.local	_ZL6COLORS
	.comm	_ZL6COLORS,16,16
	.local	_ZL19d_displayBufferCuda
	.comm	_ZL19d_displayBufferCuda,8,8
	.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>


void runDrawCell(void);

	.text
	.file	"kernel_render.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
