Skip to content

Comments

fix: create with data support mipmap#1347

Merged
yiiqii merged 3 commits intodevfrom
fix/createWithData-support-mipmap
Dec 31, 2025
Merged

fix: create with data support mipmap#1347
yiiqii merged 3 commits intodevfrom
fix/createWithData-support-mipmap

Conversation

@Fryt1
Copy link
Contributor

@Fryt1 Fryt1 commented Dec 26, 2025

支持createWithData的纹理生成mipmap

Summary by CodeRabbit

  • New Features
    • Added optional mipmap generation for data-, image-, and framebuffer-backed textures with runtime capability detection; defaults to off.
    • Ensured consistent mipmap behavior across all texture update paths and centralized decision/error handling for non-power-of-two cases.
    • Exposed a generateMipmap option when creating data-backed textures to control mipmap generation.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 26, 2025

📝 Walkthrough

Walkthrough

Adds a propagated generateMipmap option and centralizes mipmap-generation logic: GLTexture now accepts the flag for data/image sources, uses a new createMipmap(width,height) helper to decide/generate mipmaps (WebGL2 or power-of-two), logs/adjusts filters when generation isn't possible.

Changes

Cohort / File(s) Summary
GL mipmap logic
packages/effects-webgl/src/gl-texture.ts
Accepts `Texture2DSourceOptionsImage
Texture option propagation
packages/effects/src/index.ts
Destructures generateMipmap = false in Texture.createWithData and passes the flag into GL texture initialization so data-backed textures can request mipmap generation.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant App
    participant TextureAPI as Texture.createWithData
    participant GLTexture
    participant WebGL as WebGLContext

    App->>TextureAPI: createWithData(..., generateMipmap)
    TextureAPI->>GLTexture: new GLTexture(..., generateMipmap)
    GLTexture->>WebGL: texImage2D / texSubImage2D (upload data/image)
    alt generateMipmap == true
        GLTexture->>GLTexture: createMipmap(width,height) check
        alt can generate (WebGL2 or POT)
            GLTexture->>WebGL: generateMipmap()
            WebGL-->>GLTexture: mipmaps created
        else cannot generate
            GLTexture->>GLTexture: set minFilter = LINEAR (downgrade)
            GLTexture->>GLTexture: log error/info
        end
    else generateMipmap == false
        Note right of GLTexture: keep existing filters/settings
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibbled bytes and checked each size,
If WebGL2 smiled or POW2 was wise.
I spun up mips when rules aligned,
Or softened filters when they declined.
Hooray — textures cozy, trimmed, and fine.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main change: adding mipmap generation support to the createWithData functionality, which is confirmed by the file summaries and PR objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/createWithData-support-mipmap

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/effects-webgl/src/gl-texture.ts (1)

202-228: Consider adding mipmap support for cube map data textures.

The new mipmap generation logic (lines 211-227) only applies to 2D data textures. Cube map data textures (lines 202-208) don't receive mipmap generation, even when generateMipmap is set.

For consistency and completeness, consider adding similar mipmap logic for cube maps after the loop at line 208.

🔎 Suggested implementation for cube map mipmaps
       if (target === gl.TEXTURE_CUBE_MAP) {
         optionsCube.forEach((data, key) => {
           const [x, y] = this.texImage2DData(gl, gl.TEXTURE_CUBE_MAP_POSITIVE_X + key, 0, internalFormat, format, type, data as TextureDataType);
 
           width = Math.max(x, width);
           height = Math.max(y, height);
         });
+        const wantsMip =
+          source.minFilter === gl.LINEAR_MIPMAP_LINEAR ||
+          source.minFilter === gl.LINEAR_MIPMAP_NEAREST ||
+          source.minFilter === gl.NEAREST_MIPMAP_LINEAR ||
+          source.minFilter === gl.NEAREST_MIPMAP_NEAREST;
+
+        const canGenMip =
+          !!generateMipmap && ((isPowerOfTwo(width) && isPowerOfTwo(height)) || isWebGL2(gl));
+
+        if (wantsMip) {
+          if (canGenMip) {
+            gl.generateMipmap(target);
+          } else {
+            source.minFilter = gl.LINEAR;
+          }
+        }
       } else {
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 584f2e9 and 079c862.

📒 Files selected for processing (2)
  • packages/effects-webgl/src/gl-texture.ts
  • packages/effects/src/index.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-10T07:59:18.725Z
Learnt from: ChengYi996
Repo: galacean/effects-runtime PR: 1263
File: packages/effects-core/src/texture/ktx2/transcoder/binomial-workercode.ts:99-110
Timestamp: 2025-11-10T07:59:18.725Z
Learning: In packages/effects-core/src/texture/ktx2/transcoder/binomial-workercode.ts, the KTX2TargetFormat.ETC case in getTranscodeFormatFromTarget already handles ETC1 through the hasAlpha flag (returns BasisFormat.ETC1 when hasAlpha is false, BasisFormat.ETC2 when true), so a separate ETC1 case is not needed.

Applied to files:

  • packages/effects-webgl/src/gl-texture.ts
🧬 Code graph analysis (1)
packages/effects-webgl/src/gl-texture.ts (3)
packages/effects-core/src/texture/types.ts (2)
  • Texture2DSourceOptionsImage (131-136)
  • Texture2DSourceOptionsData (138-142)
packages/effects-core/src/utils/index.ts (1)
  • isPowerOfTwo (127-129)
packages/effects-core/src/render/gpu-capability.ts (1)
  • isWebGL2 (57-59)
🔇 Additional comments (3)
packages/effects/src/index.ts (2)

37-37: LGTM - Good backward compatibility.

The default value of false ensures existing code continues to work without mipmaps, which is the expected behavior.


52-52: LGTM - Proper option propagation.

The generateMipmap option is correctly passed to the GLTexture constructor, following the same pattern as other texture configuration options.

packages/effects-webgl/src/gl-texture.ts (1)

211-227: LGTM - Solid mipmap generation logic.

The implementation correctly:

  • Detects when mipmaps are required based on minFilter settings
  • Validates mipmap generation capability (power-of-two dimensions for WebGL1, or WebGL2)
  • Generates mipmaps when feasible, or gracefully downgrades to LINEAR filtering to prevent sampling errors

The fallback strategy ensures textures render correctly even when mipmap generation isn't possible.

@Fryt1 Fryt1 closed this Dec 26, 2025
@Fryt1 Fryt1 reopened this Dec 26, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
packages/effects-webgl/src/gl-texture.ts (1)

134-134: Verify that Texture2DSourceOptionsData type definition includes generateMipmap.

This is the same concern raised in the previous review: the type cast includes Texture2DSourceOptionsData, but according to the type definitions in packages/effects-core/src/texture/types.ts, only Texture2DSourceOptionsImage declares the generateMipmap property. Accessing generateMipmap from Texture2DSourceOptionsData will be undefined at runtime unless the type definition has been updated.

Please confirm that generateMipmap?: boolean has been added to the Texture2DSourceOptionsData interface.

#!/bin/bash
# Verify that Texture2DSourceOptionsData includes generateMipmap property
rg -n -A 10 "interface Texture2DSourceOptionsData" packages/effects-core/src/texture/types.ts
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 079c862 and 919d303.

📒 Files selected for processing (1)
  • packages/effects-webgl/src/gl-texture.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-10T07:59:18.725Z
Learnt from: ChengYi996
Repo: galacean/effects-runtime PR: 1263
File: packages/effects-core/src/texture/ktx2/transcoder/binomial-workercode.ts:99-110
Timestamp: 2025-11-10T07:59:18.725Z
Learning: In packages/effects-core/src/texture/ktx2/transcoder/binomial-workercode.ts, the KTX2TargetFormat.ETC case in getTranscodeFormatFromTarget already handles ETC1 through the hasAlpha flag (returns BasisFormat.ETC1 when hasAlpha is false, BasisFormat.ETC2 when true), so a separate ETC1 case is not needed.

Applied to files:

  • packages/effects-webgl/src/gl-texture.ts
🧬 Code graph analysis (1)
packages/effects-webgl/src/gl-texture.ts (3)
packages/effects-core/src/texture/types.ts (2)
  • Texture2DSourceOptionsImage (131-136)
  • Texture2DSourceOptionsData (138-142)
packages/effects-core/src/utils/index.ts (1)
  • isPowerOfTwo (127-129)
packages/effects-core/src/render/gpu-capability.ts (1)
  • isWebGL2 (57-59)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript-typescript)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
packages/effects-webgl/src/gl-texture.ts (1)

134-134: Type safety issue: generateMipmap is not defined on Texture2DSourceOptionsData.

According to the type definitions, only Texture2DSourceOptionsImage has the generateMipmap property. Accessing it from Texture2DSourceOptionsData will result in undefined at runtime, which is then defaulted to false.

Since the PR objective is to enable mipmap generation for data textures, the proper fix is to add generateMipmap?: boolean to the Texture2DSourceOptionsData interface in packages/effects-core/src/texture/types.ts.

🔎 Proposed fix: Update the type definition

In packages/effects-core/src/texture/types.ts, update the Texture2DSourceOptionsData interface:

 export interface Texture2DSourceOptionsData extends TextureOptionsBase {
   sourceType?: TextureSourceType.data,
   data: TextureDataType,
   target?: WebGLRenderingContext['TEXTURE_2D'],
+  generateMipmap?: boolean,
 }
🧹 Nitpick comments (1)
packages/effects-webgl/src/gl-texture.ts (1)

189-195: Use logger.error for consistency.

The file consistently uses logger.error for error logging (lines 147, 162, 496), but this helper uses console.error. For consistency and proper logging infrastructure, use logger.error instead.

🔎 Proposed fix
 const createMipmap = (textureWidth: number, textureHeight: number) => {
   if ((isPowerOfTwo(textureWidth) && isPowerOfTwo(textureHeight)) || isWebGL2(gl)) {
     gl.generateMipmap(target);
   } else {
-    console.error('Mipmap generation failed. Texture width and height must be power of two in WebGL1.');
+    logger.error('Mipmap generation failed. Texture width and height must be power of two in WebGL1.');
   }
 };
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 919d303 and 7b7f691.

📒 Files selected for processing (1)
  • packages/effects-webgl/src/gl-texture.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-10T07:59:18.725Z
Learnt from: ChengYi996
Repo: galacean/effects-runtime PR: 1263
File: packages/effects-core/src/texture/ktx2/transcoder/binomial-workercode.ts:99-110
Timestamp: 2025-11-10T07:59:18.725Z
Learning: In packages/effects-core/src/texture/ktx2/transcoder/binomial-workercode.ts, the KTX2TargetFormat.ETC case in getTranscodeFormatFromTarget already handles ETC1 through the hasAlpha flag (returns BasisFormat.ETC1 when hasAlpha is false, BasisFormat.ETC2 when true), so a separate ETC1 case is not needed.

Applied to files:

  • packages/effects-webgl/src/gl-texture.ts
🧬 Code graph analysis (1)
packages/effects-webgl/src/gl-texture.ts (3)
packages/effects-core/src/texture/types.ts (2)
  • Texture2DSourceOptionsImage (131-136)
  • Texture2DSourceOptionsData (138-142)
packages/effects-core/src/utils/index.ts (1)
  • isPowerOfTwo (127-129)
packages/effects-core/src/render/gpu-capability.ts (1)
  • isWebGL2 (57-59)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (2)
packages/effects-webgl/src/gl-texture.ts (2)

220-222: Good addition: enables mipmap generation for data textures.

This correctly implements the PR objective by allowing data textures to generate mipmaps when explicitly requested. The conditional check ensures mipmaps are only generated when the generateMipmap flag is set.


241-243: Good refactoring: centralizes mipmap generation logic.

The refactoring to use the createMipmap helper unifies the mipmap generation logic across different texture source types (data, image, video) and adds consistent error handling for WebGL1 non-power-of-two textures.

@yiiqii yiiqii merged commit 8b18905 into dev Dec 31, 2025
2 checks passed
@yiiqii yiiqii deleted the fix/createWithData-support-mipmap branch December 31, 2025 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants