@@ -68,11 +68,54 @@ bool Buffer::isValidSubBufferOffset(size_t offset) {
6868 return false ;
6969}
7070
71+ void Buffer::validateInputAndCreateBuffer (cl_context &context,
72+ MemoryProperties properties,
73+ size_t size,
74+ void *hostPtr,
75+ cl_int &retVal,
76+ cl_mem &buffer) {
77+ if (size == 0 ) {
78+ retVal = CL_INVALID_BUFFER_SIZE;
79+ return ;
80+ }
81+
82+ if (!MemObjHelper::validateMemoryProperties (properties)) {
83+ retVal = CL_INVALID_VALUE;
84+ return ;
85+ }
86+
87+ /* Check the host ptr and data */
88+ bool expectHostPtr = (properties.flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) != 0 ;
89+ if ((hostPtr == nullptr ) == expectHostPtr) {
90+ retVal = CL_INVALID_HOST_PTR;
91+ return ;
92+ }
93+
94+ Context *pContext = nullptr ;
95+ retVal = validateObjects (WithCastToInternal (context, &pContext));
96+ if (retVal != CL_SUCCESS) {
97+ return ;
98+ }
99+
100+ // create the buffer
101+ buffer = create (pContext, properties, size, hostPtr, retVal);
102+ }
103+
71104Buffer *Buffer::create (Context *context,
72105 cl_mem_flags flags,
73106 size_t size,
74107 void *hostPtr,
75108 cl_int &errcodeRet) {
109+ MemoryProperties properties;
110+ properties.flags = flags;
111+ return create (context, properties, size, hostPtr, errcodeRet);
112+ }
113+
114+ Buffer *Buffer::create (Context *context,
115+ MemoryProperties properties,
116+ size_t size,
117+ void *hostPtr,
118+ cl_int &errcodeRet) {
76119 Buffer *pBuffer = nullptr ;
77120 errcodeRet = CL_SUCCESS;
78121
@@ -84,14 +127,14 @@ Buffer *Buffer::create(Context *context,
84127 bool allocateMemory = true ;
85128 bool copyMemoryFromHostPtr = false ;
86129 GraphicsAllocation::AllocationType allocationType = getGraphicsAllocationType (
87- flags,
130+ properties. flags ,
88131 context->isSharedContext ,
89132 context->getDevice (0 )->getHardwareInfo ().capabilityTable .ftrRenderCompressedBuffers );
90133
91134 MemoryManager *memoryManager = context->getMemoryManager ();
92135 UNRECOVERABLE_IF (!memoryManager);
93136
94- checkMemory (flags, size, hostPtr, errcodeRet, alignementSatisfied, copyMemoryFromHostPtr, memoryManager);
137+ checkMemory (properties. flags , size, hostPtr, errcodeRet, alignementSatisfied, copyMemoryFromHostPtr, memoryManager);
95138
96139 if (errcodeRet != CL_SUCCESS) {
97140 return nullptr ;
@@ -100,16 +143,16 @@ Buffer *Buffer::create(Context *context,
100143 if (allocationType == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED) {
101144 zeroCopyAllowed = false ;
102145 allocateMemory = true ;
103- if (flags & CL_MEM_USE_HOST_PTR) {
146+ if (properties. flags & CL_MEM_USE_HOST_PTR) {
104147 copyMemoryFromHostPtr = true ;
105148 }
106149 }
107150
108151 if (allocationType == GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY) {
109- if (flags & CL_MEM_ALLOC_HOST_PTR) {
152+ if (properties. flags & CL_MEM_ALLOC_HOST_PTR) {
110153 zeroCopyAllowed = true ;
111154 allocateMemory = true ;
112- } else if (flags & CL_MEM_USE_HOST_PTR) {
155+ } else if (properties. flags & CL_MEM_USE_HOST_PTR) {
113156 allocateMemory = false ;
114157 if (!alignementSatisfied || DebugManager.flags .DisableZeroCopyForUseHostPtr .get ()) {
115158 zeroCopyAllowed = false ;
@@ -124,7 +167,7 @@ Buffer *Buffer::create(Context *context,
124167 allocateMemory = false ;
125168 }
126169
127- if (flags & CL_MEM_USE_HOST_PTR) {
170+ if (properties. flags & CL_MEM_USE_HOST_PTR) {
128171 memory = context->getSVMAllocsManager ()->getSVMAlloc (hostPtr);
129172 if (memory) {
130173 allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
@@ -152,8 +195,8 @@ Buffer *Buffer::create(Context *context,
152195 }
153196
154197 if (!memory) {
155- AllocationFlags allocFlags = MemObjHelper::getAllocationFlags (flags, allocateMemory);
156- DevicesBitfield devices = MemObjHelper::getDevicesBitfield (flags );
198+ AllocationFlags allocFlags = MemObjHelper::getAllocationFlags (properties. flags , allocateMemory);
199+ DevicesBitfield devices = MemObjHelper::getDevicesBitfield (properties. flags_intel );
157200 memory = memoryManager->allocateGraphicsMemoryInPreferredPool (allocFlags, devices, hostPtr, static_cast <size_t >(size), allocationType);
158201 }
159202
@@ -163,12 +206,12 @@ Buffer *Buffer::create(Context *context,
163206
164207 // if memory pointer should not be allcoated and graphics allocation is nullptr
165208 // and cl_mem flags allow, create non-zerocopy buffer
166- if (!allocateMemory && !memory && Buffer::isReadOnlyMemoryPermittedByFlags (flags)) {
209+ if (!allocateMemory && !memory && Buffer::isReadOnlyMemoryPermittedByFlags (properties. flags )) {
167210 allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
168211 zeroCopyAllowed = false ;
169212 copyMemoryFromHostPtr = true ;
170- AllocationFlags allocFlags = MemObjHelper::getAllocationFlags (flags, true );
171- DevicesBitfield devices = MemObjHelper::getDevicesBitfield (flags );
213+ AllocationFlags allocFlags = MemObjHelper::getAllocationFlags (properties. flags , true );
214+ DevicesBitfield devices = MemObjHelper::getDevicesBitfield (properties. flags_intel );
172215 memory = memoryManager->allocateGraphicsMemoryInPreferredPool (allocFlags, devices, nullptr , static_cast <size_t >(size), allocationType);
173216 }
174217
@@ -184,12 +227,12 @@ Buffer *Buffer::create(Context *context,
184227 }
185228
186229 memory->setAllocationType (allocationType);
187- memory->setMemObjectsAllocationWithWritableFlags (!(flags & (CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS)));
230+ memory->setMemObjectsAllocationWithWritableFlags (!(properties. flags & (CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS)));
188231
189232 DBG_LOG (LogMemoryObject, __FUNCTION__, " hostPtr:" , hostPtr, " size:" , size, " memoryStorage:" , memory->getUnderlyingBuffer (), " GPU address:" , std::hex, memory->getGpuAddress ());
190233
191234 pBuffer = createBufferHw (context,
192- flags,
235+ properties. flags ,
193236 size,
194237 memory->getUnderlyingBuffer (),
195238 const_cast <void *>(hostPtr),
0 commit comments