From def4aeb3d8141db3d7189739bd98f5b8de4ec5fd Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 25 Jan 2020 23:05:33 -0800 Subject: Fixed resource mapping c interface; added API test. --- .../GraphicsEngine/interface/ResourceMapping.h | 37 +++++++---- .../src/c_interface/ResourceMapping_C_Test.c | 74 ++++++++++++++++++++++ 2 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 Tests/DiligentCoreAPITest/src/c_interface/ResourceMapping_C_Test.c diff --git a/Graphics/GraphicsEngine/interface/ResourceMapping.h b/Graphics/GraphicsEngine/interface/ResourceMapping.h index 723bc275..4fb2db14 100644 --- a/Graphics/GraphicsEngine/interface/ResourceMapping.h +++ b/Graphics/GraphicsEngine/interface/ResourceMapping.h @@ -151,26 +151,39 @@ public: struct IResourceMapping; +// clang-format off + +struct IResourceMappingMethods +{ + void (*AddResource) (struct IResourceMapping*, const Char* Name, class IDeviceObject* pObject, bool bIsUnique); + void (*AddResourceArray) (struct IResourceMapping*, const Char* Name, Uint32 StartIndex, class IDeviceObject* const* ppObjects, Uint32 NumElements, bool bIsUnique); + void (*RemoveResourceByName)(struct IResourceMapping*, const Char* Name, Uint32 ArrayIndex); + void (*GetResource) (struct IResourceMapping*, const Char* Name, class IDeviceObject** ppResource, Uint32 ArrayIndex); + size_t (*GetSize) (struct IResourceMapping*); +}; + +// clang-format on + struct IResourceMappingVtbl { - void (*AddResource)(const Char* Name, class IDeviceObject* pObject, bool bIsUnique); - void (*AddResourceArray)(const Char* Name, Uint32 StartIndex, class IDeviceObject* const* ppObjects, Uint32 NumElements, bool bIsUnique); - void (*RemoveResourceByName)(const Char* Name, Uint32 ArrayIndex); - void (*GetResource)(const Char* Name, class IDeviceObject** ppResource, Uint32 ArrayIndex); - size_t (*GetSize)(); + struct IObjectMethods Object; + struct IResourceMappingMethods ResourceMapping; }; struct IResourceMapping { - struct IObjectVtbl* pObjectVtbl; - struct IResourceMapping* pResourceMappingVtbl; + struct IResourceMappingVtbl* pVtbl; }; -# define IResourceMapping_AddResource(This, ...) (This)->pResourceMappingVtbl->AddResource(This, __VA_ARGS__) -# define IResourceMapping_AddResourceArray(This, ...) (This)->pResourceMappingVtbl->AddResourceArray(This, __VA_ARGS__) -# define IResourceMapping_RemoveResourceByName(This, ...) (This)->pResourceMappingVtbl->RemoveResourceByName(This, __VA_ARGS__) -# define IResourceMapping_GetResource(This, ...) (This)->pResourceMappingVtbl->GetResource(This, __VA_ARGS__) -# define IResourceMapping_GetSize(This) (This)->pResourceMappingVtbl->GetSize(This) +// clang-format off + +# define IResourceMapping_AddResource(This, ...) (This)->pVtbl->ResourceMapping.AddResource ((struct IResourceMapping*)(This), __VA_ARGS__) +# define IResourceMapping_AddResourceArray(This, ...) (This)->pVtbl->ResourceMapping.AddResourceArray ((struct IResourceMapping*)(This), __VA_ARGS__) +# define IResourceMapping_RemoveResourceByName(This, ...) (This)->pVtbl->ResourceMapping.RemoveResourceByName((struct IResourceMapping*)(This), __VA_ARGS__) +# define IResourceMapping_GetResource(This, ...) (This)->pVtbl->ResourceMapping.GetResource ((struct IResourceMapping*)(This), __VA_ARGS__) +# define IResourceMapping_GetSize(This) (This)->pVtbl->ResourceMapping.GetSize ((struct IResourceMapping*)(This)) + +// clang-format on #endif diff --git a/Tests/DiligentCoreAPITest/src/c_interface/ResourceMapping_C_Test.c b/Tests/DiligentCoreAPITest/src/c_interface/ResourceMapping_C_Test.c new file mode 100644 index 00000000..1a0ca449 --- /dev/null +++ b/Tests/DiligentCoreAPITest/src/c_interface/ResourceMapping_C_Test.c @@ -0,0 +1,74 @@ +/* + * Copyright 2019-2020 Diligent Graphics LLC + * Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include "ResourceMapping.h" + +int TestObjectCInterface(struct IObject* pObject); + +int TestResourceMappingCInterface(struct IResourceMapping* pResourceMapping) +{ + struct IObject* pUnknown = NULL; + ReferenceCounterValueType RefCnt1 = 0, RefCnt2 = 0; + + struct ResourceMappingDesc SCDesc; + + memset(&SCDesc, 0, sizeof(SCDesc)); + + int num_errors = TestObjectCInterface((struct IObject*)pResourceMapping); + + IObject_QueryInterface(pResourceMapping, &IID_Unknown, &pUnknown); + if (pUnknown != NULL) + IObject_Release(pUnknown); + else + ++num_errors; + + RefCnt1 = IObject_AddRef(pResourceMapping); + if (RefCnt1 <= 1) + ++num_errors; + RefCnt2 = IObject_Release(pResourceMapping); + if (RefCnt2 <= 0) + ++num_errors; + if (RefCnt2 != RefCnt1 - 1) + ++num_errors; + + return num_errors; +} + +void TestResourceMappingC_API(struct IResourceMapping* pResourceMapping) +{ + struct IDeviceObject* pObject = NULL; + + Uint32 ArraySize = 4; + Uint32 ArrayIndex = 6; + size_t Size = 0; + + IResourceMapping_AddResource(pResourceMapping, "Resource Name", pObject, true); + IResourceMapping_AddResourceArray(pResourceMapping, "Resource Array Name", 0, &pObject, ArraySize, true); + IResourceMapping_RemoveResourceByName(pResourceMapping, "Resource Name", ArrayIndex); + IResourceMapping_GetResource(pResourceMapping, "Resource Name", &pObject, ArrayIndex); + Size = IResourceMapping_GetSize(pResourceMapping); +} -- cgit v1.2.3