Skip to content

Commit 02cad7c

Browse files
Added support to pass in texture type while creating textures. (flutter#175376)
Resolves part of flutter#145027
1 parent 48b5e23 commit 02cad7c

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

engine/src/flutter/lib/gpu/lib/src/context.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,20 @@ base class GpuContext extends NativeFieldWrapperClass1 {
117117
sampleCount = 1,
118118
TextureCoordinateSystem coordinateSystem =
119119
TextureCoordinateSystem.renderToTexture,
120+
121+
/// The type of texture to create.
122+
///
123+
/// If not specified, this will be inferred from the `sampleCount`.
124+
TextureType? textureType,
120125
bool enableRenderTargetUsage = true,
121126
bool enableShaderReadUsage = true,
122127
bool enableShaderWriteUsage = false,
123128
}) {
129+
final resolvedTextureType =
130+
textureType ??
131+
((sampleCount == 1)
132+
? TextureType.texture2D
133+
: TextureType.texture2DMultisample);
124134
Texture result = Texture._initialize(
125135
this,
126136
storageMode,
@@ -129,6 +139,7 @@ base class GpuContext extends NativeFieldWrapperClass1 {
129139
height,
130140
sampleCount,
131141
coordinateSystem,
142+
resolvedTextureType,
132143
enableRenderTargetUsage,
133144
enableShaderReadUsage,
134145
enableShaderWriteUsage,

engine/src/flutter/lib/gpu/lib/src/formats.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,17 @@ enum StencilOperation {
162162
/// Decrement the current stencil value by 1. If at zero, set to maximum.
163163
decrementWrap,
164164
}
165+
166+
enum TextureType {
167+
/// A 2-dimensional texture.
168+
texture2D,
169+
170+
/// A 2-dimensional texture with multisampling enabled.
171+
texture2DMultisample,
172+
173+
/// A cubemap texture.
174+
textureCube,
175+
176+
/// A texture sourced from an external source.
177+
textureExternalOES,
178+
}

engine/src/flutter/lib/gpu/lib/src/texture.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ base class Texture extends NativeFieldWrapperClass1 {
2121
this.height,
2222
this.sampleCount,
2323
TextureCoordinateSystem coordinateSystem,
24+
this.textureType,
2425
this.enableRenderTargetUsage,
2526
this.enableShaderReadUsage,
2627
this.enableShaderWriteUsage,
@@ -37,6 +38,7 @@ base class Texture extends NativeFieldWrapperClass1 {
3738
height,
3839
sampleCount,
3940
coordinateSystem.index,
41+
textureType.index,
4042
enableRenderTargetUsage,
4143
enableShaderReadUsage,
4244
enableShaderWriteUsage,
@@ -50,6 +52,7 @@ base class Texture extends NativeFieldWrapperClass1 {
5052
final int width;
5153
final int height;
5254
final int sampleCount;
55+
final TextureType textureType;
5356

5457
/// Enable using this texture as a render pass attachment.
5558
final bool enableRenderTargetUsage;
@@ -128,6 +131,7 @@ base class Texture extends NativeFieldWrapperClass1 {
128131
Int,
129132
Int,
130133
Int,
134+
Int,
131135
Bool,
132136
Bool,
133137
Bool,
@@ -141,6 +145,7 @@ base class Texture extends NativeFieldWrapperClass1 {
141145
int height,
142146
int sampleCount,
143147
int coordinateSystem,
148+
int textureType,
144149
bool enableRenderTargetUsage,
145150
bool enableShaderReadUsage,
146151
bool enableShaderWriteUsage,

engine/src/flutter/lib/gpu/texture.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ bool InternalFlutterGpu_Texture_Initialize(Dart_Handle wrapper,
100100
int height,
101101
int sample_count,
102102
int coordinate_system,
103+
int texture_type,
103104
bool enable_render_target_usage,
104105
bool enable_shader_read_usage,
105106
bool enable_shader_write_usage) {
@@ -119,16 +120,20 @@ bool InternalFlutterGpu_Texture_Initialize(Dart_Handle wrapper,
119120
}
120121
switch (sample_count) {
121122
case 1:
122-
desc.type = impeller::TextureType::kTexture2D;
123123
desc.sample_count = impeller::SampleCount::kCount1;
124124
break;
125125
case 4:
126-
desc.type = impeller::TextureType::kTexture2DMultisample;
127126
desc.sample_count = impeller::SampleCount::kCount4;
128127
break;
129128
default:
130129
return false;
131130
}
131+
desc.type = static_cast<impeller::TextureType>(texture_type);
132+
if (!impeller::IsMultisampleCapable(desc.type) &&
133+
desc.sample_count != impeller::SampleCount::kCount1) {
134+
return false;
135+
}
136+
132137
auto texture =
133138
gpu_context->GetContext().GetResourceAllocator()->CreateTexture(desc,
134139
true);

engine/src/flutter/lib/gpu/texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern bool InternalFlutterGpu_Texture_Initialize(
5858
int height,
5959
int sample_count,
6060
int coordinate_system,
61+
int texture_type,
6162
bool enable_render_target_usage,
6263
bool enable_shader_read_usage,
6364
bool enable_shader_write_usage);

engine/src/flutter/testing/dart/gpu_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ void main() async {
285285
expect(texture.height, 100);
286286
expect(texture.storageMode, gpu.StorageMode.hostVisible);
287287
expect(texture.sampleCount, 1);
288+
expect(texture.textureType, gpu.TextureType.texture2D);
288289
expect(texture.format, gpu.PixelFormat.r8g8b8a8UNormInt);
289290
expect(texture.enableRenderTargetUsage, true);
290291
expect(texture.enableShaderReadUsage, true);
@@ -293,6 +294,25 @@ void main() async {
293294
expect(texture.getBaseMipLevelSizeInBytes(), 40000);
294295
}, skip: !impellerEnabled);
295296

297+
test(
298+
'GpuContext.createTexture fails if invalid sampleCount and texture type is passed.',
299+
() async {
300+
try {
301+
gpu.gpuContext.createTexture(
302+
gpu.StorageMode.hostVisible,
303+
100,
304+
100,
305+
sampleCount: 4,
306+
textureType: gpu.TextureType.texture2D,
307+
);
308+
fail('Exception not thrown when creating an invalid texture.');
309+
} catch (e) {
310+
expect(e.toString(), contains('Texture creation failed'));
311+
}
312+
},
313+
skip: !impellerEnabled,
314+
);
315+
296316
test('Texture.overwrite', () async {
297317
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 2, 2);
298318

0 commit comments

Comments
 (0)