SpoutBPFunctionLibrary.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include "SpoutPluginPrivatePCH.h"
  2. #include "../Public/SpoutBPFunctionLibrary.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <sstream>
  7. static ID3D11Device* g_D3D11Device;
  8. ID3D11DeviceContext* g_pImmediateContext = NULL;
  9. spoutSenderNames * sender;
  10. spoutGLDXinterop * interop;
  11. spoutDirectX * sdx;
  12. TArray<FSenderStruct> FSenders;
  13. UMaterialInterface* BaseMaterial;
  14. FName TextureParameterName = "SpoutTexture";
  15. void DestroyTexture(UTexture2D*& Texture)
  16. {
  17. // Here we destory the texture and its resource
  18. if (Texture){
  19. Texture->RemoveFromRoot();
  20. if (Texture->Resource)
  21. {
  22. BeginReleaseResource(Texture->Resource);
  23. FlushRenderingCommands();
  24. }
  25. Texture->MarkPendingKill();
  26. Texture = nullptr;
  27. }else{
  28. UE_LOG(SpoutLog, Warning, TEXT("Texture is ready"));
  29. }
  30. }
  31. void ResetMatInstance(UTexture2D*& Texture, UMaterialInstanceDynamic*& MaterialInstance)
  32. {
  33. if (!Texture || !BaseMaterial || TextureParameterName.IsNone())
  34. {
  35. UE_LOG(SpoutLog, Warning, TEXT("UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"));
  36. return;
  37. }
  38. // Create material instance
  39. if (!MaterialInstance)
  40. {
  41. MaterialInstance = UMaterialInstanceDynamic::Create(BaseMaterial, NULL);
  42. if (!MaterialInstance)
  43. {
  44. UE_LOG(SpoutLog, Warning, TEXT("Material instance can't be created"));
  45. return;
  46. }
  47. }
  48. // Check again, we must have material instance
  49. if (!MaterialInstance)
  50. {
  51. UE_LOG(SpoutLog, Error, TEXT("Material instance wasn't created"));
  52. return;
  53. }
  54. // Check we have desired parameter
  55. UTexture* Tex = nullptr;
  56. if (!MaterialInstance->GetTextureParameterValue(TextureParameterName, Tex))
  57. {
  58. UE_LOG(SpoutLog, Warning, TEXT("UI Material instance Texture parameter not found"));
  59. return;
  60. }
  61. MaterialInstance->SetTextureParameterValue(TextureParameterName, Texture);
  62. }
  63. void ResetTexture(UTexture2D*& Texture, UMaterialInstanceDynamic*& MaterialInstance, FSenderStruct*& SenderStruct)
  64. {
  65. // Here we init the texture to its initial state
  66. DestroyTexture(Texture);
  67. //UE_LOG(SpoutLog, Warning, TEXT("Texture is ready???????2222"));
  68. // init the new Texture2D
  69. Texture = UTexture2D::CreateTransient(SenderStruct->w, SenderStruct->h, PF_B8G8R8A8);
  70. Texture->AddToRoot();
  71. Texture->UpdateResource();
  72. //UE_LOG(SpoutLog, Warning, TEXT("Texture is ready???????333333"));
  73. SenderStruct->Texture2DResource = (FTexture2DResource*)Texture->Resource;
  74. ////////////////////////////////////////////////////////////////////////////////
  75. ResetMatInstance(Texture, MaterialInstance);
  76. }
  77. UTextureRenderTarget2D* USpoutBPFunctionLibrary::CreateTextureRenderTarget2D(int32 w, int32 h, EPixelFormat pixelFormat, bool forceLinearGamma) {
  78. UTextureRenderTarget2D* textureTarget = NewObject<UTextureRenderTarget2D>();
  79. textureTarget->bNeedsTwoCopies = true;
  80. textureTarget->InitCustomFormat(w, h, pixelFormat, forceLinearGamma);
  81. textureTarget->AddressX = TextureAddress::TA_Wrap;
  82. textureTarget->AddressY = TextureAddress::TA_Wrap;
  83. #if WITH_EDITORONLY_DATA
  84. textureTarget->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
  85. #endif
  86. textureTarget->AddToRoot();
  87. //textureTarget->UpdateResourceW();
  88. textureTarget->UpdateResource();
  89. return textureTarget;
  90. }
  91. void initSpout()
  92. {
  93. UE_LOG(SpoutLog, Warning, TEXT("-----------> Init Spout"));
  94. sender = new spoutSenderNames;
  95. interop = new spoutGLDXinterop;
  96. sdx = new spoutDirectX;
  97. }
  98. void GetDevice()
  99. {
  100. UE_LOG(SpoutLog, Warning, TEXT("-----------> Set Graphics Device D3D11"));
  101. g_D3D11Device = (ID3D11Device*)GDynamicRHI->RHIGetNativeDevice();
  102. g_D3D11Device->GetImmediateContext(&g_pImmediateContext);
  103. }
  104. int32 USpoutBPFunctionLibrary::SetMaxSenders(int32 max){
  105. if (sender == nullptr)
  106. {
  107. initSpout();
  108. };
  109. sender->SetMaxSenders(max);
  110. return max;
  111. }
  112. void USpoutBPFunctionLibrary::GetMaxSenders(int32& max) {
  113. if (sender == nullptr)
  114. {
  115. initSpout();
  116. };
  117. max = sender->GetMaxSenders();
  118. }
  119. bool GetSpoutRegistred(FName spoutName, FSenderStruct*& SenderStruct) {
  120. auto MyPredicate = [&](const FSenderStruct InItem) {return InItem.sName == spoutName; };
  121. SenderStruct = FSenders.FindByPredicate(MyPredicate);
  122. bool bIsInListSenders = FSenders.ContainsByPredicate(MyPredicate);
  123. return bIsInListSenders;
  124. }
  125. void UnregisterSpout(FName spoutName) {
  126. auto MyPredicate = [&](const FSenderStruct InItem) {return InItem.sName == spoutName; };
  127. FSenders.RemoveAll(MyPredicate);
  128. }
  129. void ClearRegister() {
  130. FSenders.Empty();
  131. }
  132. FSenderStruct* RegisterReceiver(FName spoutName){
  133. unsigned int w;
  134. unsigned int h;
  135. HANDLE sHandle;
  136. unsigned long format;
  137. sender->GetSenderInfo(spoutName.GetPlainANSIString(), w, h, sHandle, format);
  138. FSenderStruct* newFSenderStruc = new FSenderStruct();
  139. newFSenderStruc->SetH(h);
  140. newFSenderStruc->SetW(w);
  141. newFSenderStruc->SetHandle(sHandle);
  142. newFSenderStruc->SetName(spoutName);
  143. newFSenderStruc->bIsAlive = true;
  144. newFSenderStruc->spoutType = ESpoutType::Receiver;
  145. newFSenderStruc->MaterialInstanceColor = nullptr;
  146. newFSenderStruc->TextureColor = nullptr;
  147. newFSenderStruc->sharedResource = nullptr;
  148. newFSenderStruc->rView = nullptr;
  149. newFSenderStruc->texTemp = NULL;
  150. //TextureColor = new
  151. UE_LOG(SpoutLog, Warning, TEXT("No material intance, creating...//////"));
  152. // Prepara Textura, Set the texture update region
  153. newFSenderStruc->UpdateRegions = new FUpdateTextureRegion2D(0, 0, 0, 0, newFSenderStruc->w, newFSenderStruc->h);
  154. ResetTexture(newFSenderStruc->TextureColor, newFSenderStruc->MaterialInstanceColor, newFSenderStruc);
  155. UE_LOG(SpoutLog, Warning, TEXT("--starting...--___Open Shared Resource___---"));
  156. HRESULT openResult = g_D3D11Device->OpenSharedResource(newFSenderStruc->sHandle, __uuidof(ID3D11Resource), (void**)(&newFSenderStruc->sharedResource));
  157. if (FAILED(openResult)) {
  158. UE_LOG(SpoutLog, Error, TEXT("--FAIL--___Open Shared Resource___---"));
  159. return false;
  160. }
  161. UE_LOG(SpoutLog, Warning, TEXT("--starting...--___Create Shader Resource View___---"));
  162. HRESULT createShaderResourceViewResult = g_D3D11Device->CreateShaderResourceView(newFSenderStruc->sharedResource, NULL, &newFSenderStruc->rView);
  163. if (FAILED(createShaderResourceViewResult)) {
  164. UE_LOG(SpoutLog, Error, TEXT("--FAIL--___Create Shader Resource View___---"));
  165. return false;
  166. }
  167. //texture shared for the spout resource handle
  168. ID3D11Texture2D* tex = (ID3D11Texture2D*)newFSenderStruc->sharedResource;
  169. if (tex == nullptr) {
  170. UE_LOG(SpoutLog, Error, TEXT("---|||------||||----"));
  171. return false;
  172. }
  173. D3D11_TEXTURE2D_DESC description;
  174. tex->GetDesc(&description);
  175. description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  176. description.Usage = D3D11_USAGE_STAGING;
  177. description.BindFlags = 0;
  178. description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
  179. description.MiscFlags = 0;
  180. description.MipLevels = 1;
  181. description.ArraySize = 1;
  182. UE_LOG(SpoutLog, Warning, TEXT("--- Creando d3d11 Texture 2D---"));
  183. HRESULT hr = g_D3D11Device->CreateTexture2D(&description, NULL, &newFSenderStruc->texTemp);
  184. if (FAILED(hr))
  185. {
  186. std::stringstream ss;
  187. ss << " Error code = 0x" << std::hex << hr << std::endl;
  188. std::cout << ss.str() << std::endl;
  189. std::string TestString = ss.str();
  190. UE_LOG(SpoutLog, Error, TEXT("Failed Create Texture: ----> %s"), *FString(TestString.c_str()));
  191. if (hr == E_OUTOFMEMORY) {
  192. UE_LOG(SpoutLog, Error, TEXT("OUT OF MEMORY"));
  193. }
  194. if (newFSenderStruc->texTemp)
  195. {
  196. newFSenderStruc->texTemp->Release();
  197. newFSenderStruc->texTemp = NULL;
  198. }
  199. UE_LOG(SpoutLog, Error, TEXT("error creating temporal textura"));
  200. return false;
  201. }
  202. FSenders.Add(*newFSenderStruc);
  203. return newFSenderStruc;
  204. }
  205. bool USpoutBPFunctionLibrary::SpoutInfo(TArray<FSenderStruct>& Senders){
  206. Senders = FSenders;
  207. return true;
  208. }
  209. bool USpoutBPFunctionLibrary::SpoutInfoFrom(FName spoutName, FSenderStruct& SenderStruct){
  210. //Existe en mi lista ??
  211. auto MyPredicate = [&](const FSenderStruct InItem) {return InItem.sName == spoutName; };
  212. FSenderStruct* EncontradoSenderStruct = FSenders.FindByPredicate(MyPredicate);
  213. if (EncontradoSenderStruct == nullptr){
  214. UE_LOG(SpoutLog, Warning, TEXT("No Encontrado sender con nombre : %s"), *spoutName.GetPlainNameString());
  215. return false;
  216. }
  217. else
  218. {
  219. SenderStruct = *EncontradoSenderStruct;
  220. }
  221. return true;
  222. }
  223. bool USpoutBPFunctionLibrary::CreateRegisterSender(FName spoutName, ID3D11Texture2D* baseTexture)
  224. {
  225. if (g_D3D11Device == nullptr || g_pImmediateContext == NULL){
  226. UE_LOG(SpoutLog, Warning, TEXT("Getting Device..."));
  227. GetDevice();
  228. }
  229. HANDLE sharedSendingHandle = NULL;
  230. bool texResult = false;
  231. bool updateResult = false;
  232. bool senderResult = false;
  233. D3D11_TEXTURE2D_DESC desc;
  234. baseTexture->GetDesc(&desc);
  235. ID3D11Texture2D * sendingTexture;
  236. UE_LOG(SpoutLog, Warning, TEXT("ID3D11Texture2D Info : ancho_%i, alto_%i"), desc.Width, desc.Height);
  237. UE_LOG(SpoutLog, Warning, TEXT("ID3D11Texture2D Info : Format is %i"), int(desc.Format));
  238. //use the pixel format from basetexture (the native texture textureRenderTarget2D)
  239. DXGI_FORMAT texFormat = desc.Format;
  240. if (desc.Format == DXGI_FORMAT_B8G8R8A8_TYPELESS) {
  241. texFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
  242. }
  243. texResult = sdx->CreateSharedDX11Texture(g_D3D11Device, desc.Width, desc.Height, texFormat, &sendingTexture, sharedSendingHandle);
  244. UE_LOG(SpoutLog, Warning, TEXT("Create shared Texture with SDX : %i"), texResult);
  245. if (!texResult)
  246. {
  247. UE_LOG(SpoutLog, Error, TEXT("SharedDX11Texture creation failed"));
  248. return 0;
  249. }
  250. const auto tmp = spoutName.GetPlainNameString();
  251. UE_LOG(SpoutLog, Warning, TEXT("Created Sender: name --> %s"), *tmp);
  252. //
  253. senderResult = sender->CreateSender(spoutName.GetPlainANSIString(), desc.Width, desc.Height, sharedSendingHandle, texFormat);
  254. UE_LOG(SpoutLog, Warning, TEXT("Created sender DX11 with sender name : %s"), *tmp);
  255. // remove old sender register
  256. auto MyPredicate = [&](const FSenderStruct InItem) {return InItem.sName == spoutName; };
  257. FSenders.RemoveAll(MyPredicate);
  258. // add new register
  259. UE_LOG(SpoutLog, Warning, TEXT("Adding Sender to Sender list"));
  260. FSenderStruct* newFSenderStruc = new FSenderStruct();
  261. newFSenderStruc->SetW(desc.Width);
  262. newFSenderStruc->SetH(desc.Height);
  263. newFSenderStruc->SetName(spoutName);
  264. newFSenderStruc->bIsAlive = true;
  265. newFSenderStruc->spoutType = ESpoutType::Sender;
  266. newFSenderStruc->SetHandle(sharedSendingHandle);
  267. newFSenderStruc->activeTextures = sendingTexture;
  268. newFSenderStruc->MaterialInstanceColor = nullptr;
  269. newFSenderStruc->TextureColor = nullptr;
  270. FSenders.Add(*newFSenderStruc);
  271. return senderResult;
  272. }
  273. ESpoutState CheckSenderState(FName spoutName){
  274. auto MyPredicate = [&](const FSenderStruct InItem) {return InItem.sName == spoutName; };
  275. bool bIsInListSenders = FSenders.ContainsByPredicate(MyPredicate);
  276. ESpoutState state = ESpoutState::noEnoR;
  277. if (sender->FindSenderName(spoutName.GetPlainANSIString())) {
  278. //UE_LOG(SpoutLog, Warning, TEXT("Sender State: --> Exist"));
  279. if (bIsInListSenders) {
  280. //UE_LOG(SpoutLog, Warning, TEXT("Sender State: --> Exist y Registred"));
  281. state = ESpoutState::ER;
  282. }
  283. else {
  284. //UE_LOG(SpoutLog, Warning, TEXT("Sender State: --> Exist y No Registred"));
  285. state = ESpoutState::EnoR;
  286. }
  287. }
  288. else {
  289. //UE_LOG(SpoutLog, Warning, TEXT("Sender State: --> No Exist"));
  290. if (bIsInListSenders) {
  291. //UE_LOG(SpoutLog, Warning, TEXT("Sender State: --> No Exist y Registred"));
  292. state = ESpoutState::noER;
  293. }
  294. else {
  295. //UE_LOG(SpoutLog, Warning, TEXT("Sender State: --> No Exist y No Registred"));
  296. state = ESpoutState::noEnoR;
  297. }
  298. }
  299. return state;
  300. }
  301. bool USpoutBPFunctionLibrary::SpoutSender(FName spoutName, ESpoutSendTextureFrom sendTextureFrom, UTextureRenderTarget2D* textureRenderTarget2D, float targetGamma)
  302. {
  303. if (sender == nullptr)
  304. {
  305. initSpout();
  306. };
  307. if (g_D3D11Device == nullptr || g_pImmediateContext == NULL) {
  308. GetDevice();
  309. }
  310. ID3D11Texture2D* baseTexture = 0;
  311. FSenderStruct* SenderStruct = 0;
  312. switch (sendTextureFrom)
  313. {
  314. case ESpoutSendTextureFrom::GameViewport:
  315. baseTexture = (ID3D11Texture2D*)GEngine->GameViewport->Viewport->GetRenderTargetTexture()->GetNativeResource();
  316. break;
  317. case ESpoutSendTextureFrom::TextureRenderTarget2D:
  318. if (textureRenderTarget2D == nullptr) {
  319. UE_LOG(SpoutLog, Warning, TEXT("No TextureRenderTarget2D Selected!!"));
  320. return false;
  321. }
  322. textureRenderTarget2D->TargetGamma = targetGamma;
  323. baseTexture = (ID3D11Texture2D*)textureRenderTarget2D->Resource->TextureRHI->GetTexture2D()->GetNativeResource();
  324. break;
  325. default:
  326. break;
  327. }
  328. if (baseTexture == nullptr) {
  329. UE_LOG(SpoutLog, Warning, TEXT("baseTexture is null"));
  330. return false;
  331. }
  332. ESpoutState state = CheckSenderState(spoutName);
  333. if (state == ESpoutState::noEnoR || state == ESpoutState::noER) {
  334. UE_LOG(SpoutLog, Warning, TEXT("New Sender creating, registering..."));
  335. CreateRegisterSender(spoutName, baseTexture);
  336. return false;
  337. }
  338. if (state == ESpoutState::EnoR) {
  339. UE_LOG(SpoutLog, Warning, TEXT("Already exist a Sender with the name %s"), *spoutName.GetPlainNameString());
  340. return false;
  341. }
  342. if (state == ESpoutState::ER) {
  343. GetSpoutRegistred(spoutName, SenderStruct);
  344. if (SenderStruct->spoutType == ESpoutType::Sender) {
  345. // Check whether texture size has changed
  346. D3D11_TEXTURE2D_DESC td;
  347. baseTexture->GetDesc(&td);
  348. if (td.Width != SenderStruct->w || td.Height != SenderStruct->h) {
  349. UE_LOG(SpoutLog, Warning, TEXT("Texture Size has changed, Updating registered spout: "), *spoutName.GetPlainNameString());
  350. UpdateRegisteredSpout(spoutName, baseTexture);
  351. return false;
  352. }
  353. }
  354. if (SenderStruct->spoutType == ESpoutType::Receiver) {
  355. UE_LOG(SpoutLog, Warning, TEXT("Already exist a Sender with the name %s and you have a receiver in unreal receiving"), *spoutName.GetPlainNameString());
  356. return false;
  357. }
  358. }
  359. bool result = false;
  360. if (SenderStruct->activeTextures == nullptr)
  361. {
  362. UE_LOG(SpoutLog, Warning, TEXT("activeTextures is null"));
  363. return false;
  364. }
  365. HANDLE targetHandle = SenderStruct->sHandle;
  366. ID3D11Texture2D * targetTex = SenderStruct->activeTextures;
  367. if (targetTex == nullptr){
  368. UE_LOG(SpoutLog, Warning, TEXT("targetTex is null"));
  369. return false;
  370. }
  371. //Sincroniza el thread del render y la copia de la textura
  372. ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
  373. void,
  374. ID3D11Texture2D*, tt, targetTex,
  375. ID3D11Texture2D*, bt, baseTexture,
  376. {
  377. g_pImmediateContext->CopyResource(tt, bt);
  378. g_pImmediateContext->Flush();
  379. });
  380. D3D11_TEXTURE2D_DESC td;
  381. baseTexture->GetDesc(&td);
  382. result = sender->UpdateSender(spoutName.GetPlainANSIString(), td.Width, td.Height, targetHandle);
  383. return result;
  384. }
  385. bool USpoutBPFunctionLibrary::SpoutReceiver(const FName spoutName, UMaterialInstanceDynamic*& mat, UTexture2D*& texture)
  386. {
  387. const FString SenderNameString = spoutName.GetPlainNameString();
  388. if (BaseMaterial == NULL)
  389. {
  390. UMaterialInterface* mBase_Material = 0;
  391. mBase_Material = LoadObject<UMaterial>(NULL, TEXT("/SpoutPlugin/Materials/SpoutMaterial.SpoutMaterial"), NULL, LOAD_None, NULL);
  392. BaseMaterial = mBase_Material;
  393. }
  394. if (sender == nullptr)
  395. {
  396. initSpout();
  397. };
  398. if (g_D3D11Device == nullptr || g_pImmediateContext == NULL){
  399. GetDevice();
  400. }
  401. ESpoutState state = CheckSenderState(spoutName);
  402. if (state == ESpoutState::noEnoR) {
  403. UE_LOG(SpoutLog, Warning, TEXT("Not found any sender and no registred with the name %s"), *spoutName.GetPlainNameString());
  404. UE_LOG(SpoutLog, Warning, TEXT("try to rename it, or resend %s"), *SenderNameString);
  405. return false;
  406. }
  407. if(state == ESpoutState::noER) {
  408. UE_LOG(SpoutLog, Warning, TEXT("why are you registred??, unregistre, best close it"));
  409. //UnregisterSpout(spoutName);
  410. CloseSender(spoutName);
  411. return false;
  412. }
  413. if (state == ESpoutState::EnoR) {
  414. UE_LOG(SpoutLog, Warning, TEXT("Sender %s found, registring, receiving..."), *spoutName.GetPlainNameString());
  415. RegisterReceiver(spoutName);
  416. return false;
  417. }
  418. if (state == ESpoutState::ER) {
  419. FSenderStruct* SenderStruct = 0;
  420. GetSpoutRegistred(spoutName, SenderStruct);
  421. if (SenderStruct->spoutType == ESpoutType::Sender) {
  422. //UE_LOG(SpoutLog, Warning, TEXT("Receiving from sender inside ue4 with the name %s"), *spoutName.GetPlainNameString());
  423. }
  424. if (SenderStruct->spoutType == ESpoutType::Receiver) {
  425. //UE_LOG(SpoutLog, Warning, TEXT("Continue Receiver with the name %s"), *SenderName.GetPlainNameString());
  426. //communication between the two threads (rendering thread and game thread)
  427. // copy pixels from shared resource texture to texture temporal and update
  428. ENQUEUE_UNIQUE_RENDER_COMMAND_FOURPARAMETER(
  429. void,
  430. ID3D11Texture2D*, t_texTemp, SenderStruct->texTemp,
  431. ID3D11Texture2D*, t_tex, (ID3D11Texture2D*)SenderStruct->sharedResource,
  432. int32, Stride, SenderStruct->w * 4,
  433. FSenderStruct*, Params, SenderStruct,
  434. {
  435. if (Params == nullptr) {
  436. return;
  437. }
  438. g_pImmediateContext->CopyResource(t_texTemp, t_tex);
  439. //g_pImmediateContext->Flush(); //<------ No Flush
  440. D3D11_MAPPED_SUBRESOURCE mapped;
  441. //Gets a pointer to the data contained in a subresource, and denies the GPU access to that subresource.
  442. // with CPU read permissions (D3D11_MAP_READ)
  443. HRESULT hr = g_pImmediateContext->Map(t_texTemp, 0, D3D11_MAP_READ, 0, &mapped);
  444. if (FAILED(hr))
  445. {
  446. t_texTemp->Release();
  447. return;
  448. }
  449. BYTE *pixel = (BYTE *)mapped.pData;
  450. g_pImmediateContext->Unmap(t_texTemp, 0);
  451. //Update Texture
  452. RHIUpdateTexture2D(Params->Texture2DResource->GetTexture2DRHI(), 0, *Params->UpdateRegions, mapped.RowPitch, (uint8*)pixel);
  453. });
  454. texture = SenderStruct->TextureColor;
  455. mat = SenderStruct->MaterialInstanceColor;
  456. }
  457. }
  458. return true;
  459. }
  460. void USpoutBPFunctionLibrary::CloseSender(FName spoutName)
  461. {
  462. UE_LOG(SpoutLog, Warning, TEXT("Closing... sender %s "), *spoutName.GetPlainNameString());
  463. if (sender == nullptr)
  464. {
  465. initSpout();
  466. };
  467. if (g_D3D11Device == nullptr || g_pImmediateContext == NULL) {
  468. GetDevice();
  469. }
  470. ESpoutState state = CheckSenderState(spoutName);
  471. if (state == ESpoutState::noEnoR) {
  472. UE_LOG(SpoutLog, Warning, TEXT("already %s closed, there is nothing to close!! check the name"), *spoutName.GetPlainNameString());
  473. //return;
  474. }
  475. if (state == ESpoutState::noER) {
  476. UE_LOG(SpoutLog, Warning, TEXT("+++++++++++++++"));
  477. UnregisterSpout(spoutName);
  478. //return;
  479. }
  480. if (state == ESpoutState::EnoR) {
  481. UE_LOG(SpoutLog, Warning, TEXT("Already exist a Sender with the name %s, You can not close a sender that is not yours.??"), *spoutName.GetPlainNameString());
  482. //return;
  483. }
  484. if (state == ESpoutState::ER) {
  485. FSenderStruct* tempSenderStruct = 0;
  486. GetSpoutRegistred(spoutName, tempSenderStruct);
  487. if (tempSenderStruct->spoutType == ESpoutType::Sender) {
  488. UE_LOG(SpoutLog, Warning, TEXT("releasing sender %s"), *spoutName.GetPlainNameString());
  489. // here really release the sender
  490. sender->ReleaseSenderName(spoutName.GetPlainANSIString());
  491. UE_LOG(SpoutLog, Warning, TEXT("sender %s released"), *spoutName.GetPlainNameString());
  492. }
  493. else {
  494. UE_LOG(SpoutLog, Warning, TEXT("receiver always listening"));
  495. FlushRenderingCommands();
  496. if (tempSenderStruct->sharedResource != nullptr) {
  497. UE_LOG(SpoutLog, Warning, TEXT("Release sharedResource"));
  498. tempSenderStruct->sharedResource->Release();
  499. }
  500. if (tempSenderStruct->texTemp != nullptr) {
  501. UE_LOG(SpoutLog, Warning, TEXT("Release Temporal texTemp"));
  502. tempSenderStruct->texTemp->Release();
  503. }
  504. if (tempSenderStruct->rView != nullptr) {
  505. UE_LOG(SpoutLog, Warning, TEXT("Release rView"));
  506. tempSenderStruct->rView->Release();
  507. }
  508. UE_LOG(SpoutLog, Warning, TEXT("Released All Temporal Textures"));
  509. //return;
  510. }
  511. UnregisterSpout(spoutName);
  512. }
  513. UE_LOG(SpoutLog, Warning, TEXT("There are now %i senders remaining "), FSenders.Num());
  514. }
  515. bool USpoutBPFunctionLibrary::UpdateRegisteredSpout(FName spoutName, ID3D11Texture2D * baseTexture)
  516. {
  517. HANDLE sharedSendingHandle = NULL;
  518. bool texResult = false;
  519. bool updateResult = false;
  520. bool senderResult = false;
  521. D3D11_TEXTURE2D_DESC desc;
  522. baseTexture->GetDesc(&desc);
  523. ID3D11Texture2D * sendingTexture;
  524. UE_LOG(SpoutLog, Warning, TEXT("ID3D11Texture2D Info : ancho_%i, alto_%i"), desc.Width, desc.Height);
  525. UE_LOG(SpoutLog, Warning, TEXT("ID3D11Texture2D Info : Format is %i"), int(desc.Format));
  526. //use the pixel format from basetexture (the native texture textureRenderTarget2D)
  527. DXGI_FORMAT texFormat = desc.Format;
  528. if (desc.Format == DXGI_FORMAT_B8G8R8A8_TYPELESS) {
  529. texFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
  530. }
  531. texResult = sdx->CreateSharedDX11Texture(g_D3D11Device, desc.Width, desc.Height, texFormat, &sendingTexture, sharedSendingHandle);
  532. UE_LOG(SpoutLog, Warning, TEXT("Create shared Texture with SDX : %i"), texResult);
  533. if (!texResult)
  534. {
  535. UE_LOG(SpoutLog, Error, TEXT("SharedDX11Texture creation failed"));
  536. return 0;
  537. }
  538. // update register
  539. UE_LOG(SpoutLog, Warning, TEXT("Updating Sender in Sender list"));
  540. for (int32 Index = 0; Index != FSenders.Num(); ++Index)
  541. {
  542. if (FSenders[Index].sName == spoutName) {
  543. FSenders[Index].SetW(desc.Width);
  544. FSenders[Index].SetH(desc.Height);
  545. FSenders[Index].SetName(spoutName);
  546. FSenders[Index].bIsAlive = true;
  547. FSenders[Index].spoutType = ESpoutType::Sender;
  548. FSenders[Index].SetHandle(sharedSendingHandle);
  549. FSenders[Index].activeTextures = sendingTexture;
  550. FSenders[Index].MaterialInstanceColor = nullptr;
  551. FSenders[Index].TextureColor = nullptr;
  552. senderResult = true;
  553. }
  554. }
  555. return senderResult;
  556. }