Example #1
0
    void DirectXTexture::createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format)
    {
        destroy();

        mInternalUsage = 0;
        mInternalFormat = D3DFMT_UNKNOWN;

        mSize.set(_width, _height);
        mTextureUsage = _usage;
        mPixelFormat = _format;
        mInternalPool = D3DPOOL_MANAGED;

        if (mTextureUsage == TextureUsage::RenderTarget)
        {
            mInternalUsage |= D3DUSAGE_RENDERTARGET;
            mInternalPool = D3DPOOL_DEFAULT;
        }
        else if (mTextureUsage == TextureUsage::Dynamic)
            mInternalUsage |= D3DUSAGE_DYNAMIC;
        else if (mTextureUsage == TextureUsage::Stream)
            mInternalUsage |= D3DUSAGE_DYNAMIC;

        if (mPixelFormat == PixelFormat::R8G8B8A8)
        {
            mInternalFormat = D3DFMT_A8R8G8B8;
            mNumElemBytes = 4;
        }
        else if (mPixelFormat == PixelFormat::R8G8B8)
        {
            mInternalFormat = D3DFMT_R8G8B8;
            mNumElemBytes = 3;
        }
        else if (mPixelFormat == PixelFormat::L8A8)
        {
            mInternalFormat = D3DFMT_A8L8;
            mNumElemBytes = 2;
        }
        else if (mPixelFormat == PixelFormat::L8)
        {
            mInternalFormat = D3DFMT_L8;
            mNumElemBytes = 1;
        }
        else
        {
            MYGUI_PLATFORM_EXCEPT("Creating texture with unknown pixel formal.");
        }

        HRESULT result = mpD3DDevice->CreateTexture(mSize.width, mSize.height, 1, mInternalUsage, mInternalFormat, mInternalPool, &mpTexture, NULL);
        if (FAILED(result))
        {
            MYGUI_PLATFORM_EXCEPT("Failed to create texture (error code " << result <<"): size '" << mSize <<
                "' internal usage '" << mInternalUsage <<
                "' internal format '" << mInternalFormat << "'."
                );
        }

    }
	void OpenGLESVertexBuffer::create()
	{
		MYGUI_PLATFORM_ASSERT(!mBufferID, "Vertex buffer already exist");

		mSizeInBytes = mNeedVertexCount * sizeof(MyGUI::Vertex);
		void* data = 0;

		glGenBuffers(1, (GLuint*)&mBufferID); //wdy
        CHECK_GL_ERROR_DEBUG();
		glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
        CHECK_GL_ERROR_DEBUG();
		glBufferData(GL_ARRAY_BUFFER, mSizeInBytes, data, GL_STREAM_DRAW);
        CHECK_GL_ERROR_DEBUG();
        
        //MYGUI_LOG(Info, mBufferID);
		

		// check data size in VBO is same as input array, if not return 0 and delete VBO
		int bufferSize = 0;
		glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, (GLint*)&bufferSize); //wdy
        CHECK_GL_ERROR_DEBUG();
		if (mSizeInBytes != (size_t)bufferSize)
		{
			destroy();
			MYGUI_PLATFORM_EXCEPT("Data size is mismatch with input array");
		}

		glBindBuffer(GL_ARRAY_BUFFER, 0);
        CHECK_GL_ERROR_DEBUG();
	}
	void DirectXVertexBuffer::unlock()
	{
		HRESULT result = mpBuffer->Unlock();
		if (FAILED(result))
		{
			MYGUI_PLATFORM_EXCEPT("Failed to unlock vertex buffer (error code " << result << ").");
		}
	}
	Vertex* DirectXVertexBuffer::lock()
	{
		void* lockPtr = nullptr;
		HRESULT result = mpBuffer->Lock(0, 0, (void**)&lockPtr, 0);
		if (FAILED(result))
		{
			MYGUI_PLATFORM_EXCEPT("Failed to lock vertex buffer (error code " << result << ").");
		}
		return (Vertex*)lockPtr;
	}
