-
Notifications
You must be signed in to change notification settings - Fork 4
Added Packed_*_Array support #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ReKylee
wants to merge
4
commits into
wardensdev:master
Choose a base branch
from
ReKylee:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
addons/compute_worker/gpu_uniforms/gpu_packed_int32_array.gd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# GLSL data type encoding: `int` | ||
|
||
extends GPUUniform | ||
class_name GPU_PackedInt32Array | ||
|
||
enum UNIFORM_TYPES{ | ||
UNIFORM_BUFFER, | ||
STORAGE_BUFFER | ||
} | ||
|
||
## The initial data supplied to the uniform | ||
@export var data: PackedInt32Array = PackedInt32Array() | ||
## The size of the array as defined in the shader. Only used if `data` is not defined. | ||
@export var array_size: int = 0 | ||
## The shader binding for this uniform | ||
@export var binding: int = 0 | ||
## Type of uniform to create. `UNIFORM_BUFFER`s cannot be altered from within the shader | ||
@export var uniform_type: UNIFORM_TYPES = UNIFORM_TYPES.UNIFORM_BUFFER | ||
|
||
var data_rid: RID = RID() | ||
var uniform: RDUniform = RDUniform.new() | ||
|
||
|
||
func initialize(rd: RenderingDevice) -> RDUniform: | ||
|
||
if data.is_empty(): | ||
if array_size > 0: | ||
data.resize(array_size) | ||
else: | ||
printerr("You must define the uniform's `data` or `array_size`.") | ||
return | ||
|
||
# Create the buffer using our initial data | ||
data_rid = create_rid(rd) | ||
|
||
# Create RDUniform object using the provided binding id and data | ||
return create_uniform() | ||
|
||
|
||
func create_uniform() -> RDUniform: | ||
|
||
match uniform_type: | ||
UNIFORM_TYPES.UNIFORM_BUFFER: | ||
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_UNIFORM_BUFFER | ||
UNIFORM_TYPES.STORAGE_BUFFER: | ||
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER | ||
|
||
uniform.binding = binding | ||
uniform.add_id(data_rid) | ||
|
||
return uniform | ||
|
||
|
||
func create_rid(rd: RenderingDevice) -> RID: | ||
|
||
var bytes = data.to_byte_array() | ||
|
||
var buffer: RID = RID() | ||
|
||
match uniform_type: | ||
UNIFORM_TYPES.UNIFORM_BUFFER: | ||
bytes = pad_byte_array_std140(bytes) | ||
buffer = rd.uniform_buffer_create(bytes.size(), bytes) | ||
UNIFORM_TYPES.STORAGE_BUFFER: | ||
buffer = rd.storage_buffer_create(bytes.size(), bytes) | ||
|
||
return buffer | ||
|
||
|
||
func get_uniform_data(rd: RenderingDevice) -> PackedInt32Array: | ||
var out := rd.buffer_get_data(data_rid) | ||
return out.to_int32_array() | ||
|
||
|
||
func set_uniform_data(rd: RenderingDevice, array: PackedInt32Array) -> void: | ||
var sb_data = array.to_byte_array() | ||
rd.buffer_update(data_rid, 0 , sb_data.size(), sb_data) | ||
|
||
|
||
func pad_byte_array_std140(arr: PackedByteArray) -> PackedByteArray: | ||
|
||
arr.resize(arr.size() * 2) | ||
var next_offset = 0 | ||
|
||
for i in range(arr.size()): | ||
if next_offset + 4 > arr.size(): | ||
break | ||
arr.encode_double(next_offset + 4, 0.0) | ||
next_offset += 16 | ||
|
||
return arr |
10 changes: 10 additions & 0 deletions
10
addons/compute_worker/gpu_uniforms/gpu_packed_int32_array.tres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[gd_resource type="Resource" script_class="GPU_packed_int32_array" load_steps=2 format=3 uid="uid://b5uu7tqhsjaam"] | ||
|
||
[ext_resource type="Script" path="res://addons/compute_worker/gpu_uniforms/gpu_packed_int32_array.gd" id="1_571yc"] | ||
|
||
[resource] | ||
script = ExtResource("1_571yc") | ||
data = null | ||
binding = 0 | ||
uniform_type = 0 | ||
alias = "" |
79 changes: 79 additions & 0 deletions
79
addons/compute_worker/gpu_uniforms/gpu_packed_vector2_array.gd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# GLSL data type encoding: `vec4[]`, `vec3[]` | ||
|
||
extends GPUUniform | ||
class_name GPU_PackedVector2Array | ||
|
||
enum UNIFORM_TYPES{ | ||
UNIFORM_BUFFER, | ||
STORAGE_BUFFER | ||
} | ||
|
||
## The initial data supplied to the uniform | ||
@export var data: PackedVector2Array = PackedVector2Array() | ||
## The size of the array as defined in the shader. Only used if `data` is not defined. | ||
@export var array_size: int = 0 | ||
## The shader binding for this uniform | ||
@export var binding: int = 0 | ||
## Type of uniform to create. `UNIFORM_BUFFER`s cannot be altered from within the shader | ||
@export var uniform_type: UNIFORM_TYPES = UNIFORM_TYPES.UNIFORM_BUFFER | ||
|
||
var data_rid: RID = RID() | ||
var uniform: RDUniform = RDUniform.new() | ||
|
||
|
||
func initialize(rd: RenderingDevice) -> RDUniform: | ||
|
||
if data.is_empty(): | ||
|
||
assert(array_size > 0, "You must define the uniform's `data` or `array_size`.") | ||
|
||
if array_size > 0: | ||
data.resize(array_size) | ||
|
||
# Create the buffer using our initial data | ||
data_rid = create_rid(rd) | ||
|
||
# Create RDUniform object using the provided binding id and data | ||
return create_uniform() | ||
|
||
|
||
func create_uniform() -> RDUniform: | ||
|
||
match uniform_type: | ||
UNIFORM_TYPES.UNIFORM_BUFFER: | ||
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_UNIFORM_BUFFER | ||
UNIFORM_TYPES.STORAGE_BUFFER: | ||
uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_STORAGE_BUFFER | ||
|
||
uniform.binding = binding | ||
uniform.add_id(data_rid) | ||
|
||
return uniform | ||
|
||
|
||
func create_rid(rd: RenderingDevice) -> RID: | ||
|
||
var bytes = vec2_array_to_byte_array(data) | ||
|
||
var buffer: RID = RID() | ||
|
||
match uniform_type: | ||
UNIFORM_TYPES.UNIFORM_BUFFER: | ||
buffer = rd.uniform_buffer_create(bytes.size(), bytes) | ||
UNIFORM_TYPES.STORAGE_BUFFER: | ||
buffer = rd.storage_buffer_create(bytes.size(), bytes) | ||
|
||
return buffer | ||
|
||
|
||
func get_uniform_data(rd: RenderingDevice) -> PackedVector2Array: | ||
var out := rd.buffer_get_data(data_rid) | ||
return byte_array_to_vec2_array(out) | ||
|
||
|
||
func set_uniform_data(rd: RenderingDevice, array: PackedVector2Array) -> void: | ||
var sb_data = vec2_array_to_byte_array(array) | ||
rd.buffer_update(data_rid, 0 , sb_data.size(), sb_data) | ||
|
||
|
||
|
11 changes: 11 additions & 0 deletions
11
addons/compute_worker/gpu_uniforms/gpu_packed_vector2_array.tres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[gd_resource type="Resource" script_class="GPU_PackedVector2Array" load_steps=2 format=3 uid="uid://c1pyh5mnrbuwm"] | ||
|
||
[ext_resource type="Script" path="res://addons/compute_worker/gpu_uniforms/gpu_packed_vector2_array.gd" id="1_cmbw3"] | ||
|
||
[resource] | ||
script = ExtResource("1_cmbw3") | ||
data = null | ||
array_size = 0 | ||
binding = 0 | ||
uniform_type = 0 | ||
alias = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TYPE_VECTOR2I
andTYPE_VECTOR2
require different alignments when inside a struct. Like with arrays, alignments for struct members are rounded up to the alignment of avec4
. So for these two types, we'll need separate functions to get the byte arrays with the extra padding (+2 components).