Kinect 4 Windows v2.0 Plugin

[=plangton;368214]
/blob/develop/Plugins/KinectV2/Source/KinectV2/Private/KinectSensor.cpp line number 768 (void FKinectSensor::UpdateTexture(UTexture2DpTexture, const RGBQUADpData, uint32 SizeX, uint32 SizeY))

so the point is that you lock the texture in line 779 this way : uint8 Dest = (uint8*)Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);* . without checking if its already locked.
I suggest at least a simple lock check such as Texture->PlatformData->Mips[0].BulkData.IsLocked() can be used and whether check it as a infinite while condition or put the thread in sleep in that while. for sure you have even better suggestion than mine.

this makes it complicated if another thread is trying to manipulated a previously fetched texture from kinect.

PS : is it a big hassle to add CoordinateMapper::GetDepthCameraIntrinsics() function to your plugin since you already have an instance of the coordinateMapper ? I was thinking to add it but wanted to consult with you
[/]

The reason for not checking if the texture is locked comes from the design of the plugin flow, this function is being called only in the game thread by the kinect input device when UpdateTexture is called.
When Update
Texture is called I use a mutex to guard the memory from being overwritten by the Kinect Thread.

On game thread:



 void FKinectSensor::UpdateColorTexture(UTexture2D*pTexture)
{
	SCOPE_CYCLE_COUNTER(STAT_KINECT_SENSOR_ColorUpdateTime);
	
	if (m_bNewColorFrame)
	{
		FScopeLock lock(&mColorCriticalSection); // <--------
		UpdateTexture(pTexture, m_pColorFrameRGBX, cColorWidth, cColorHeight);
		m_bNewColorFrame = false;
	}

	return;
}


In the Kinect thread:


void FKinectSensor::ProcessColorFrame(IColorFrameArrivedEventArgs*pArgs)
{
	SCOPE_CYCLE_COUNTER(STAT_KINECT_SENSOR_ColorProcessTime);

	TComPtr<IColorFrameReference> pColorFrameReferance = nullptr;

	HRESULT hr = pArgs->get_FrameReference(&pColorFrameReferance);

	if (SUCCEEDED(hr)){
		TComPtr<IColorFrame> pColorFrame = nullptr;
		if (SUCCEEDED(pColorFrameReferance->AcquireFrame(&pColorFrame))){
			RGBQUAD *pColorBuffer = NULL;
			pColorBuffer = m_pColorFrameRGBX;
			uint32 nColorBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);
			{
				FScopeLock lock(&mColorCriticalSection); // <------------------------
				hr = pColorFrame->CopyConvertedFrameDataToArray(nColorBufferSize, reinterpret_cast<BYTE*>(pColorBuffer), ColorImageFormat_Bgra);
				m_bNewColorFrame = true;
			}
		}
		pColorFrame.Reset();

	}
	pColorFrameReferance.Reset();

}

The textures that are being updated are members of the KinectInputDevice class and so dose the KinectManager instance that triggers the frame events.
When the kinect manager triggers new frame events what actually happens is that this is done by the KinectInputDevice that checks if there is a new frame if there is, it updates the textures using one of the frame update functions of the KinectSensor class than it passes a pointer to the KinectEventManager event dispatcher for that frame.
All of that is done sequentially in the game thread after the frame is updated it won’t be locked again anywhere else.

P.S.

I am working on coordinateMapper pm me if you have specific feature requests.