/src/ogre/OgreMain/src/OgreHardwarePixelBuffer.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreHardwarePixelBuffer.h" |
30 | | #include "OgreImage.h" |
31 | | |
32 | | namespace Ogre |
33 | | { |
34 | | |
35 | | //----------------------------------------------------------------------------- |
36 | | HardwarePixelBuffer::HardwarePixelBuffer(uint32 width, uint32 height, uint32 depth, |
37 | | PixelFormat format, |
38 | | HardwareBuffer::Usage usage, bool useShadowBuffer): |
39 | 0 | HardwareBuffer(usage, useShadowBuffer), |
40 | 0 | mCurrentLockOptions(), mWidth(width), mHeight(height), mDepth(depth), |
41 | 0 | mFormat(format) |
42 | 0 | { |
43 | | // Default |
44 | 0 | mRowPitch = mWidth; |
45 | 0 | mSlicePitch = mHeight*mWidth; |
46 | 0 | mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat); |
47 | 0 | } |
48 | | |
49 | | //----------------------------------------------------------------------------- |
50 | | HardwarePixelBuffer::~HardwarePixelBuffer() |
51 | 0 | { |
52 | 0 | if (mUsage & TU_RENDERTARGET) |
53 | 0 | { |
54 | | // Delete all render targets that are not yet deleted via _clearSliceRTT because the rendertarget |
55 | | // was deleted by the user. |
56 | 0 | for (auto rt : mSliceTRT) |
57 | 0 | { |
58 | 0 | if (rt) |
59 | 0 | Root::getSingleton().getRenderSystem()->destroyRenderTarget(rt->getName()); |
60 | 0 | } |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | //----------------------------------------------------------------------------- |
65 | | void* HardwarePixelBuffer::lock(size_t offset, size_t length, LockOptions options) |
66 | 0 | { |
67 | 0 | OgreAssert(!isLocked(), "already locked"); |
68 | 0 | OgreAssert(offset == 0 && length == mSizeInBytes, "must lock box or entire buffer"); |
69 | | |
70 | 0 | Box myBox(0, 0, 0, mWidth, mHeight, mDepth); |
71 | 0 | const PixelBox &rv = lock(myBox, options); |
72 | 0 | return rv.data; |
73 | 0 | } |
74 | | |
75 | | //----------------------------------------------------------------------------- |
76 | | const PixelBox& HardwarePixelBuffer::lock(const Box& lockBox, LockOptions options) |
77 | 0 | { |
78 | 0 | if (mShadowBuffer) |
79 | 0 | { |
80 | 0 | if (options != HBL_READ_ONLY) |
81 | 0 | { |
82 | | // we have to assume a read / write lock so we use the shadow buffer |
83 | | // and tag for sync on unlock() |
84 | 0 | mShadowUpdated = true; |
85 | 0 | } |
86 | |
|
87 | 0 | mCurrentLock = static_cast<HardwarePixelBuffer*>(mShadowBuffer.get())->lock(lockBox, options); |
88 | 0 | } |
89 | 0 | else |
90 | 0 | { |
91 | 0 | mCurrentLockOptions = options; |
92 | 0 | mLockedBox = lockBox; |
93 | | // Lock the real buffer if there is no shadow buffer |
94 | 0 | mCurrentLock = lockImpl(lockBox, options); |
95 | 0 | mIsLocked = true; |
96 | 0 | } |
97 | |
|
98 | 0 | return mCurrentLock; |
99 | 0 | } |
100 | | |
101 | | //----------------------------------------------------------------------------- |
102 | | const PixelBox& HardwarePixelBuffer::getCurrentLock() |
103 | 0 | { |
104 | 0 | OgreAssert(isLocked(), "buffer not locked"); |
105 | 0 | return mCurrentLock; |
106 | 0 | } |
107 | | |
108 | | //----------------------------------------------------------------------------- |
109 | | /// Internal implementation of lock() |
110 | | void* HardwarePixelBuffer::lockImpl(size_t offset, size_t length, LockOptions options) |
111 | 0 | { |
112 | 0 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "lockImpl(offset,length) is not valid for PixelBuffers and should never be called", |
113 | 0 | "HardwarePixelBuffer::lockImpl"); |
114 | 0 | } |
115 | | |
116 | | //----------------------------------------------------------------------------- |
117 | | |
118 | | void HardwarePixelBuffer::blit(const HardwarePixelBufferSharedPtr &src, const Box &srcBox, const Box &dstBox) |
119 | 0 | { |
120 | 0 | if(isLocked() || src->isLocked()) |
121 | 0 | { |
122 | 0 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Source and destination buffer may not be locked!"); |
123 | 0 | } |
124 | 0 | if(src.get() == this) |
125 | 0 | { |
126 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Source must not be the same object"); |
127 | 0 | } |
128 | | |
129 | 0 | LockOptions method = HBL_WRITE_ONLY; |
130 | 0 | if (dstBox.left == 0 && dstBox.top == 0 && dstBox.front == 0 && dstBox.right == mWidth && |
131 | 0 | dstBox.bottom == mHeight && dstBox.back == mDepth) |
132 | | // Entire buffer -- we can discard the previous contents |
133 | 0 | method = HBL_DISCARD; |
134 | |
|
135 | 0 | src->blitToMemory(srcBox, lock(dstBox, method)); |
136 | 0 | unlock(); |
137 | 0 | } |
138 | | //----------------------------------------------------------------------------- |
139 | | void HardwarePixelBuffer::blit(const HardwarePixelBufferSharedPtr &src) |
140 | 0 | { |
141 | 0 | blit(src, Box(src->getSize()), Box(getSize())); |
142 | 0 | } |
143 | | //----------------------------------------------------------------------------- |
144 | | void HardwarePixelBuffer::readData(size_t offset, size_t length, void* pDest) |
145 | 0 | { |
146 | | // allow easy full buffer reads |
147 | 0 | if (offset == 0 && length == mSizeInBytes) |
148 | 0 | { |
149 | 0 | Box box(0, 0, 0, mWidth, mHeight, mDepth); |
150 | 0 | blitToMemory(box, PixelBox(box, mFormat, pDest)); |
151 | 0 | return; |
152 | 0 | } |
153 | | |
154 | | // TODO |
155 | 0 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, |
156 | 0 | "Reading a byte range is not implemented. Use blitToMemory.", |
157 | 0 | "HardwarePixelBuffer::readData"); |
158 | 0 | } |
159 | | //----------------------------------------------------------------------------- |
160 | | |
161 | | void HardwarePixelBuffer::writeData(size_t offset, size_t length, const void* pSource, |
162 | | bool discardWholeBuffer) |
163 | 0 | { |
164 | | // allow easy full buffer updates |
165 | 0 | if (offset == 0 && length == mSizeInBytes) |
166 | 0 | { |
167 | 0 | Box box(0, 0, 0, mWidth, mHeight, mDepth); |
168 | | // we know pSource will not be written to |
169 | 0 | blitFromMemory(PixelBox(box, mFormat, const_cast<void*>(pSource)), box); |
170 | 0 | return; |
171 | 0 | } |
172 | | |
173 | | // TODO |
174 | 0 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, |
175 | 0 | "Writing a byte range is not implemented. Use blitFromMemory.", |
176 | 0 | "HardwarePixelBuffer::writeData"); |
177 | 0 | } |
178 | | //----------------------------------------------------------------------------- |
179 | | |
180 | | RenderTexture *HardwarePixelBuffer::getRenderTarget(size_t zoffset) const |
181 | 0 | { |
182 | 0 | assert(mUsage & TU_RENDERTARGET); |
183 | 0 | OgreAssert(zoffset < mSliceTRT.size(), "out of range"); |
184 | 0 | return mSliceTRT[zoffset]; |
185 | 0 | } |
186 | | //----------------------------------------------------------------------------- |
187 | | |
188 | | void HardwarePixelBuffer::_clearSliceRTT(size_t zoffset) |
189 | 0 | { |
190 | 0 | if(zoffset < mSliceTRT.size()) |
191 | 0 | mSliceTRT[zoffset] = NULL; |
192 | 0 | } |
193 | | |
194 | | |
195 | | String HardwarePixelBuffer::getNameForRenderTexture(const String& parentName, uint32 layer) const |
196 | 0 | { |
197 | | // the parent texture can either have multiple slices or faces - so we can call both layer here |
198 | 0 | return StringUtil::format("%s:layer%d", parentName.c_str(), layer); |
199 | 0 | } |
200 | | } |