1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "BsGLHardwareBufferManager.h"
4#include "BsGLVertexBuffer.h"
5#include "BsGLIndexBuffer.h"
6#include "BsGLGpuBuffer.h"
7#include "RenderAPI/BsHardwareBuffer.h"
8#include "BsGLGpuParamBlockBuffer.h"
9#include "RenderAPI/BsRenderAPI.h"
10#include "RenderAPI/BsRenderAPICapabilities.h"
11
12namespace bs { namespace ct
13{
14 SPtr<VertexBuffer> GLHardwareBufferManager::createVertexBufferInternal(const VERTEX_BUFFER_DESC& desc,
15 GpuDeviceFlags deviceMask)
16 {
17 SPtr<GLVertexBuffer> ret = bs_shared_ptr_new<GLVertexBuffer>(desc, deviceMask);
18 ret->_setThisPtr(ret);
19
20 return ret;
21 }
22
23 SPtr<IndexBuffer> GLHardwareBufferManager::createIndexBufferInternal(const INDEX_BUFFER_DESC& desc,
24 GpuDeviceFlags deviceMask)
25 {
26 SPtr<GLIndexBuffer> ret = bs_shared_ptr_new<GLIndexBuffer>(desc, deviceMask);
27 ret->_setThisPtr(ret);
28
29 return ret;
30 }
31
32 SPtr<GpuParamBlockBuffer> GLHardwareBufferManager::createGpuParamBlockBufferInternal(UINT32 size,
33 GpuBufferUsage usage, GpuDeviceFlags deviceMask)
34 {
35 GLGpuParamBlockBuffer* paramBlockBuffer =
36 new (bs_alloc<GLGpuParamBlockBuffer>()) GLGpuParamBlockBuffer(size, usage, deviceMask);
37
38 SPtr<GpuParamBlockBuffer> paramBlockBufferPtr = bs_shared_ptr<GLGpuParamBlockBuffer>(paramBlockBuffer);
39 paramBlockBufferPtr->_setThisPtr(paramBlockBufferPtr);
40
41 return paramBlockBufferPtr;
42 }
43
44 SPtr<GpuBuffer> GLHardwareBufferManager::createGpuBufferInternal(const GPU_BUFFER_DESC& desc,
45 GpuDeviceFlags deviceMask)
46 {
47 GLGpuBuffer* buffer = new (bs_alloc<GLGpuBuffer>()) GLGpuBuffer(desc, deviceMask);
48
49 SPtr<GpuBuffer> bufferPtr = bs_shared_ptr<GLGpuBuffer>(buffer);
50 bufferPtr->_setThisPtr(bufferPtr);
51
52 return bufferPtr;
53 }
54
55 SPtr<GpuBuffer> GLHardwareBufferManager::createGpuBufferInternal(const GPU_BUFFER_DESC& desc,
56 SPtr<HardwareBuffer> underlyingBuffer)
57 {
58 GLGpuBuffer* buffer = new (bs_alloc<GLGpuBuffer>()) GLGpuBuffer(desc, std::move(underlyingBuffer));
59
60 SPtr<GpuBuffer> bufferPtr = bs_shared_ptr<GLGpuBuffer>(buffer);
61 bufferPtr->_setThisPtr(bufferPtr);
62
63 return bufferPtr;
64 }
65
66 GLenum GLHardwareBufferManager::getGLUsage(GpuBufferUsage usage)
67 {
68 if((usage & GBU_LOADSTORE) == GBU_LOADSTORE)
69 {
70 if ((usage & GBU_STATIC) != 0)
71 return GL_STATIC_READ;
72
73 return GL_DYNAMIC_READ;
74 }
75 else
76 {
77 if ((usage & GBU_STATIC) != 0)
78 return GL_STATIC_DRAW;
79
80 return GL_DYNAMIC_DRAW;
81 }
82 }
83
84 GLenum GLHardwareBufferManager::getGLType(VertexElementType type)
85 {
86 switch(type)
87 {
88 case VET_FLOAT1:
89 case VET_FLOAT2:
90 case VET_FLOAT3:
91 case VET_FLOAT4:
92 return GL_FLOAT;
93 case VET_SHORT1:
94 case VET_SHORT2:
95 case VET_SHORT4:
96 return GL_SHORT;
97 case VET_USHORT1:
98 case VET_USHORT2:
99 case VET_USHORT4:
100 return GL_UNSIGNED_SHORT;
101 case VET_INT1:
102 case VET_INT2:
103 case VET_INT3:
104 case VET_INT4:
105 return GL_INT;
106 case VET_UINT1:
107 case VET_UINT2:
108 case VET_UINT3:
109 case VET_UINT4:
110 return GL_UNSIGNED_INT;
111 case VET_COLOR:
112 case VET_COLOR_ABGR:
113 case VET_COLOR_ARGB:
114 case VET_UBYTE4:
115 case VET_UBYTE4_NORM:
116 return GL_UNSIGNED_BYTE;
117 default:
118 return 0;
119 };
120 }
121}}