Przeglądaj źródła

Added C# Texture and implemented Texture2D

Marko Pintera 11 lat temu
rodzic
commit
25aca52bc3

+ 1 - 1
MBansheeEngine/MBansheeEngine.csproj

@@ -110,8 +110,8 @@
     <Compile Include="SerializeField.cs" />
     <Compile Include="SerializeField.cs" />
     <Compile Include="SpriteTexture.cs" />
     <Compile Include="SpriteTexture.cs" />
     <Compile Include="StringTable.cs" />
     <Compile Include="StringTable.cs" />
+    <Compile Include="Texture.cs" />
     <Compile Include="Texture2D.cs" />
     <Compile Include="Texture2D.cs" />
-    <Compile Include="TextureFormat.cs" />
     <Compile Include="Math\Vector2.cs" />
     <Compile Include="Math\Vector2.cs" />
     <Compile Include="Math\Vector3.cs" />
     <Compile Include="Math\Vector3.cs" />
     <Compile Include="Math\Vector4.cs" />
     <Compile Include="Math\Vector4.cs" />

+ 4 - 0
MBansheeEngine/PixelData.cs

@@ -122,6 +122,10 @@ namespace BansheeEngine
             }
             }
         }
         }
 
 
+        // Only for use by native code
+        private PixelData()
+        { }
+
         public PixelData(PixelVolume volume, PixelFormat format)
         public PixelData(PixelVolume volume, PixelFormat format)
         {
         {
             Internal_CreateInstance(this, volume, format);
             Internal_CreateInstance(this, volume, format);

+ 113 - 0
MBansheeEngine/Texture.cs

@@ -0,0 +1,113 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace BansheeEngine
+{
+    public class Texture : Resource
+    {
+        public PixelFormat PixelFormat
+        {
+            get
+            {
+                PixelFormat value;
+                Internal_GetPixelFormat(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        public TextureUsage Usage
+        {
+            get
+            {
+                TextureUsage value;
+                Internal_GetUsage(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        public int Width
+        {
+            get
+            {
+                int value;
+                Internal_GetWidth(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        public int Height
+        {
+            get
+            {
+                int value;
+                Internal_GetHeight(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        public bool GammaCorrection
+        {
+            get
+            {
+                bool value;
+                Internal_GetGammaCorrection(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        public int SampleCount
+        {
+            get
+            {
+                int value;
+                Internal_GetSampleCount(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        public int MipmapCount
+        {
+            get
+            {
+                int value;
+                Internal_GetMipmapCount(mCachedPtr, out value);
+                return value;
+            }
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetPixelFormat(IntPtr thisPtr, out PixelFormat value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetUsage(IntPtr thisPtr, out TextureUsage value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetWidth(IntPtr thisPtr, out int value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetHeight(IntPtr thisPtr, out int value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetGammaCorrection(IntPtr thisPtr, out bool value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetSampleCount(IntPtr thisPtr, out int value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetMipmapCount(IntPtr thisPtr, out int value);
+    }
+
+    // Note: Do not modify IDs as they must match TextureUsage C++ enum
+    public enum TextureUsage
+    {
+        Default = 0x1,
+        Dynamic = 0x2,
+        Render = 0x200,
+        DepthStencil = 0x400,
+        LoadStore = 0x800,
+        CPUCached = 0x1000
+    }
+}

+ 28 - 4
MBansheeEngine/Texture2D.cs

@@ -3,20 +3,44 @@ using System.Runtime.CompilerServices;
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
-    public sealed class Texture2D : Resource
+    public sealed class Texture2D : Texture
     {
     {
         // For internal use by the runtime
         // For internal use by the runtime
         private Texture2D()
         private Texture2D()
+        { }
+
+        public Texture2D(PixelFormat format, int width, int height, TextureUsage usage = TextureUsage.Default, 
+            int numSamples = 1, bool hasMipmaps = false, bool gammaCorrection = false)
+        {
+            Internal_CreateInstance(this, format, width, height, usage, numSamples, hasMipmaps, gammaCorrection);
+        }
+
+        public PixelData GetPixels(int mipLevel = 0)
         {
         {
+            return Internal_GetPixels(mCachedPtr, mipLevel);
+        }
 
 
+        public void SetPixels(PixelData data, int mipLevel = 0)
+        {
+            Internal_SetPixels(mCachedPtr, data, mipLevel);
         }
         }
 
 
-        public Texture2D(TextureFormat format, int width, int height, bool hasMipmaps = false, bool gammaCorrection = false)
+        public AsyncOp GetGPUPixels(int mipLevel = 0)
         {
         {
-            Internal_CreateInstance(this, format, width, height, hasMipmaps, gammaCorrection);
+            return Internal_GetGPUPixels(mCachedPtr, mipLevel);
         }
         }
 
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern void Internal_CreateInstance(Texture2D instance, TextureFormat format, int width, int height, bool hasMipmaps, bool gammaCorrection);
+        private static extern void Internal_CreateInstance(Texture2D instance, PixelFormat format, int width, 
+            int height, TextureUsage usage, int numSamples, bool hasMipmaps, bool gammaCorrection);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern PixelData Internal_GetPixels(IntPtr thisPtr, int mipLevel);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern AsyncOp Internal_GetGPUPixels(IntPtr thisPtr, int mipLevel);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetPixels(IntPtr thisPtr, PixelData data, int mipLevel);
     }
     }
 }
 }

+ 0 - 15
MBansheeEngine/TextureFormat.cs

@@ -1,15 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace BansheeEngine
-{
-    // TODO - Add more formats
-    public enum TextureFormat
-    {
-        RGB,
-        RGBA
-    }
-}

+ 5 - 1
SBansheeEngine/Include/BsScriptPixelData.h

@@ -14,10 +14,14 @@ namespace BansheeEngine
 
 
 		PixelDataPtr getInternalValue() const { return mPixelData; }
 		PixelDataPtr getInternalValue() const { return mPixelData; }
 
 
+		static MonoObject* create(const PixelDataPtr& pixelData);
+
 	private:
 	private:
-		ScriptPixelData(MonoObject* managedInstance, const PixelDataPtr& pixelData);
+		ScriptPixelData(MonoObject* managedInstance);
 		~ScriptPixelData();
 		~ScriptPixelData();
 
 
+		void initialize(const PixelDataPtr& pixelData);
+
 		static void internal_createInstance(MonoObject* instance, PixelVolume volume, PixelFormat format);
 		static void internal_createInstance(MonoObject* instance, PixelVolume volume, PixelFormat format);
 		static void internal_getPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value);
 		static void internal_getPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value);
 		static void internal_setPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color value);
 		static void internal_setPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color value);

+ 2 - 2
SBansheeEngine/Include/BsScriptResourceManager.h

@@ -15,13 +15,13 @@ namespace BansheeEngine
 		 * @note Throws an exception if resource for the handle already exists.
 		 * @note Throws an exception if resource for the handle already exists.
 		 * 		 Creates a new managed instance of the object.
 		 * 		 Creates a new managed instance of the object.
 		 */
 		 */
-		ScriptTexture2D* createScriptTexture(const HTexture& resourceHandle);
+		ScriptTexture2D* createScriptTexture2D(const HTexture& resourceHandle);
 
 
 		/**
 		/**
 		 * @note Throws an exception if resource for the handle already exists.
 		 * @note Throws an exception if resource for the handle already exists.
 		 * 		 Initializes the ScriptResource with an existing managed instance.
 		 * 		 Initializes the ScriptResource with an existing managed instance.
 		 */
 		 */
-		ScriptTexture2D* createScriptTexture(MonoObject* existingInstance, const HTexture& resourceHandle);
+		ScriptTexture2D* createScriptTexture2D(MonoObject* existingInstance, const HTexture& resourceHandle);
 
 
 		/**
 		/**
 		 * @note Throws an exception if resource for the handle already exists.
 		 * @note Throws an exception if resource for the handle already exists.

+ 38 - 0
SBansheeEngine/Include/BsScriptTexture.h

@@ -0,0 +1,38 @@
+#pragma once
+
+#include "BsScriptEnginePrerequisites.h"
+#include "BsScriptResource.h"
+#include "BsPixelData.h"
+#include "BsTexture.h"
+
+namespace BansheeEngine
+{
+	class BS_SCR_BE_EXPORT ScriptTextureBase : public ScriptResourceBase
+	{
+	protected:
+		friend class ScriptResourceManager;
+
+		ScriptTextureBase(MonoObject* instance)
+			:ScriptResourceBase(instance)
+		{ }
+
+		virtual ~ScriptTextureBase() {}
+	};
+
+	class BS_SCR_BE_EXPORT ScriptTexture : public ScriptObject <ScriptTexture, ScriptTextureBase>
+	{
+	public:
+		SCRIPT_OBJ(BansheeEngineAssemblyName, "BansheeEngine", "Texture")
+
+	private:
+		ScriptTexture(MonoObject* instance);
+
+		static void internal_getPixelFormat(ScriptTexture* thisPtr, PixelFormat* value);
+		static void internal_getUsage(ScriptTexture* thisPtr, TextureUsage* value);
+		static void internal_getWidth(ScriptTexture* thisPtr, int* value);
+		static void internal_getHeight(ScriptTexture* thisPtr, int* value);
+		static void internal_getGammaCorrection(ScriptTexture* thisPtr, bool* value);
+		static void internal_getSampleCount(ScriptTexture* thisPtr, int* value);
+		static void internal_getMipmapCount(ScriptTexture* thisPtr, int* value);
+	};
+}

+ 7 - 3
SBansheeEngine/Include/BsScriptTexture2D.h

@@ -1,13 +1,13 @@
 #pragma once
 #pragma once
 
 
 #include "BsScriptEnginePrerequisites.h"
 #include "BsScriptEnginePrerequisites.h"
-#include "BsScriptResource.h"
+#include "BsScriptTexture.h"
 #include "BsScriptObject.h"
 #include "BsScriptObject.h"
 #include "BsTexture.h"
 #include "BsTexture.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
-	class BS_SCR_BE_EXPORT ScriptTexture2D : public ScriptObject<ScriptTexture2D, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptTexture2D : public ScriptObject<ScriptTexture2D, ScriptTextureBase>
 	{
 	{
 	public:
 	public:
 		SCRIPT_OBJ(BansheeEngineAssemblyName, "BansheeEngine", "Texture2D")
 		SCRIPT_OBJ(BansheeEngineAssemblyName, "BansheeEngine", "Texture2D")
@@ -19,7 +19,11 @@ namespace BansheeEngine
 	private:
 	private:
 		friend class ScriptResourceManager;
 		friend class ScriptResourceManager;
 
 
-		static void internal_createInstance(MonoObject* instance, UINT32 format, UINT32 width, UINT32 height, bool hasMipmaps, bool gammaCorrection);
+		static void internal_createInstance(MonoObject* instance, PixelFormat format, UINT32 width, 
+			UINT32 height, TextureUsage usage, UINT32 numSamples, bool hasMipmaps, bool gammaCorrection);
+		static MonoObject* internal_getPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel);
+		static MonoObject* internal_getGPUPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel);
+		static void internal_setPixels(ScriptTexture2D* thisPtr, MonoObject* data, UINT32 mipLevel);
 
 
 		ScriptTexture2D(MonoObject* instance, const HTexture& texture);
 		ScriptTexture2D(MonoObject* instance, const HTexture& texture);
 
 

+ 2 - 0
SBansheeEngine/SBansheeEngine.vcxproj

@@ -291,6 +291,7 @@
     <ClInclude Include="Include\BsScriptSerializableProperty.h" />
     <ClInclude Include="Include\BsScriptSerializableProperty.h" />
     <ClInclude Include="Include\BsScriptSpriteTexture.h" />
     <ClInclude Include="Include\BsScriptSpriteTexture.h" />
     <ClInclude Include="Include\BsScriptStringTable.h" />
     <ClInclude Include="Include\BsScriptStringTable.h" />
+    <ClInclude Include="Include\BsScriptTexture.h" />
     <ClInclude Include="Include\BsScriptTexture2D.h" />
     <ClInclude Include="Include\BsScriptTexture2D.h" />
     <ClInclude Include="Include\BsScriptGUIContent.h" />
     <ClInclude Include="Include\BsScriptGUIContent.h" />
     <ClInclude Include="Include\BsManagedSerializableObjectInfo.h" />
     <ClInclude Include="Include\BsManagedSerializableObjectInfo.h" />
@@ -353,6 +354,7 @@
     <ClCompile Include="Source\BsScriptSerializableProperty.cpp" />
     <ClCompile Include="Source\BsScriptSerializableProperty.cpp" />
     <ClCompile Include="Source\BsScriptSpriteTexture.cpp" />
     <ClCompile Include="Source\BsScriptSpriteTexture.cpp" />
     <ClCompile Include="Source\BsScriptStringTable.cpp" />
     <ClCompile Include="Source\BsScriptStringTable.cpp" />
+    <ClCompile Include="Source\BsScriptTexture.cpp" />
     <ClCompile Include="Source\BsScriptTexture2D.cpp" />
     <ClCompile Include="Source\BsScriptTexture2D.cpp" />
     <ClCompile Include="Source\BsScriptGUIContent.cpp" />
     <ClCompile Include="Source\BsScriptGUIContent.cpp" />
     <ClCompile Include="Source\BsScriptVector2I.cpp" />
     <ClCompile Include="Source\BsScriptVector2I.cpp" />

+ 6 - 0
SBansheeEngine/SBansheeEngine.vcxproj.filters

@@ -246,6 +246,9 @@
     <ClInclude Include="Include\BsScriptAsyncOp.h">
     <ClInclude Include="Include\BsScriptAsyncOp.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="Include\BsScriptTexture.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsScriptTexture2D.cpp">
     <ClCompile Include="Source\BsScriptTexture2D.cpp">
@@ -425,5 +428,8 @@
     <ClCompile Include="Source\BsScriptAsyncOp.cpp">
     <ClCompile Include="Source\BsScriptAsyncOp.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="Source\BsScriptTexture.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 1 - 1
SBansheeEngine/Source/BsManagedSerializableField.cpp

@@ -413,7 +413,7 @@ namespace BansheeEngine
 					HTexture texture = static_resource_cast<Texture>(value);
 					HTexture texture = static_resource_cast<Texture>(value);
 					ScriptTexture2D* scriptResource = ScriptResourceManager::instance().getScriptTexture(texture);
 					ScriptTexture2D* scriptResource = ScriptResourceManager::instance().getScriptTexture(texture);
 					if(scriptResource == nullptr)
 					if(scriptResource == nullptr)
-						scriptResource = ScriptResourceManager::instance().createScriptTexture(texture);
+						scriptResource = ScriptResourceManager::instance().createScriptTexture2D(texture);
 
 
 					return scriptResource->getManagedInstance();
 					return scriptResource->getManagedInstance();
 				}
 				}

+ 19 - 3
SBansheeEngine/Source/BsScriptPixelData.cpp

@@ -9,8 +9,8 @@
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
-	ScriptPixelData::ScriptPixelData(MonoObject* managedInstance, const PixelDataPtr& pixelData)
-		:ScriptObject(managedInstance), mPixelData(pixelData)
+	ScriptPixelData::ScriptPixelData(MonoObject* managedInstance)
+		:ScriptObject(managedInstance)
 	{
 	{
 
 
 	}
 	}
@@ -37,12 +37,28 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_GetIsConsecutive", &ScriptPixelData::internal_getIsConsecutive);
 		metaData.scriptClass->addInternalCall("Internal_GetIsConsecutive", &ScriptPixelData::internal_getIsConsecutive);
 	}
 	}
 
 
+	void ScriptPixelData::initialize(const PixelDataPtr& pixelData)
+	{
+		mPixelData = pixelData;
+	}
+
+	MonoObject* ScriptPixelData::create(const PixelDataPtr& pixelData)
+	{
+		MonoObject* pixelDataObj = metaData.scriptClass->createInstance();
+
+		ScriptPixelData* scriptPixelData = ScriptPixelData::toNative(pixelDataObj);
+		scriptPixelData->initialize(pixelData);
+
+		return pixelDataObj;
+	}
+
 	void ScriptPixelData::internal_createInstance(MonoObject* instance, PixelVolume volume, PixelFormat format)
 	void ScriptPixelData::internal_createInstance(MonoObject* instance, PixelVolume volume, PixelFormat format)
 	{
 	{
 		PixelDataPtr pixelData = bs_shared_ptr<PixelData>(volume, format);
 		PixelDataPtr pixelData = bs_shared_ptr<PixelData>(volume, format);
 		pixelData->allocateInternalBuffer();
 		pixelData->allocateInternalBuffer();
 
 
-		ScriptPixelData* scriptPixelData = new (bs_alloc<ScriptPixelData>()) ScriptPixelData(instance, pixelData);
+		ScriptPixelData* scriptPixelData = new (bs_alloc<ScriptPixelData>()) ScriptPixelData(instance);
+		scriptPixelData->initialize(pixelData);
 	}
 	}
 
 
 	void ScriptPixelData::internal_getPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value)
 	void ScriptPixelData::internal_getPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value)

+ 4 - 4
SBansheeEngine/Source/BsScriptResourceManager.cpp

@@ -30,14 +30,14 @@ namespace BansheeEngine
 			BS_EXCEPT(InternalErrorException, "Cannot find managed Font class.");
 			BS_EXCEPT(InternalErrorException, "Cannot find managed Font class.");
 	}
 	}
 
 
-	ScriptTexture2D* ScriptResourceManager::createScriptTexture(const HTexture& resourceHandle)
+	ScriptTexture2D* ScriptResourceManager::createScriptTexture2D(const HTexture& resourceHandle)
 	{
 	{
 		MonoObject* monoInstance = mTextureClass->createInstance();
 		MonoObject* monoInstance = mTextureClass->createInstance();
 
 
-		return createScriptTexture(monoInstance, resourceHandle);
+		return createScriptTexture2D(monoInstance, resourceHandle);
 	}
 	}
 
 
-	ScriptTexture2D* ScriptResourceManager::createScriptTexture(MonoObject* instance, const HTexture& resourceHandle)
+	ScriptTexture2D* ScriptResourceManager::createScriptTexture2D(MonoObject* instance, const HTexture& resourceHandle)
 	{
 	{
 		const String& uuid = resourceHandle.getUUID();
 		const String& uuid = resourceHandle.getUUID();
 #if BS_DEBUG_MODE
 #if BS_DEBUG_MODE
@@ -139,7 +139,7 @@ namespace BansheeEngine
 		switch (resTypeID)
 		switch (resTypeID)
 		{
 		{
 		case TID_Texture:
 		case TID_Texture:
-			return createScriptTexture(static_resource_cast<Texture>(resource));
+			return createScriptTexture2D(static_resource_cast<Texture>(resource));
 		case TID_SpriteTexture:
 		case TID_SpriteTexture:
 			return createScriptSpriteTexture(static_resource_cast<SpriteTexture>(resource));
 			return createScriptSpriteTexture(static_resource_cast<SpriteTexture>(resource));
 		case TID_Font:
 		case TID_Font:

+ 67 - 0
SBansheeEngine/Source/BsScriptTexture.cpp

@@ -0,0 +1,67 @@
+#include "BsScriptTexture.h"
+#include "BsScriptMeta.h"
+#include "BsMonoField.h"
+#include "BsMonoClass.h"
+#include "BsMonoManager.h"
+
+namespace BansheeEngine
+{
+	ScriptTexture::ScriptTexture(MonoObject* instance)
+		:ScriptObject(instance)
+	{
+
+	}
+
+	void ScriptTexture::initRuntimeData()
+	{
+		metaData.scriptClass->addInternalCall("Internal_GetPixelFormat", &ScriptTexture::internal_getPixelFormat);
+		metaData.scriptClass->addInternalCall("Internal_GetUsage", &ScriptTexture::internal_getUsage);
+		metaData.scriptClass->addInternalCall("Internal_GetWidth", &ScriptTexture::internal_getWidth);
+		metaData.scriptClass->addInternalCall("Internal_GetHeight", &ScriptTexture::internal_getHeight);
+		metaData.scriptClass->addInternalCall("Internal_GetGammaCorrection", &ScriptTexture::internal_getGammaCorrection);
+		metaData.scriptClass->addInternalCall("Internal_GetSampleCount", &ScriptTexture::internal_getSampleCount);
+		metaData.scriptClass->addInternalCall("Internal_GetMipmapCount", &ScriptTexture::internal_getMipmapCount);
+	}
+
+	void ScriptTexture::internal_getPixelFormat(ScriptTexture* thisPtr, PixelFormat* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = texture->getFormat();
+	}
+
+	void ScriptTexture::internal_getUsage(ScriptTexture* thisPtr, TextureUsage* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = (TextureUsage)texture->getUsage();
+	}
+
+	void ScriptTexture::internal_getWidth(ScriptTexture* thisPtr, int* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = (TextureUsage)texture->getWidth();
+	}
+
+	void ScriptTexture::internal_getHeight(ScriptTexture* thisPtr, int* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = (TextureUsage)texture->getHeight();
+	}
+
+	void ScriptTexture::internal_getGammaCorrection(ScriptTexture* thisPtr, bool* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = (TextureUsage)texture->isHardwareGammaEnabled();
+	}
+
+	void ScriptTexture::internal_getSampleCount(ScriptTexture* thisPtr, int* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = (TextureUsage)texture->getMultisampleCount();
+	}
+
+	void ScriptTexture::internal_getMipmapCount(ScriptTexture* thisPtr, int* value)
+	{
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		*value = (TextureUsage)texture->getNumMipmaps();
+	}
+}

+ 56 - 17
SBansheeEngine/Source/BsScriptTexture2D.cpp

@@ -7,6 +7,11 @@
 #include "BsTexture.h"
 #include "BsTexture.h"
 #include "BsPixelUtil.h"
 #include "BsPixelUtil.h"
 #include "BsException.h"
 #include "BsException.h"
+#include "BsScriptPixelData.h"
+#include "BsScriptAsyncOp.h"
+#include "BsCoreThread.h"
+
+using namespace std::placeholders;
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
@@ -19,30 +24,64 @@ namespace BansheeEngine
 	void ScriptTexture2D::initRuntimeData()
 	void ScriptTexture2D::initRuntimeData()
 	{
 	{
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptTexture2D::internal_createInstance);
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptTexture2D::internal_createInstance);
+		metaData.scriptClass->addInternalCall("Internal_GetPixels", &ScriptTexture2D::internal_getPixels);
+		metaData.scriptClass->addInternalCall("Internal_GetGPUPixels", &ScriptTexture2D::internal_getGPUPixels);
+		metaData.scriptClass->addInternalCall("Internal_SetPixels", &ScriptTexture2D::internal_setPixels);
 	}
 	}
 
 
-	void ScriptTexture2D::internal_createInstance(MonoObject* instance, UINT32 format, UINT32 width, UINT32 height, bool hasMipmaps, bool gammaCorrection)
+	void ScriptTexture2D::internal_createInstance(MonoObject* instance, PixelFormat format, UINT32 width,
+		UINT32 height, TextureUsage usage, UINT32 numSamples, bool hasMipmaps, bool gammaCorrection)
 	{
 	{
-		PixelFormat texFormat = PF_R8G8B8;
-		switch(format)
-		{
-		case 0: // RGB
-			texFormat = PF_R8G8B8;
-			break;
-		case 1: // RGBA
-			texFormat = PF_R8G8B8A8;
-			break;
-		default:
-			BS_EXCEPT(InvalidParametersException, "Unsupported texture format");
-		}
-
 		int numMips = 0;
 		int numMips = 0;
 		if(hasMipmaps)
 		if(hasMipmaps)
-			numMips = PixelUtil::getMaxMipmaps(width, height, 1, texFormat);
+			numMips = PixelUtil::getMaxMipmaps(width, height, 1, format);
 
 
-		HTexture texture = Texture::create(TEX_TYPE_2D, width, height, numMips, texFormat, TU_STATIC, gammaCorrection);
+		HTexture texture = Texture::create(TEX_TYPE_2D, width, height, numMips, format, usage, gammaCorrection, numSamples);
 
 
-		ScriptResourceManager::instance().createScriptTexture(instance, texture);
+		ScriptResourceManager::instance().createScriptTexture2D(instance, texture);
+	}
+
+	MonoObject* ScriptTexture2D::internal_getPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
+	{
+		HTexture texture = thisPtr->mTexture;
+		UINT32 subresourceIdx = texture->mapToSubresourceIdx(0, mipLevel);
+
+		PixelDataPtr pixelData = thisPtr->mTexture->allocateSubresourceBuffer(subresourceIdx);
+
+		thisPtr->mTexture->readDataSim(*pixelData, mipLevel);
+
+		return ScriptPixelData::create(pixelData);
+	}
+
+	MonoObject* ScriptTexture2D::internal_getGPUPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
+	{
+		HTexture texture = thisPtr->mTexture;
+		UINT32 subresourceIdx = texture->mapToSubresourceIdx(0, mipLevel);
+
+		PixelDataPtr readData = texture->allocateSubresourceBuffer(subresourceIdx);
+
+		AsyncOp asyncOp = gCoreAccessor().readSubresource(texture.getInternalPtr(), subresourceIdx, readData);
+
+		std::function<MonoObject*(const AsyncOp&, const PixelDataPtr&)> asyncOpToMono =
+			[&](const AsyncOp& op, const PixelDataPtr& returnValue)
+		{
+			return ScriptPixelData::create(returnValue);
+		};
+
+		return ScriptAsyncOp::create(asyncOp, std::bind(asyncOpToMono, _1, readData));
+	}
+
+	void ScriptTexture2D::internal_setPixels(ScriptTexture2D* thisPtr, MonoObject* data, UINT32 mipLevel)
+	{
+		ScriptPixelData* scriptPixelData = ScriptPixelData::toNative(data);
+
+		if (scriptPixelData != nullptr)
+		{
+			HTexture texture = thisPtr->mTexture;
+			UINT32 subresourceIdx = texture->mapToSubresourceIdx(0, mipLevel);
+
+			gCoreAccessor().writeSubresource(texture.getInternalPtr(), subresourceIdx, scriptPixelData->getInternalValue());
+		}
 	}
 	}
 
 
 	void ScriptTexture2D::_onManagedInstanceDeleted()
 	void ScriptTexture2D::_onManagedInstanceDeleted()

+ 0 - 10
TODO.txt

@@ -9,16 +9,6 @@
 Optionally port PixelUtility to compressing, converting, generating mipmaps, applying gamma to PixelData
 Optionally port PixelUtility to compressing, converting, generating mipmaps, applying gamma to PixelData
  - Also getMaxMimaps and other methods for retrieving pixel format information
  - Also getMaxMimaps and other methods for retrieving pixel format information
 
 
- C# AsyncOp
- - blockUntilComplete doesn't actually wait for this command, but instead waits until entire queue is empty
-  - Modify CoreThreadAccesor so it uses playbackWithNotify instead of playback
-  - Internally keep a mutex + condition variable that can be referenced by every AsyncOp (they should be in a shared_ptr so that if CoreThreadAccesor maybe gets destroyed, they still remain)
-  - Every AsyncOp gets assigned an unique ID on creation (use an atomic counter?)
-  - Notify method in CoreThreadAccesor triggers the condition variable whenever a variable completes
-  - When notified AsyncOp checks its isCompleted variable
-  - This means I probably need to synchronize isCompleted (use atomic?)
-
-