Example #5
0
    void DirectXTexture::unlock()
    {
        HRESULT result = mpTexture->UnlockRect(0);
        if (FAILED(result))
        {
            MYGUI_PLATFORM_EXCEPT("Failed to unlock texture (error code " << result << ").");
        }

        mLock = false;
    }
	Vertex* MyGUI_EFFVertexBuffer::lock()
	{
		void* lockPtr = nullptr;
		HRESULT result = mpBuffer->LockBuffer(0, 0, (void**)&lockPtr, 0);
		if (FAILED(result))
		{
			MYGUI_PLATFORM_EXCEPT("Failed to lock vertex buffer (error code " << result << ").");
		}
		return reinterpret_cast<Vertex*>(lockPtr);
	}
Example #7
0
 void DirectXTexture::deviceRestore()
 {
     if (mInternalPool == D3DPOOL_DEFAULT)
     {
         HRESULT result = mpD3DDevice->CreateTexture(mSize.width, mSize.height, 1, mInternalUsage, mInternalFormat, D3DPOOL_DEFAULT, &mpTexture, NULL);
         if (FAILED(result))
         {
             MYGUI_PLATFORM_EXCEPT("Failed to recreate texture on device restore (error code " << result << ").");
         }
     }
 }
	void* OpenGLTexture::lock(TextureUsage _access)
	{
		MYGUI_PLATFORM_ASSERT(mTextureID, "Texture is not created");

		if (_access == TextureUsage::Read)
		{
			glBindTexture(GL_TEXTURE_2D, mTextureID);

			mBuffer = new unsigned char[mDataSize];
			glGetTexImage(GL_TEXTURE_2D, 0, mPixelFormat, GL_UNSIGNED_BYTE, mBuffer);

			mLock = false;

			return mBuffer;
		}

		// bind the texture
		glBindTexture(GL_TEXTURE_2D, mTextureID);
		if (!OpenGLRenderManager::getInstance().isPixelBufferObjectSupported())
		{
			//Fallback if PBO's are not supported
			mBuffer = new unsigned char[mDataSize];
		}
		else
		{
			// bind the PBO
			glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, mPboID);
			
			// Note that glMapBufferARB() causes sync issue.
			// If GPU is working with this buffer, glMapBufferARB() will wait(stall)
			// until GPU to finish its job. To avoid waiting (idle), you can call
			// first glBufferDataARB() with NULL pointer before glMapBufferARB().
			// If you do that, the previous data in PBO will be discarded and
			// glMapBufferARB() returns a new allocated pointer immediately
			// even if GPU is still working with the previous data.
			glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, mDataSize, 0, mUsage);

			// map the buffer object into client's memory
			mBuffer = (GLubyte*)glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, mAccess);
			if (!mBuffer)
			{
				glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
				glBindTexture(GL_TEXTURE_2D, 0);
				MYGUI_PLATFORM_EXCEPT("Error texture lock");
			}
		}

		mLock = true;

		return mBuffer;
	}
Example #9
0
    void* DirectXTexture::lock(TextureUsage _access)
    {
        D3DLOCKED_RECT d3dlr;
        int lockFlag = (_access == TextureUsage::Write) ? D3DLOCK_DISCARD : D3DLOCK_READONLY;

        HRESULT result = mpTexture->LockRect(0, &d3dlr, NULL, lockFlag);
        if (FAILED(result))
        {
            MYGUI_PLATFORM_EXCEPT("Failed to lock texture (error code " << result << ").");
        }

        mLock = true;
        return d3dlr.pBits;
    }
