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 "OgreD3D9RenderSystem.h"
29 #include "OgreD3D9HardwareBufferManager.h"
30 #include "OgreD3D9HardwareVertexBuffer.h"
31 #include "OgreD3D9HardwareIndexBuffer.h"
32 #include "OgreD3D9VertexDeclaration.h"
33 #include "OgreLogManager.h"
34 #include "OgreStringConverter.h"
35 #include "OgreException.h"
36 
37 namespace Ogre {
38     //-----------------------------------------------------------------------
D3D9HardwareBufferManager()39     D3D9HardwareBufferManager::D3D9HardwareBufferManager()
40     {
41     }
42     //-----------------------------------------------------------------------
~D3D9HardwareBufferManager()43     D3D9HardwareBufferManager::~D3D9HardwareBufferManager()
44     {
45         destroyAllDeclarations();
46         destroyAllBindings();
47     }
48     //-----------------------------------------------------------------------
49     HardwareVertexBufferSharedPtr
50     D3D9HardwareBufferManager::
createVertexBuffer(size_t vertexSize,size_t numVerts,HardwareBuffer::Usage usage,bool useShadowBuffer)51     createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage,
52         bool useShadowBuffer)
53     {
54         assert (numVerts > 0);
55 #if OGRE_D3D_MANAGE_BUFFERS
56         // Override shadow buffer setting; managed buffers are automatically
57         // backed by system memory
58         // Don't override shadow buffer if discardable, since then we use
59         // unmanaged buffers for speed (avoids write-through overhead)
60         // Don't override if we use directX9EX, since then we don't have managed
61         // pool. And creating non-write only default pool causes a performance warning.
62         if (useShadowBuffer && !(usage & HardwareBuffer::HBU_DISCARDABLE) &&
63             !D3D9RenderSystem::isDirectX9Ex())
64         {
65             useShadowBuffer = false;
66             // Also drop any WRITE_ONLY so we can read direct
67             if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
68             {
69                 usage = HardwareBuffer::HBU_DYNAMIC;
70             }
71             else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
72             {
73                 usage = HardwareBuffer::HBU_STATIC;
74             }
75         }
76         //If we have write only buffers in DirectX9Ex we will turn on the discardable flag.
77         //Otherwise Ogre will operates in far less framerate
78         if (D3D9RenderSystem::isDirectX9Ex() && (usage & HardwareBuffer::HBU_WRITE_ONLY))
79         {
80             usage = (HardwareBuffer::Usage)
81                 ((unsigned int)usage | (unsigned int)HardwareBuffer::HBU_DISCARDABLE);
82         }
83 #endif
84         D3D9HardwareVertexBuffer* vbuf = OGRE_NEW D3D9HardwareVertexBuffer(
85             this, vertexSize, numVerts, usage, false, useShadowBuffer);
86         {
87                     OGRE_LOCK_MUTEX(mVertexBuffersMutex);
88             mVertexBuffers.insert(vbuf);
89         }
90         return HardwareVertexBufferSharedPtr(vbuf);
91     }
92     //-----------------------------------------------------------------------
93     HardwareIndexBufferSharedPtr
94     D3D9HardwareBufferManager::
createIndexBuffer(HardwareIndexBuffer::IndexType itype,size_t numIndexes,HardwareBuffer::Usage usage,bool useShadowBuffer)95     createIndexBuffer(HardwareIndexBuffer::IndexType itype, size_t numIndexes,
96         HardwareBuffer::Usage usage, bool useShadowBuffer)
97     {
98         assert (numIndexes > 0);
99 #if OGRE_D3D_MANAGE_BUFFERS
100         // Override shadow buffer setting; managed buffers are automatically
101         // backed by system memory
102         // Don't override shadow buffer if discardable, since then we use
103         // unmanaged buffers for speed (avoids write-through overhead)
104         // Don't override if we use directX9EX, since then we don't have managed
105         // pool. And creating non-write only default pool causes a performance warning.
106         if (useShadowBuffer && !(usage & HardwareBuffer::HBU_DISCARDABLE) &&
107             !D3D9RenderSystem::isDirectX9Ex())
108         {
109             useShadowBuffer = false;
110             // Also drop any WRITE_ONLY so we can read direct
111             if (usage == HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY)
112             {
113                 usage = HardwareBuffer::HBU_DYNAMIC;
114             }
115             else if (usage == HardwareBuffer::HBU_STATIC_WRITE_ONLY)
116             {
117                 usage = HardwareBuffer::HBU_STATIC;
118             }
119         }
120         //If we have write only buffers in DirectX9Ex we will turn on the discardable flag.
121         //Otherwise Ogre will operates in far less framerate
122         if (D3D9RenderSystem::isDirectX9Ex() && (usage & HardwareBuffer::HBU_WRITE_ONLY))
123         {
124             usage = (HardwareBuffer::Usage)
125                 ((unsigned int)usage | (unsigned int)HardwareBuffer::HBU_DISCARDABLE);
126         }
127 #endif
128         D3D9HardwareIndexBuffer* idx = OGRE_NEW D3D9HardwareIndexBuffer(
129             this, itype, numIndexes, usage, false, useShadowBuffer);
130         {
131                     OGRE_LOCK_MUTEX(mIndexBuffersMutex);
132             mIndexBuffers.insert(idx);
133         }
134         return HardwareIndexBufferSharedPtr(idx);
135 
136     }
137     //-----------------------------------------------------------------------
138     RenderToVertexBufferSharedPtr
createRenderToVertexBuffer()139         D3D9HardwareBufferManager::createRenderToVertexBuffer()
140     {
141         OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
142             "Direct3D9 does not support render to vertex buffer objects",
143             "D3D9HardwareBufferManager::createRenderToVertexBuffer");
144     }
145     //---------------------------------------------------------------------
146     HardwareUniformBufferSharedPtr
createUniformBuffer(size_t sizeBytes,HardwareBuffer::Usage usage,bool useShadowBuffer,const String & name)147         D3D9HardwareBufferManager::createUniformBuffer(size_t sizeBytes, HardwareBuffer::Usage usage, bool useShadowBuffer, const String& name)
148     {
149         OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
150                 "Uniform buffer not supported in Direct3D 9 RenderSystem.",
151                 "D3D9HardwareBufferManager::createUniformBuffer");
152     }
153     //-----------------------------------------------------------------------
154     HardwareCounterBufferSharedPtr
createCounterBuffer(size_t sizeBytes,HardwareBuffer::Usage usage,bool useShadowBuffer,const String & name)155     D3D9HardwareBufferManager::createCounterBuffer(size_t sizeBytes,
156                                                           HardwareBuffer::Usage usage, bool useShadowBuffer, const String& name)
157     {
158         OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
159                     "D3D9 does not support atomic counter buffers",
160                     "D3D9HardwareBufferManager::createCounterBuffer");
161     }
162     //-----------------------------------------------------------------------
createVertexDeclarationImpl(void)163     VertexDeclaration* D3D9HardwareBufferManager::createVertexDeclarationImpl(void)
164     {
165         return OGRE_NEW D3D9VertexDeclaration();
166     }
167     //-----------------------------------------------------------------------
destroyVertexDeclarationImpl(VertexDeclaration * decl)168     void D3D9HardwareBufferManager::destroyVertexDeclarationImpl(VertexDeclaration* decl)
169     {
170         OGRE_DELETE decl;
171     }
172 }
173