Python Script: gpu.types.GPUTexture not loading cubemap faces correctly #120068

Open
opened 2024-03-29 16:59:31 +01:00 by NathanMiller · 1 comment

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 3060 Ti/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 551.23

Blender Version
Broken: version: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: 9be62e85b727
Worked: No build found that works

Tried blender 3.3 and latest daily ( 4.2.0 Alpha, branch: main, commit date: 2024-03-29 14:05, hash: a78d4f31be67) aswell

Short description of error
I am trying to load a cubemap through a use of a gpu.types.GPUTexture in python.
Blender however only seems to have loaded the first cubemap face, regardless of the direction given in glsl.

Exact steps for others to reproduce the error
I have added a blend with the script with the issue. All faces set as red aside for the first one which is a pink color. Adjusting the direction in the glsl code texture(cube_map, vec3(0, 0, 1)) will always result in the pink color when red is expected.

Full code here.

import gpu
from gpu_extras.batch import batch_for_shader
import numpy as np

#Setup a 64x64 cubemap, all red faces expect the first one
cubemap_face_size = 64 * 64 * 4
cubemap_size = cubemap_face_size * 6 

cubemap_pixels = np.empty(cubemap_size, dtype=np.float32)
num_pixels_per_face = 64 * 64

for i in range(6):
    start_index = i * cubemap_face_size
    end_index = (i + 1) * cubemap_face_size
    if i == 0:  # First face
        cubemap_pixels[start_index:end_index] = [1.0, 0.0, 1.0, 1.0] * num_pixels_per_face   # Pink
    else:
        cubemap_pixels[start_index:end_index] = [1.0, 0.0, 0.0, 1.0] * num_pixels_per_face # Red

buffer = gpu.types.Buffer('FLOAT', cubemap_size)
buffer[:] = cubemap_pixels

#make gpu texture
gpuImage = gpu.types.GPUTexture(size=64,
                             is_cubemap=True, format='RGBA32F',
                             data=buffer)

shader_info = gpu.types.GPUShaderCreateInfo()
shader_info.push_constant('MAT4', "viewProjectionMatrix")
shader_info.vertex_in(0, 'VEC3', "position")
shader_info.fragment_out(0, 'VEC4', "FragColor")
shader_info.sampler(0, 'FLOAT_CUBE', "cube_map")

shader_info.vertex_source(
    "void main()"
    "{"
    "  gl_Position = viewProjectionMatrix * vec4(position, 1.0f);"
    "}"
)

shader_info.fragment_source(
    "void main()"
    "{"
    "  vec4 cube = texture(cube_map, vec3(0, 0, 1));" #Uses a fixed direction. Same color either direction given
    "  FragColor = vec4(cube.rgb, 1.0);"
    "}"
)

shader = gpu.shader.create_from_info(shader_info)
del shader_info

batch = batch_for_shader(shader, 'TRIS', 
{
    "position": (
    (-1, -1, 1),
    (-1, 1, 1),
    (1, 1, 1),
    
    (1, 1, 1),
    (1, -1, 1),
    (-1, -1, 1)),
})


def draw():
    matrix = bpy.context.region_data.perspective_matrix
    shader.uniform_float("viewProjectionMatrix", matrix)
    shader.uniform_sampler("cube_map", gpuImage)

    batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

image

**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 3060 Ti/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 551.23 **Blender Version** Broken: version: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: `9be62e85b727` Worked: No build found that works Tried blender 3.3 and latest daily ( 4.2.0 Alpha, branch: main, commit date: 2024-03-29 14:05, hash: `a78d4f31be67`) aswell **Short description of error** I am trying to load a cubemap through a use of a gpu.types.GPUTexture in python. Blender however only seems to have loaded the first cubemap face, regardless of the direction given in glsl. **Exact steps for others to reproduce the error** I have added a blend with the script with the issue. All faces set as red aside for the first one which is a pink color. Adjusting the direction in the glsl code `texture(cube_map, vec3(0, 0, 1))` will always result in the pink color when red is expected. Full code here. ```py import bpy import gpu from gpu_extras.batch import batch_for_shader import numpy as np #Setup a 64x64 cubemap, all red faces expect the first one cubemap_face_size = 64 * 64 * 4 cubemap_size = cubemap_face_size * 6 cubemap_pixels = np.empty(cubemap_size, dtype=np.float32) num_pixels_per_face = 64 * 64 for i in range(6): start_index = i * cubemap_face_size end_index = (i + 1) * cubemap_face_size if i == 0: # First face cubemap_pixels[start_index:end_index] = [1.0, 0.0, 1.0, 1.0] * num_pixels_per_face # Pink else: cubemap_pixels[start_index:end_index] = [1.0, 0.0, 0.0, 1.0] * num_pixels_per_face # Red buffer = gpu.types.Buffer('FLOAT', cubemap_size) buffer[:] = cubemap_pixels #make gpu texture gpuImage = gpu.types.GPUTexture(size=64, is_cubemap=True, format='RGBA32F', data=buffer) shader_info = gpu.types.GPUShaderCreateInfo() shader_info.push_constant('MAT4', "viewProjectionMatrix") shader_info.vertex_in(0, 'VEC3', "position") shader_info.fragment_out(0, 'VEC4', "FragColor") shader_info.sampler(0, 'FLOAT_CUBE', "cube_map") shader_info.vertex_source( "void main()" "{" " gl_Position = viewProjectionMatrix * vec4(position, 1.0f);" "}" ) shader_info.fragment_source( "void main()" "{" " vec4 cube = texture(cube_map, vec3(0, 0, 1));" #Uses a fixed direction. Same color either direction given " FragColor = vec4(cube.rgb, 1.0);" "}" ) shader = gpu.shader.create_from_info(shader_info) del shader_info batch = batch_for_shader(shader, 'TRIS', { "position": ( (-1, -1, 1), (-1, 1, 1), (1, 1, 1), (1, 1, 1), (1, -1, 1), (-1, -1, 1)), }) def draw(): matrix = bpy.context.region_data.perspective_matrix shader.uniform_float("viewProjectionMatrix", matrix) shader.uniform_sampler("cube_map", gpuImage) batch.draw(shader) bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW') ``` ![image](/attachments/95e5ae5b-0d52-4e02-b05b-6f22d9b36e8a)
NathanMiller added the
Type
Report
Priority
Normal
Status
Needs Triage
labels 2024-03-29 16:59:32 +01:00

I can confirm that, no mater the direction set in the fragment shader, the color retrieved of the cube_map texture is always the color of the first face (the pink one).

vec4 cube = texture(cube_map, vec3(0, 0, 1));

@fclem, can you identify if there is something in the script that could result in this behavior or if it is really a bug?

I can confirm that, no mater the direction set in the fragment shader, the color retrieved of the cube_map texture is always the color of the first face (the pink one). ```glsl vec4 cube = texture(cube_map, vec3(0, 0, 1)); ``` @fclem, can you identify if there is something in the script that could result in this behavior or if it is really a bug?
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#120068
No description provided.