Skip to content

render.ReadPixel doesn't work for certain image formats #6643

@meetric1

Description

@meetric1

Details

IMAGE_FORMAT_RGBA16161616F doesn't return the values you would expect from render.ReadPixel.

In my specific case, I am trying to generate a displacement map from an image. (There isn't a way to bind a vertex texture to a shader, so I am forced to return data to the CPU to generate a mesh. rubatplsfix)

The values returned are integers ranging from 0-255, but it would be nice for the more precise formats to return a float, or at the very least the full information of that specific pixel. (For parity, you can still return the 0-255 range, but allow decimal values. So a return could look like 50.25 100.5 150.75)

Steps to reproduce

local rt = GetRenderTargetEx("test_rt", 3, 3, 0, 2, 1, 0, IMAGE_FORMAT_RGBA16161616F)
local clear_mat = CreateMaterial("clear_mat", "UnlitGeneric", {
	["$basetexture"] = "lights/white",
	["$color2"] = "[0.12345 3.1415 2.7182]",
	["$ignorez"] = 1,
	["$linearwrite"] = 1
}) -- cant use render.Clear

render.PushRenderTarget(rt)
	cam.Start2D()
		-- clear to our precise color
		render.SetMaterial(clear_mat)
		render.DrawBox(Vector(), Angle(), Vector(0, 0, 1), Vector(3, 3, 1))

		render.CapturePixels()
	cam.End2D()
render.PopRenderTarget()

print(render.ReadPixel(1, 1)) -- sample in center

You can check the rendertarget with mat_texture_list 1. It should be a very vibrant cyan blue

Ideally, this would print something consistent. You would expect this to return 31 255 255 or 0.12345 3.1415 2.7182 but for me it returns 111 110 116 (omitting alpha channel)

IMAGE_FORMAT_RGBA16161616 does appear to be pretty consistent, although the data is heavily truncated.
(65536 possible color values -> 256 possible color values). Fixing this would be nice too

Version of the game

Protocol version 24
Network version 2025.03.26 (garrysmod)
Exe build: 15:00:14 Dec 3 2025 (9888) (4000)
GMod version 2025.12.03, branch: x86-64, multicore: 1
Windows 64bit

Beta

x86-64 beta

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions