Skip to content

Commit

Permalink
igl | Move optimizedMemcpy() to shared code
Browse files Browse the repository at this point in the history
Summary: Moveed `igl::opengl::optimizedMemcpy()` to shared code.

Reviewed By: mmaurer

Differential Revision: D52551062

fbshipit-source-id: c884af58fdc392d80efff601a2d035a436d987b9
  • Loading branch information
corporateshark authored and facebook-github-bot committed Jan 6, 2024
1 parent b442cc4 commit ece419c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 77 deletions.
34 changes: 34 additions & 0 deletions src/igl/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <igl/Common.h>

namespace igl {
Expand All @@ -29,4 +32,35 @@ std::string BackendTypeToString(BackendType backendType) {
IGL_UNREACHABLE_RETURN(std::string())
}

void optimizedMemcpy(void* dst, const void* src, size_t size) {
size_t optimizationCase = size;

// There are cases where this function is used on an array of bytes, For
// example, an IGL boolean array. In this case, dst or src only need to be
// byte aligned. The logic here checks to see if dst and src are
// multiples of 4. If not, then we will use the memcpy default case below
if ((ptrdiff_t)dst & 0x3 || (ptrdiff_t)src & 0x3) {
optimizationCase = 1;
}

switch (optimizationCase) {
case 4:
*((uint32_t*)dst) = *((const uint32_t*)src);
break;
case 8:
*((uint64_t*)dst) = *((const uint64_t*)src);
break;
case 12:
*((uint64_t*)dst) = *((const uint64_t*)src);
*((uint32_t*)(dst) + 2) = *((const uint32_t*)(src) + 2);
break;
case 16:
*((uint64_t*)dst) = *((const uint64_t*)src);
*((uint64_t*)(dst) + 1) = *((const uint64_t*)(src) + 1);
break;
default:
memcpy(dst, src, size);
}
}

} // namespace igl
9 changes: 9 additions & 0 deletions src/igl/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ ScopeGuard<T> operator+(ScopeGuardOnExit, T&& fn) {

#define IGL_SCOPE_EXIT \
auto FB_ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE) = ScopeGuardOnExit() + [&]() noexcept

///--------------------------------------
/// MARK: - optimizedMemcpy

// Optimized version of memcpy that allows to copy smallest unforms in most efficient way
// Other sizes utilize a libc memcpy implementation
// It's not a universal function and expects to have a proper alignment for data!
void optimizedMemcpy(void* IGL_NULLABLE dst, const void* IGL_NULLABLE src, size_t size);

} // namespace igl

#endif // IGL_COMMON_H
1 change: 0 additions & 1 deletion src/igl/opengl/ComputeCommandAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <igl/opengl/Errors.h>
#include <igl/opengl/Framebuffer.h>
#include <igl/opengl/IContext.h>
#include <igl/opengl/Memcpy.h>
#include <igl/opengl/SamplerState.h>
#include <igl/opengl/Shader.h>
#include <igl/opengl/Texture.h>
Expand Down
48 changes: 0 additions & 48 deletions src/igl/opengl/Memcpy.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/igl/opengl/Memcpy.h

This file was deleted.

1 change: 0 additions & 1 deletion src/igl/opengl/UniformAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

#include <igl/opengl/Buffer.h>
#include <igl/opengl/Memcpy.h>
#include <igl/opengl/UniformAdapter.h>
#include <igl/opengl/UniformBuffer.h>

Expand Down
1 change: 0 additions & 1 deletion src/igl/opengl/UniformBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <igl/opengl/CommandBuffer.h>
#include <igl/opengl/Device.h>
#include <igl/opengl/Errors.h>
#include <igl/opengl/Memcpy.h>
#include <igl/opengl/RenderPipelineState.h>
#include <igl/opengl/Shader.h>
#include <memory>
Expand Down
4 changes: 1 addition & 3 deletions src/igl/tests/ogl/Memcpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

#include <igl/opengl/Memcpy.h>

#include <gtest/gtest.h>
#include <igl/IGL.h>
#include <string>
Expand Down Expand Up @@ -55,7 +53,7 @@ TEST_F(MemcpyOGLTest, optimizedMemcpyAlignmentPermutation) {

// Always clear the dst buffer before each optimizedMemcpy() call.
memcpy(dst, clr, sizeof(dst));
opengl::optimizedMemcpy(dst + di, src + i, len);
igl::optimizedMemcpy(dst + di, src + i, len);

// Verify optimizedMemcpy() has done the job correctly.
// We divide the dst buffer into up to 3 sections,
Expand Down

0 comments on commit ece419c

Please sign in to comment.