Search Feedback

200
votes

Garbage free Texture2D.GetPixels()

2D

-

-

The current signature of GetPixels is:
public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight, int miplevel = 0);

if a version was added that didn't return an array but instead took a ref to the destination array as an argument the whole thing could be done without creating garbage for the GC to collect.

public bool GetPixels(int x, int y, int blockWidth, int blockHeight, ref Color[] dest_array, int miplevel = 0);

This is currently causing hitches due to GC when I blit sprites together in a character paperdoll system and it seems so easy to fix.

Comments (11)

  1. 77f05875baea9117bd7966dd6d12b2fc?d=mm

    PearShapedCode

    Dec 24, 2018 12:12

    I second this with a little expansion, since I'd like it also in bool GetPixels32(int x, int y, int blockWidth, int blockHeight, ref Color32[] dest_array, int miplevel = 0) version too. Maybe GC.Collect does work well for this, but being able to have my own managed cached array when dealing with a lot of dynamic textures gradually over frames really would be nice.

  2. 1f644fe42aaa1c1bbddd24908aed3ddd?d=mm

    ReynV

    Oct 20, 2018 15:32

    I can't believe this isn't implemented yet. There are so many reasons to read back data. Why do we need to create new memory each time ?

  3. 4ffc9c1dac81a690226f3e558324df12?d=mm

    gwiasdeczka2

    Feb 02, 2018 11:09

    If one could provide additional argument (pointer) to a method call then GetPixels could not allocate new data but reuse existing array.

    Something like this:
    GetPixels(0,0,200,200,false, myAlreadyAllocatedArrayPointerWhichIsStatic)

  4. 91849aacb33d84abe032faaec81dbe42?d=mm

    Venecius

    Oct 14, 2017 09:43

    No way to get raw pixel data without allocations. (except calling GetPixel() for every pixel and the performance is no more).
    How should we obtain these pixels every frame without extra huge allocations?
    We need these functions: GetPixels(), Encode...() to accept buffers in real time camera capture scenarios.

  5. 304fecb756c290ed7315142485f6d1b2?d=mm

    NerdyOrc

    Jun 15, 2017 19:02

    I would add a Garbage Free EncodeToJpg, PNG and EXR

  6. 1ef09908cafdbc8ba06057af5b15ef30?d=mm

    npatch

    Apr 13, 2017 10:29

    Why don't you use Graphics.CopyTexture/Blit instead? Happens in the gpu. If its just blits that its.

  7. 4573505c10c98cecca1672a826ab2a02?d=mm

    Bibzball

    Feb 16, 2017 09:38

    I support this, there's no reason to keep creating a new array everytime when we could simply reuse it.

  8. 71d00689d1ac3d7060382f02e8ccde25?d=mm

    mark_isci

    Jan 27, 2017 15:40

    this is such a pain in the ass that i spent a long time writing my own thing to mirror the pixel data in something under my own control. you can use it too for $15 https://candle.itch.io/unity-bitmap-drawing

  9. 33c40919b6f814e5f07bacfd50e48d20?d=mm

    hurleybird

    Nov 11, 2016 21:56

    +1

  10. D68bba2ffa9cf7f3d9a23cc6a3071020?d=mm

    startselect_

    Oct 14, 2016 07:30

    I make ~20 GetPixels calls each frame for ~15 characters using the paperdoll system, the sprites are small but they add up. Calling GC.Collect every frame is obviously not going to work for me. I could offload this work to when equipment changes occur but then each character would have all their frames baked and each character would get a huge amount of required texture memory which sort of defeats the purpose of the paperdoll system.

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea

Categories

All

(10889)

2D

(287)

Ads

(57)

AI & Navigation

(81)

Analytics

(130)

Animation

(406)

Asset Store

(364)

Asset Store Publisher

(19)

Assets

(551)

Audio

(185)

Cloud Build

(148)

Collaborate

(68)

Docs & Tutorials

(247)

Editor

(2545)

Everyplay

(17)

Game Performance Reporting

(21)

General

(987)

Graphics

(894)

GUI

(442)

Input

(173)

Licensing

(93)

Networking

(190)

Physics

(387)

Platforms

(445)

Profiling & Optimization

(84)

Runtime

(185)

Scripting

(1141)

Terrain

(174)

WebGL

(142)