BsScriptRenderTexture2D.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptRenderTexture2D.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoField.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoManager.h"
  8. #include "BsRenderTexture.h"
  9. #include "BsScriptTexture2D.h"
  10. #include "BsMonoUtil.h"
  11. #include "BsScriptResourceManager.h"
  12. namespace BansheeEngine
  13. {
  14. ScriptRenderTexture2D::ScriptRenderTexture2D(const SPtr<RenderTarget>& target, MonoObject* instance)
  15. :ScriptObject(instance), mRenderTarget(target)
  16. {
  17. }
  18. void ScriptRenderTexture2D::initRuntimeData()
  19. {
  20. metaData.scriptClass->addInternalCall("Internal_CreateDetailed", &ScriptRenderTexture2D::internal_createDetailed);
  21. metaData.scriptClass->addInternalCall("Internal_Create", &ScriptRenderTexture2D::internal_create);
  22. metaData.scriptClass->addInternalCall("Internal_GetColorSurfaces", &ScriptRenderTexture2D::internal_getColorSurfaces);
  23. metaData.scriptClass->addInternalCall("Internal_GetDepthStencilSurface", &ScriptRenderTexture2D::internal_getDepthStencilSurface);
  24. }
  25. SPtr<RenderTexture> ScriptRenderTexture2D::getRenderTexture() const
  26. {
  27. return std::static_pointer_cast<RenderTexture>(mRenderTarget);
  28. }
  29. void ScriptRenderTexture2D::internal_createDetailed(MonoObject* instance, PixelFormat format, UINT32 width, UINT32 height,
  30. UINT32 numSamples, bool gammaCorrection, bool createDepth, PixelFormat depthStencilFormat)
  31. {
  32. SPtr<RenderTexture> tex = RenderTexture::create(TEX_TYPE_2D, width, height, format, gammaCorrection, numSamples,
  33. createDepth, depthStencilFormat);
  34. new (bs_alloc<ScriptRenderTexture2D>()) ScriptRenderTexture2D(tex, instance);
  35. }
  36. void ScriptRenderTexture2D::internal_create(MonoObject* instance, MonoArray* colorSurfaces, MonoObject* depthStencilSurface)
  37. {
  38. ScriptArray colorSurfacesList(colorSurfaces);
  39. RENDER_SURFACE_DESC depthStencilSurfaceDesc;
  40. ScriptTexture2D* scriptDepthStencilSurface = ScriptTexture2D::toNative(depthStencilSurface);
  41. if (scriptDepthStencilSurface != nullptr)
  42. {
  43. depthStencilSurfaceDesc.face = 0;
  44. depthStencilSurfaceDesc.mipLevel = 0;
  45. depthStencilSurfaceDesc.numFaces = 1;
  46. HTexture textureHandle = scriptDepthStencilSurface->getHandle();
  47. if (!textureHandle.isLoaded())
  48. {
  49. LOGERR("Render texture must be created using a fully loaded texture.");
  50. }
  51. else
  52. depthStencilSurfaceDesc.texture = textureHandle;
  53. }
  54. UINT32 numSurfaces = std::min(colorSurfacesList.size(), (UINT32)BS_MAX_MULTIPLE_RENDER_TARGETS);
  55. RENDER_TEXTURE_DESC desc;
  56. for (UINT32 i = 0; i < numSurfaces; i++)
  57. {
  58. RENDER_SURFACE_DESC surfaceDesc;
  59. surfaceDesc.face = 0;
  60. surfaceDesc.mipLevel = 0;
  61. surfaceDesc.numFaces = 1;
  62. MonoObject* surfaceObj = colorSurfacesList.get<MonoObject*>(i);
  63. ScriptTexture2D* scriptSurface = ScriptTexture2D::toNative(surfaceObj);
  64. if (scriptSurface != nullptr)
  65. {
  66. HTexture textureHandle = scriptSurface->getHandle();
  67. if (!textureHandle.isLoaded())
  68. {
  69. LOGERR("Render texture must be created using a fully loaded texture.");
  70. }
  71. else
  72. surfaceDesc.texture = textureHandle;
  73. }
  74. desc.colorSurfaces[i] = surfaceDesc;
  75. }
  76. desc.depthStencilSurface = depthStencilSurfaceDesc;
  77. SPtr<RenderTarget> tex = RenderTexture::create(desc);
  78. new (bs_alloc<ScriptRenderTexture2D>()) ScriptRenderTexture2D(tex, instance);
  79. }
  80. void ScriptRenderTexture2D::internal_getColorSurfaces(ScriptRenderTexture2D* thisPtr, MonoArray** value)
  81. {
  82. SPtr<RenderTexture> tex = thisPtr->getRenderTexture();
  83. UINT32 numColorSurfaces = BS_MAX_MULTIPLE_RENDER_TARGETS;
  84. ScriptArray outArray = ScriptArray::create<ScriptTexture2D>(numColorSurfaces);
  85. for (UINT32 i = 0; i < numColorSurfaces; i++)
  86. {
  87. HTexture colorTex = tex->getColorTexture(i);
  88. if (colorTex != nullptr)
  89. {
  90. ScriptTexture2D* scriptSurface;
  91. ScriptResourceManager::instance().getScriptResource(colorTex, &scriptSurface, true);
  92. outArray.set<MonoObject*>(i, scriptSurface->getManagedInstance());
  93. }
  94. else
  95. outArray.set<MonoObject*>(i, nullptr);
  96. }
  97. *value = outArray.getInternal();
  98. }
  99. void ScriptRenderTexture2D::internal_getDepthStencilSurface(ScriptRenderTexture2D* thisPtr, MonoObject** value)
  100. {
  101. SPtr<RenderTexture> tex = thisPtr->getRenderTexture();
  102. HTexture depthTex = tex->getDepthStencilTexture();
  103. ScriptTexture2D* scriptSurface;
  104. ScriptResourceManager::instance().getScriptResource(depthTex, &scriptSurface, true);
  105. *value = scriptSurface->getManagedInstance();
  106. }
  107. }