-
-
Notifications
You must be signed in to change notification settings - Fork 914
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
feat: Refactor shader uniform binding to support shader arrays [flame_3d] #3282
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
This file contains 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 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 |
---|---|---|
|
@@ -15,8 +15,8 @@ abstract class Material extends Resource<gpu.RenderPipeline> { | |
_fragmentShader = fragmentShader, | ||
super( | ||
gpu.gpuContext.createRenderPipeline( | ||
vertexShader.resource, | ||
fragmentShader.resource, | ||
vertexShader.compile().resource, | ||
fragmentShader.compile().resource, | ||
), | ||
); | ||
|
||
|
@@ -25,8 +25,8 @@ abstract class Material extends Resource<gpu.RenderPipeline> { | |
var resource = super.resource; | ||
if (_recreateResource) { | ||
resource = super.resource = gpu.gpuContext.createRenderPipeline( | ||
_vertexShader.resource, | ||
_fragmentShader.resource, | ||
_vertexShader.compile().resource, | ||
_fragmentShader.compile().resource, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm open to any other cleaner ways to make this testable |
||
); | ||
_recreateResource = false; | ||
} | ||
|
This file contains 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 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 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
89 changes: 89 additions & 0 deletions
89
packages/flame_3d/lib/src/resources/shader/uniform_array.dart
This file contains 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,89 @@ | ||
import 'dart:collection'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flame_3d/graphics.dart'; | ||
import 'package:flame_3d/resources.dart'; | ||
|
||
typedef UniformArrayKey = ({ | ||
int idx, | ||
String field, | ||
}); | ||
|
||
/// {@template uniform_value} | ||
/// Instance of a uniform array. Represented by a [ByteBuffer]. | ||
/// {@endtemplate} | ||
class UniformArray extends UniformInstance<UniformArrayKey, ByteBuffer> { | ||
/// {@macro uniform_value} | ||
UniformArray(super.slot); | ||
|
||
final List<Map<int, ({int hash, List<double> data})>> _storage = []; | ||
|
||
@override | ||
ByteBuffer? get resource { | ||
if (super.resource == null) { | ||
final data = <double>[]; | ||
for (final element in _storage) { | ||
var previousIndex = -1; | ||
for (final entry in element.entries) { | ||
if (previousIndex + 1 != entry.key) { | ||
final field = slot.fields.indexed | ||
.firstWhere((e) => e.$1 == previousIndex + 1); | ||
throw StateError( | ||
'Uniform ${slot.name}.${field.$2} was not set', | ||
); | ||
} | ||
previousIndex = entry.key; | ||
data.addAll(entry.value.data); | ||
} | ||
} | ||
super.resource = Float32List.fromList(data).buffer; | ||
} | ||
|
||
return super.resource; | ||
} | ||
|
||
Map<int, ({int hash, List<double> data})> _get(int idx) { | ||
while (idx >= _storage.length) { | ||
_storage.add(HashMap()); | ||
} | ||
return _storage[idx]; | ||
} | ||
|
||
List<double>? get(int idx, String key) => _get(idx)[slot.indexOf(key)]?.data; | ||
|
||
@override | ||
void set(UniformArrayKey key, ByteBuffer buffer) { | ||
final storage = _get(key.idx); | ||
final index = slot.indexOf(key.field); | ||
|
||
// Ensure that we are only setting new data if the hash has changed. | ||
final data = buffer.asFloat32List(); | ||
final hash = Object.hashAll(data); | ||
if (storage[index]?.hash == hash) { | ||
return; | ||
} | ||
|
||
// Store the storage at the given slot index. | ||
storage[index] = (data: data, hash: hash); | ||
|
||
// Clear the cache. | ||
super.resource = null; | ||
} | ||
|
||
@override | ||
UniformArrayKey makeKey(int? idx, String? field) { | ||
if (idx == null) { | ||
throw StateError('idx is required for ${slot.name}'); | ||
} | ||
if (field == null) { | ||
throw StateError('field is required for ${slot.name}'); | ||
} | ||
|
||
return (idx: idx, field: field); | ||
} | ||
|
||
@override | ||
void bind(GraphicsDevice device) { | ||
device.bindUniform(slot.resource!, resource!); | ||
} | ||
} |
This file contains 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 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 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.
unrelated DCM