浏览代码

Merge pull request #935 from Unity-Technologies/hotfix-visual-leak

Hotfix - Visual observation memory leak
/hotfix-v0.9.2a
GitHub 6 年前
当前提交
c59b1c6a
共有 1 个文件被更改,包括 35 次插入19 次删除
  1. 54
      unity-environment/Assets/ML-Agents/Scripts/Agent.cs

54
unity-environment/Assets/ML-Agents/Scripts/Agent.cs


/// their own experience.
int stepCount;
// Flag to signify that an agent has been reset but the fact that it is
// done has not been communicated (required for On Demand Decisions).
/// Flag to signify that an agent has been reset but the fact that it is
/// done has not been communicated (required for On Demand Decisions).
// Flag to signify that an agent is done and should not reset until
// the fact that it is done has been communicated.
/// Flag to signify that an agent is done and should not reset until
/// the fact that it is done has been communicated.
bool terminate;
/// Unique identifier each agent receives at initialization. It is used

/// Array of Texture2D used to render to from render buffer before
/// transforming into float tensor.
Texture2D[] textureArray;
textureArray = new Texture2D[agentParameters.agentCameras.Count];
for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++)
{
textureArray[i] = new Texture2D(brain.brainParameters.cameraResolutions[i].width,
brain.brainParameters.cameraResolutions[i].height, TextureFormat.RGB24, false);
}
id = gameObject.GetInstanceID();
Academy academy = Object.FindObjectOfType<Academy>() as Academy;
OnEnableHelper(academy);

for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++)
{
info.visualObservations.Add(ObservationToTexture(
ObservationToTexture(
param.cameraResolutions[i].height));
param.cameraResolutions[i].height,
ref textureArray[i]);
info.visualObservations.Add(textureArray[i]);
}
info.reward = reward;

/// Converts a camera and correspinding resolution to a 2D texture.
/// </summary>
/// <returns>The 2D texture.</returns>
/// <param name="camera">Camera.</param>
/// <param name="obsCamera">Camera.</param>
public static Texture2D ObservationToTexture(Camera camera, int width, int height)
/// <param name="texture2D">Texture2D to render to.</param>
public static void ObservationToTexture(Camera obsCamera, int width, int height, ref Texture2D texture2D)
Rect oldRec = camera.rect;
camera.rect = new Rect(0f, 0f, 1f, 1f);
Rect oldRec = obsCamera.rect;
obsCamera.rect = new Rect(0f, 0f, 1f, 1f);
var depth = 24;
var format = RenderTextureFormat.Default;
var readWrite = RenderTextureReadWrite.Default;

var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
if (width != texture2D.width || height != texture2D.height)
{
texture2D.Resize(width, height);
}
var prevCameraRT = camera.targetTexture;
var prevCameraRT = obsCamera.targetTexture;
camera.targetTexture = tempRT;
obsCamera.targetTexture = tempRT;
camera.Render();
obsCamera.Render();
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
tex.Apply();
camera.targetTexture = prevCameraRT;
camera.rect = oldRec;
texture2D.ReadPixels(new Rect(0, 0, texture2D.width, texture2D.height), 0, 0);
texture2D.Apply();
obsCamera.targetTexture = prevCameraRT;
obsCamera.rect = oldRec;
return tex;
}
}
}
正在加载...
取消
保存