Example #10
0
    void DirectXTexture::loadFromFile(const std::string& _filename)
    {
        destroy();
        mTextureUsage = TextureUsage::Default;
        mPixelFormat = PixelFormat::R8G8B8A8;
        mNumElemBytes = 4;

        std::string fullname = DirectXDataManager::getInstance().getDataPath(_filename);

        D3DXIMAGE_INFO info;
        D3DXGetImageInfoFromFile(fullname.c_str(), &info);

        if (info.Format == D3DFMT_A8R8G8B8)
        {
            mPixelFormat = PixelFormat::R8G8B8A8;
            mNumElemBytes = 4;
        }
        else if (info.Format == D3DFMT_R8G8B8)
        {
            mPixelFormat = PixelFormat::R8G8B8;
            mNumElemBytes = 3;
        }
        else if (info.Format == D3DFMT_A8L8)
        {
            mPixelFormat = PixelFormat::L8A8;
            mNumElemBytes = 2;
        }
        else if (info.Format == D3DFMT_L8)
        {
            mPixelFormat = PixelFormat::L8;
            mNumElemBytes = 1;
        }

        mSize.set(info.Width, info.Height);
        HRESULT result = D3DXCreateTextureFromFile(mpD3DDevice, fullname.c_str(), &mpTexture);
        if (FAILED(result))
        {
            MYGUI_PLATFORM_EXCEPT("Failed to load texture '" << _filename <<
                "' (error code " << result <<
                "): size '" << mSize <<
                "' format '" << info.Format << "'."
                );
        }
    }
Example #11
0
    void DirectXTexture::destroy()
    {
        if (mRenderTarget != nullptr)
        {
            delete mRenderTarget;
            mRenderTarget = nullptr;
        }

        if (mpTexture != nullptr)
        {
            int nNewRefCount = mpTexture->Release();

            if (nNewRefCount > 0)
            {
                MYGUI_PLATFORM_EXCEPT("The texture object failed to cleanup properly.\n"
                    "Release() returned a reference count of '" << nNewRefCount << "'."
                    );
            }

            mpTexture = nullptr;
        }
    }
	void OpenGLTexture::createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format, void* _data)
	{
		MYGUI_PLATFORM_ASSERT(!mTextureID, "Texture already exist");

		//FIXME перенести в метод
		mInternalPixelFormat = 0;
		mPixelFormat = 0;
		mNumElemBytes = 0;
		if (_format == PixelFormat::L8)
		{
			mInternalPixelFormat = GL_LUMINANCE8;
			mPixelFormat = GL_LUMINANCE;
			mNumElemBytes = 1;
		}
		else if (_format == PixelFormat::L8A8)
		{
			mInternalPixelFormat = GL_LUMINANCE8_ALPHA8;
			mPixelFormat = GL_LUMINANCE_ALPHA;
			mNumElemBytes = 2;
		}
		else if (_format == PixelFormat::R8G8B8)
		{
			mInternalPixelFormat = GL_RGB8;
			mPixelFormat = GL_BGR;
			mNumElemBytes = 3;
		}
		else if (_format == PixelFormat::R8G8B8A8)
		{
			mInternalPixelFormat = GL_RGBA8;
			mPixelFormat = GL_BGRA;
			mNumElemBytes = 4;
		}
		else
		{
			MYGUI_PLATFORM_EXCEPT("format not support");
		}

		mWidth = _width;
		mHeight = _height;
		mDataSize = _width * _height * mNumElemBytes;
		setUsage(_usage);
		//MYGUI_PLATFORM_ASSERT(mUsage, "usage format not support");

		mOriginalFormat = _format;
		mOriginalUsage = _usage;

		// Set unpack alignment to one byte
		int alignment = 0;
		glGetIntegerv( GL_UNPACK_ALIGNMENT, &alignment );
		glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );

		// создаем тукстуру
		glGenTextures(1, &mTextureID);
		glBindTexture(GL_TEXTURE_2D, mTextureID);
		// Set texture parameters
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexImage2D(GL_TEXTURE_2D, 0, mInternalPixelFormat, mWidth, mHeight, 0, mPixelFormat, GL_UNSIGNED_BYTE, (GLvoid*)_data);
		glBindTexture(GL_TEXTURE_2D, 0);

		// Restore old unpack alignment
		glPixelStorei( GL_UNPACK_ALIGNMENT, alignment );

		if (!_data)
		{
			//создаем текстурнный буфер
			glGenBuffersARB(1, &mPboID);
			glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, mPboID);
			glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, mDataSize, 0, mUsage);
			glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
		}
	}