private bool PopulateLayer()

in RealityMaterialExplorer/Assets/Oculus/VR/Scripts/OVROverlay.cs [658:776]


	private bool PopulateLayer(int mipLevels, bool isHdr, OVRPlugin.Sizei size, int sampleCount, int stage)
	{
		if (isExternalSurface)
		{
			return true;
		}

		bool ret = false;

		RenderTextureFormat rtFormat = (isHdr) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

		for (int eyeId = 0; eyeId < texturesPerStage; ++eyeId)
		{
			Texture et = layerTextures[eyeId].swapChain[stage];
			if (et == null)
				continue;

			for (int mip = 0; mip < mipLevels; ++mip)
			{
				bool dataIsLinear = isHdr || (QualitySettings.activeColorSpace == ColorSpace.Linear);

				var rt = textures[eyeId] as RenderTexture;
#if UNITY_ANDROID && !UNITY_EDITOR
				dataIsLinear = true; //HACK: Graphics.CopyTexture causes linear->srgb conversion on target write with D3D but not GLES.
#endif
				// PC requries premultiplied Alpha
				bool requiresPremultipliedAlpha = !Application.isMobilePlatform;

				bool linearToSRGB = !isHdr && dataIsLinear;
				// if the texture needs to be premultiplied, premultiply it unless its already premultiplied
				bool premultiplyAlpha = requiresPremultipliedAlpha && !isAlphaPremultiplied;

				bool bypassBlit = !linearToSRGB && !premultiplyAlpha && rt != null && rt.format == rtFormat;

				RenderTexture tempRTDst = null;

				if (!bypassBlit)
				{
					int width = size.w >> mip;
					if (width < 1) width = 1;
					int height = size.h >> mip;
					if (height < 1) height = 1;
					RenderTextureDescriptor descriptor = new RenderTextureDescriptor(width, height, rtFormat, 0);
					descriptor.msaaSamples = sampleCount;
					descriptor.useMipMap = true;
					descriptor.autoGenerateMips = false;
					descriptor.sRGB = false;

					tempRTDst = RenderTexture.GetTemporary(descriptor);

					if (!tempRTDst.IsCreated())
					{
						tempRTDst.Create();
					}

					tempRTDst.DiscardContents();

					Material blitMat = null;
					if (currentOverlayShape != OverlayShape.Cubemap && currentOverlayShape != OverlayShape.OffcenterCubemap)
					{
						blitMat = tex2DMaterial;
					}
					else
					{
						blitMat = cubeMaterial;
					}

					blitMat.SetInt("_linearToSrgb", linearToSRGB ? 1 : 0);
					blitMat.SetInt("_premultiply", premultiplyAlpha ? 1 : 0);
				}

				if (currentOverlayShape != OverlayShape.Cubemap && currentOverlayShape != OverlayShape.OffcenterCubemap)
				{
					if (bypassBlit)
					{
						Graphics.CopyTexture(textures[eyeId], 0, mip, et, 0, mip);
					}
					else
					{
						if (overrideTextureRectMatrix)
						{
							BlitSubImage(textures[eyeId], tempRTDst, tex2DMaterial, GetBlitRect(eyeId));
						}
						else
						{
							Graphics.Blit(textures[eyeId], tempRTDst, tex2DMaterial);
						}
						//Resolve, decompress, swizzle, etc not handled by simple CopyTexture.
						Graphics.CopyTexture(tempRTDst, 0, 0, et, 0, mip);
					}
				}
				else // Cubemap
				{
					for (int face = 0; face < 6; ++face)
					{
						cubeMaterial.SetInt("_face", face);
						if (bypassBlit)
						{
							Graphics.CopyTexture(textures[eyeId], face, mip, et, face, mip);
						}
						else
						{
							//Resolve, decompress, swizzle, etc not handled by simple CopyTexture.
							Graphics.Blit(textures[eyeId], tempRTDst, cubeMaterial);
							Graphics.CopyTexture(tempRTDst, 0, 0, et, face, mip);
						}
					}
				}
				if (tempRTDst != null)
				{
					RenderTexture.ReleaseTemporary(tempRTDst);
				}

				ret = true;
			}
		}

		return ret;
	}