Skip to content

Commit

Permalink
feat:add imutable texture flag for Webgl2
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesky013 committed Sep 11, 2023
1 parent d7f90a3 commit c1f6ba4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
1 change: 1 addition & 0 deletions cocos/gfx/base/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export enum TextureFlagBit {
EXTERNAL_NORMAL = 0x8, // External normal texture
LAZILY_ALLOCATED = 0x10, // Try lazily allocated mode.
MUTABLE_VIEW_FORMAT = 0x40, // texture view as different format
MUTABLE_STORAGE = 0x80, // mutable storage for gl
}

export enum FormatFeatureBit {
Expand Down
53 changes: 41 additions & 12 deletions cocos/gfx/webgl2/webgl2-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,57 @@
*/

import {
BufferUsageBit, ColorMask, CullMode, DynamicStateFlagBit, Filter, Format, TextureType, Type, FormatInfo,
FormatInfos, FormatSize, LoadOp, MemoryUsageBit, SampleCount, ShaderStageFlagBit, TextureFlagBit,
Color, Rect, BufferTextureCopy, BufferSource, DrawInfo, IndirectBuffer, UniformBlock, DynamicStates,
UniformSamplerTexture, alignTo, Extent, formatAlignment, getTypedArrayConstructor, Offset, TextureBlit,
alignTo,
BufferSource,
BufferTextureCopy,
BufferUsageBit,
Color,
ColorMask,
CullMode,
DrawInfo,
DynamicStateFlagBit,
DynamicStates,
Extent,
Filter,
Format,
formatAlignment,
FormatInfo,
FormatInfos,
FormatSize,
getTypedArrayConstructor,
IndirectBuffer,
LoadOp,
MemoryUsageBit,
Offset,
Rect,
SampleCount,
ShaderStageFlagBit,
TextureBlit,
TextureFlagBit,
TextureType,
Type,
UniformBlock,
UniformSamplerTexture,
} from '../base/define';
import { WebGL2EXT } from './webgl2-define';
import { WebGL2CommandAllocator } from './webgl2-command-allocator';
import { WebGL2Device } from './webgl2-device';
import {WebGL2EXT} from './webgl2-define';
import {WebGL2CommandAllocator} from './webgl2-command-allocator';
import {WebGL2Device} from './webgl2-device';
import {
IWebGL2GPUInputAssembler,
IWebGL2Attrib,
IWebGL2GPUDescriptorSet,
IWebGL2GPUBuffer,
IWebGL2GPUDescriptorSet,
IWebGL2GPUFramebuffer,
IWebGL2GPUInput,
IWebGL2GPUInputAssembler,
IWebGL2GPUPipelineState,
IWebGL2GPURenderPass,
IWebGL2GPUSampler,
IWebGL2GPUShader,
IWebGL2GPUTexture,
IWebGL2GPUUniformBlock,
IWebGL2GPUUniformSamplerTexture,
IWebGL2GPURenderPass,
} from './webgl2-gpu-objects';
import { CachedArray, error, errorID, debug, cclegacy, assertID } from '../../core';
import {assertID, CachedArray, cclegacy, debug, error, errorID} from '../../core';

const WebGLWraps: GLenum[] = [
0x2901, // WebGLRenderingContext.REPEAT
Expand Down Expand Up @@ -1087,6 +1114,8 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2Device, gpuTexture: IW
w = Math.max(1, w >> 1);
h = Math.max(1, h >> 1);
}
} else if (gpuTexture.flags & TextureFlagBit.MUTABLE_STORAGE) {
gl.texImage2D(gl.TEXTURE_2D, 0, gpuTexture.glInternalFmt, w, h, 0, gpuTexture.glFormat, gpuTexture.glType, null);
} else {
gl.texStorage2D(gl.TEXTURE_2D, gpuTexture.mipLevel, gpuTexture.glInternalFmt, w, h);
}
Expand Down Expand Up @@ -2849,7 +2878,7 @@ export function WebGL2CmdFuncCopyTexImagesToTexture (

switch (gpuTexture.glTarget) {
case gl.TEXTURE_2D: {
if (toUseTexImage2D(texImages, regions)) {
if ((gpuTexture.flags & TextureFlagBit.MUTABLE_STORAGE) || toUseTexImage2D(texImages, regions)) {
gl.texImage2D(
gl.TEXTURE_2D,
regions[0].texSubres.mipLevel,
Expand Down
1 change: 1 addition & 0 deletions native/cocos/renderer/gfx-base/GFXDef-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ enum class TextureFlagBit : uint32_t {
EXTERNAL_NORMAL = 0x8, // External normal texture
LAZILY_ALLOCATED = 0x10, // Try lazily allocated mode.
MUTABLE_VIEW_FORMAT = 0x40, // texture view as different format
MUTABLE_STORAGE = 0x80, // mutable storage for gl image
};
using TextureFlags = TextureFlagBit;
CC_ENUM_BITWISE_OPERATORS(TextureFlagBit);
Expand Down
17 changes: 16 additions & 1 deletion native/cocos/renderer/gfx-gles3/GLES3Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,12 @@ static void textureStorage(GLES3Device *device, GLES3GPUTexture *gpuTexture) {
} else {
auto target = hasFlag(gpuTexture->flags, TextureFlagBit::EXTERNAL_OES) ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
GL_CHECK(glBindTexture(target, gpuTexture->glTexture));
GL_CHECK(glTexStorage2D(target, gpuTexture->mipLevel, gpuTexture->glInternalFmt, w, h));
if (hasFlag(gpuTexture->flags, TextureFlagBit::MUTABLE_STORAGE)) {
GL_CHECK(glTexImage2D(GL_TEXTURE_2D, gpuTexture->mipLevel, gpuTexture->glInternalFmt, w, h, 0,
gpuTexture->glFormat, gpuTexture->glType, nullptr));
} else {
GL_CHECK(glTexStorage2D(GL_TEXTURE_2D, gpuTexture->mipLevel, gpuTexture->glInternalFmt, w, h));
}
}
break;
case TextureType::TEX3D:
Expand Down Expand Up @@ -2736,6 +2741,16 @@ void cmdFuncGLES3CopyBuffersToTexture(GLES3Device *device, const uint8_t *const
gpuTexture->glFormat,
memSize,
(GLvoid *)buff));
} else if (hasFlag(gpuTexture->flags, TextureFlagBit::MUTABLE_STORAGE)) {
GL_CHECK(glTexImage2D(GL_TEXTURE_2D,
gpuTexture->mipLevel,
gpuTexture->glInternalFmt,
destWidth,
destHeight,
0,
gpuTexture->glFormat,
gpuTexture->glType,
(GLvoid *)buff));
} else {
GL_CHECK(glTexSubImage2D(GL_TEXTURE_2D,
mipLevel,
Expand Down

0 comments on commit c1f6ba4

Please sign in to comment.