From 93bbffb82d54868c52930c48bdd46c9b12342079 Mon Sep 17 00:00:00 2001 From: Rinnegatamante Date: Wed, 7 Oct 2020 11:32:38 +0200 Subject: [PATCH] Replaced memcpy usage with NEON optimized ones. --- src/base.cpp | 10 ++++++---- src/geometry.cpp | 6 ++++-- src/gl/gl3device.cpp | 16 ++++++++-------- src/gl/gl3pipe.cpp | 10 +++------- src/gl/gl3skin.cpp | 2 +- src/gl/wdgl.cpp | 6 +++--- src/image.cpp | 6 ++++-- src/matfx.cpp | 2 +- src/pipeline.cpp | 12 +++++++----- src/png.cpp | 12 +++++++----- src/skin.cpp | 8 ++++---- src/tristrip.cpp | 4 +++- src/userdata.cpp | 8 +++++--- 13 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/base.cpp b/src/base.cpp index de3706b..dba57a5 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -19,6 +19,8 @@ #include "rwobjects.h" #include "rwengine.h" +#include + namespace rw { #define PLUGIN_ID 0 @@ -759,7 +761,7 @@ Stream::write32(const void *data, uint32 length) int32 n, len; for(len = length >>= 2; len > 0; len -= 256){ n = len < 256 ? len : 256; - memcpy(buf, src, n*4); + memcpy_neon(buf, src, n*4); memLittle16(buf, n*4); write8(buf, n*4); src += n*4; @@ -779,7 +781,7 @@ Stream::write16(const void *data, uint32 length) int32 n, len; for(len = length >>= 1; len > 0; len -= 256){ n = len < 256 ? len : 256; - memcpy(buf, src, n*2); + memcpy_neon(buf, src, n*2); memLittle16(buf, n*2); write8(buf, n*2); src += n*2; @@ -924,7 +926,7 @@ StreamMemory::write8(const void *data, uint32 len) l = this->capacity-this->position; this->length = this->position+l; } - memcpy(&this->data[this->position], data, l); + memcpy_neon(&this->data[this->position], data, l); this->position += l; if(len != l) this->position = S_EOF; @@ -939,7 +941,7 @@ StreamMemory::read8(void *data, uint32 len) uint32 l = len; if(this->position+l > this->length) l = this->length-this->position; - memcpy(data, &this->data[this->position], l); + memcpy_neon(data, &this->data[this->position], l); this->position += l; if(len != l) this->position = S_EOF; diff --git a/src/geometry.cpp b/src/geometry.cpp index e62bc5d..8271c85 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -11,6 +11,8 @@ #include "rwobjects.h" #include "rwengine.h" +#include + #define PLUGIN_ID ID_GEOMETRY namespace rw { @@ -616,7 +618,7 @@ Geometry::correctTristripWinding(void) rwFree(header); // Now allocate indices and copy them this->allocateMeshes(newhead->numMeshes, newhead->totalIndices, 0); - memcpy(this->meshHeader->getMeshes()->indices, indices, this->meshHeader->totalIndices*2); + memcpy_neon(this->meshHeader->getMeshes()->indices, indices, this->meshHeader->totalIndices*2); rwFree(indices); } @@ -671,7 +673,7 @@ Geometry::removeUnusedMaterials(void) for(uint32 i = 0; i < mh->numMeshes; i++){ if(m[i].numIndices <= 0) continue; - memcpy(newm->indices, m[i].indices, + memcpy_neon(newm->indices, m[i].indices, m[i].numIndices*sizeof(*m[i].indices)); newm++; } diff --git a/src/gl/gl3device.cpp b/src/gl/gl3device.cpp index c867872..5633d8e 100644 --- a/src/gl/gl3device.cpp +++ b/src/gl/gl3device.cpp @@ -800,7 +800,7 @@ setLights(WorldLights *lightData) l = lightData->directionals[i]; uniformObject.lightParams[n].type = 1.0f; uniformObject.lightColor[n] = l->color; - memcpy(&uniformObject.lightDirection[n], &l->getFrame()->getLTM()->at, sizeof(V3d)); + memcpy_neon(&uniformObject.lightDirection[n], &l->getFrame()->getLTM()->at, sizeof(V3d)); bits |= VSLIGHT_POINT; n++; if(n >= MAX_LIGHTS) @@ -815,7 +815,7 @@ setLights(WorldLights *lightData) uniformObject.lightParams[n].type = 2.0f; uniformObject.lightParams[n].radius = l->radius; uniformObject.lightColor[n] = l->color; - memcpy(&uniformObject.lightPosition[n], &l->getFrame()->getLTM()->pos, sizeof(V3d)); + memcpy_neon(&uniformObject.lightPosition[n], &l->getFrame()->getLTM()->pos, sizeof(V3d)); bits |= VSLIGHT_POINT; n++; if(n >= MAX_LIGHTS) @@ -827,8 +827,8 @@ setLights(WorldLights *lightData) uniformObject.lightParams[n].minusCosAngle = l->minusCosAngle; uniformObject.lightParams[n].radius = l->radius; uniformObject.lightColor[n] = l->color; - memcpy(&uniformObject.lightPosition[n], &l->getFrame()->getLTM()->pos, sizeof(V3d)); - memcpy(&uniformObject.lightDirection[n], &l->getFrame()->getLTM()->at, sizeof(V3d)); + memcpy_neon(&uniformObject.lightPosition[n], &l->getFrame()->getLTM()->pos, sizeof(V3d)); + memcpy_neon(&uniformObject.lightDirection[n], &l->getFrame()->getLTM()->at, sizeof(V3d)); // lower bound of falloff if(l->getType() == Light::SOFTSPOT) uniformObject.lightParams[n].hardSpot = 0.0f; @@ -851,14 +851,14 @@ out: void setProjectionMatrix(float32 *mat) { - memcpy(&uniformScene.proj, mat, 64); + memcpy_neon(&uniformScene.proj, mat, 64); sceneDirty = 1; } void setViewMatrix(float32 *mat) { - memcpy(&uniformScene.view, mat, 64); + memcpy_neon(&uniformScene.view, mat, 64); sceneDirty = 1; } @@ -1035,7 +1035,7 @@ beginUpdate(Camera *cam) view[13] = inv.pos.y; view[14] = inv.pos.z; view[15] = 1.0f; - memcpy(&cam->devView, &view, sizeof(RawMatrix)); + memcpy_neon(&cam->devView, &view, sizeof(RawMatrix)); setViewMatrix(view); // Projection Matrix @@ -1070,7 +1070,7 @@ beginUpdate(Camera *cam) proj[14] = 2.0f*invz; proj[15] = 1.0f; } - memcpy(&cam->devProj, &proj, sizeof(RawMatrix)); + memcpy_neon(&cam->devProj, &proj, sizeof(RawMatrix)); setProjectionMatrix(proj); if(rwStateCache.fogStart != cam->fogPlane){ diff --git a/src/gl/gl3pipe.cpp b/src/gl/gl3pipe.cpp index b99756c..4328254 100644 --- a/src/gl/gl3pipe.cpp +++ b/src/gl/gl3pipe.cpp @@ -30,11 +30,7 @@ freeInstanceData(Geometry *geometry) return; InstanceDataHeader *header = (InstanceDataHeader*)geometry->instData; geometry->instData = nil; - glDeleteBuffers(1, &header->ibo); - glDeleteBuffers(1, &header->vbo); -#ifdef RW_GL_USE_VAOS - glDeleteBuffers(1, &header->vao); -#endif + rwFree(header->indexBuffer); rwFree(header->vertexBuffer); rwFree(header->attribDesc); @@ -76,7 +72,7 @@ instanceMesh(rw::ObjPipeline *rwpipe, Geometry *geo) inst->vertexAlpha = 0; inst->program = 0; inst->offset = offset; - memcpy((uint8*)header->indexBuffer + inst->offset, + memcpy_neon((uint8*)header->indexBuffer + inst->offset, mesh->indices, inst->numIndex*2); offset += inst->numIndex*2; mesh++; @@ -228,7 +224,7 @@ defaultInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance) for(a = tmpAttribs; a != &tmpAttribs[header->numAttribs]; a++) a->stride = stride; header->attribDesc = rwNewT(AttribDesc, header->numAttribs, MEMDUR_EVENT | ID_GEOMETRY); - memcpy(header->attribDesc, tmpAttribs, + memcpy_neon(header->attribDesc, tmpAttribs, header->numAttribs*sizeof(AttribDesc)); // diff --git a/src/gl/gl3skin.cpp b/src/gl/gl3skin.cpp index 4ac14f2..a9b278b 100644 --- a/src/gl/gl3skin.cpp +++ b/src/gl/gl3skin.cpp @@ -113,7 +113,7 @@ skinInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance) for(a = tmpAttribs; a != &tmpAttribs[header->numAttribs]; a++) a->stride = stride; header->attribDesc = rwNewT(AttribDesc, header->numAttribs, MEMDUR_EVENT | ID_GEOMETRY); - memcpy(header->attribDesc, tmpAttribs, + memcpy_neon(header->attribDesc, tmpAttribs, header->numAttribs*sizeof(AttribDesc)); // diff --git a/src/gl/wdgl.cpp b/src/gl/wdgl.cpp index 05e74e8..3c89071 100644 --- a/src/gl/wdgl.cpp +++ b/src/gl/wdgl.cpp @@ -116,7 +116,7 @@ packattrib(uint8 *dst, float32 *src, AttribDesc *a, float32 scale=1.0f) switch(a->type){ case 0: // float - memcpy(dst, src, a->size*4); + memcpy_neon(dst, src, a->size*4); break; // TODO: maybe have loop inside if? @@ -170,7 +170,7 @@ unpackattrib(float *dst, uint8 *src, AttribDesc *a, float32 scale=1.0f) switch(a->type){ case 0: // float - memcpy(dst, src, a->size*4); + memcpy_neon(dst, src, a->size*4); break; // TODO: maybe have loop inside if? @@ -637,7 +637,7 @@ skinUninstanceCB(Geometry *geo) uint8 *data = skin->data; float *invMats = skin->inverseMatrices; skin->init(skin->numBones, skin->numBones, geo->numVertices); - memcpy(skin->inverseMatrices, invMats, skin->numBones*64); + memcpy_neon(skin->inverseMatrices, invMats, skin->numBones*64); rwFree(data); uint8 *p; diff --git a/src/image.cpp b/src/image.cpp index 632ca97..4776909 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -17,6 +17,8 @@ #define PLUGIN_ID ID_IMAGE +#include + namespace rw { int32 Image::numAllocated; @@ -476,7 +478,7 @@ Image::palettize(int32 depth) this->palette = nil; this->setPixels(newpixels); this->allocate(); - memcpy(this->palette, colors, 4*(1<palette, colors, 4*(1<fx[i].type){ case MatFX::BUMPMAP: diff --git a/src/pipeline.cpp b/src/pipeline.cpp index 6284f68..88667b9 100644 --- a/src/pipeline.cpp +++ b/src/pipeline.cpp @@ -9,6 +9,8 @@ #include "rwobjects.h" #include "rwengine.h" +#include + #define COLOR_ARGB(a,r,g,b) \ ((uint32)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff))) @@ -70,7 +72,7 @@ instV4d(int type, uint8 *dst, V4d *src, uint32 numVertices, uint32 stride) { if(type == VERT_FLOAT4) for(uint32 i = 0; i < numVertices; i++){ - memcpy(dst, src, 16); + memcpy_neon(dst, src, 16); dst += stride; src++; } @@ -83,7 +85,7 @@ instV3d(int type, uint8 *dst, V3d *src, uint32 numVertices, uint32 stride) { if(type == VERT_FLOAT3) for(uint32 i = 0; i < numVertices; i++){ - memcpy(dst, src, 12); + memcpy_neon(dst, src, 12); dst += stride; src++; } @@ -105,7 +107,7 @@ uninstV3d(int type, V3d *dst, uint8 *src, uint32 numVertices, uint32 stride) { if(type == VERT_FLOAT3) for(uint32 i = 0; i < numVertices; i++){ - memcpy(dst, src, 12); + memcpy_neon(dst, src, 12); src += stride; dst++; } @@ -135,7 +137,7 @@ instTexCoords(int type, uint8 *dst, TexCoords *src, uint32 numVertices, uint32 s { assert(type == VERT_FLOAT2); for(uint32 i = 0; i < numVertices; i++){ - memcpy(dst, src, 8); + memcpy_neon(dst, src, 8); dst += stride; src++; } @@ -146,7 +148,7 @@ uninstTexCoords(int type, TexCoords *dst, uint8 *src, uint32 numVertices, uint32 { assert(type == VERT_FLOAT2); for(uint32 i = 0; i < numVertices; i++){ - memcpy(dst, src, 8); + memcpy_neon(dst, src, 8); src += stride; dst++; } diff --git a/src/png.cpp b/src/png.cpp index 5749a32..acb471f 100644 --- a/src/png.cpp +++ b/src/png.cpp @@ -12,6 +12,8 @@ #include "lodepng/lodepng.h" +#include + #ifdef _WIN32 /* srsly? */ #define strdup _strdup @@ -46,20 +48,20 @@ readPNG(const char *filename) if(state.info_raw.bitdepth == 4 && state.info_raw.colortype == LCT_PALETTE){ image = Image::create(w, h, 4); image->allocate(); - memcpy(image->palette, state.info_raw.palette, state.info_raw.palettesize*4); + memcpy_neon(image->palette, state.info_raw.palette, state.info_raw.palettesize*4); expandPal4_BE(image->pixels, image->stride, raw, w/2, w, h); }else if(state.info_raw.bitdepth == 8){ switch(state.info_raw.colortype){ case LCT_PALETTE: image = Image::create(w, h, state.info_raw.palettesize <= 16 ? 4 : 8); image->allocate(); - memcpy(image->palette, state.info_raw.palette, state.info_raw.palettesize*4); - memcpy(image->pixels, raw, w*h); + memcpy_neon(image->palette, state.info_raw.palette, state.info_raw.palettesize*4); + memcpy_neon(image->pixels, raw, w*h); break; case LCT_RGB: image = Image::create(w, h, 24); image->allocate(); - memcpy(image->pixels, raw, w*h*3); + memcpy_neon(image->pixels, raw, w*h*3); break; default: // Second try: just load as 32 bit @@ -74,7 +76,7 @@ readPNG(const char *filename) case LCT_RGBA: image = Image::create(w, h, 32); image->allocate(); - memcpy(image->pixels, raw, w*h*4); + memcpy_neon(image->pixels, raw, w*h*4); break; } } diff --git a/src/skin.cpp b/src/skin.cpp index 3d7eac4..2e703e3 100644 --- a/src/skin.cpp +++ b/src/skin.cpp @@ -64,11 +64,11 @@ copySkin(void *dst, void *src, int32 offset, int32) assert(0 && "can't copy skin yet"); dstskin->init(srcskin->numBones, srcskin->numUsedBones, geometry->numVertices); - memcpy(dstskin->usedBones, srcskin->usedBones, srcskin->numUsedBones); - memcpy(dstskin->inverseMatrices, srcskin->inverseMatrices, + memcpy_neon(dstskin->usedBones, srcskin->usedBones, srcskin->numUsedBones); + memcpy_neon(dstskin->inverseMatrices, srcskin->inverseMatrices, srcskin->numBones*64); - memcpy(dstskin->indices, srcskin->indices, geometry->numVertices*4); - memcpy(dstskin->weights, srcskin->weights, geometry->numVertices*16); + memcpy_neon(dstskin->indices, srcskin->indices, geometry->numVertices*4); + memcpy_neon(dstskin->weights, srcskin->weights, geometry->numVertices*16); return dst; } diff --git a/src/tristrip.cpp b/src/tristrip.cpp index e71b844..4a82263 100644 --- a/src/tristrip.cpp +++ b/src/tristrip.cpp @@ -11,6 +11,8 @@ #include "rwobjects.h" #include "rwengine.h" +#include + #define PLUGIN_ID 2 namespace rw { @@ -609,7 +611,7 @@ printSmesh(&smesh); md[i].numIndices = ms[i].numIndices; md[i].indices = indices; indices += md[i].numIndices; - memcpy(md[i].indices, ms[i].indices, md[i].numIndices*sizeof(uint16)); + memcpy_neon(md[i].indices, ms[i].indices, md[i].numIndices*sizeof(uint16)); rwFree(ms[i].indices); } rwFree(header); diff --git a/src/userdata.cpp b/src/userdata.cpp index e17a3d5..3afe6da 100644 --- a/src/userdata.cpp +++ b/src/userdata.cpp @@ -11,6 +11,8 @@ #include "rwengine.h" #include "rwuserdata.h" +#include + #define PLUGIN_ID ID_USERDATA namespace rw { @@ -90,11 +92,11 @@ copyUserData(void *dst, void *src, int32 offset, int32) switch(srca->datatype){ case USERDATAINT: dsta->data = (int32*)udMalloc(sizeof(int32)*dsta->numElements); - memcpy(dsta->data, srca->data, sizeof(int32)*dsta->numElements); + memcpy_neon(dsta->data, srca->data, sizeof(int32)*dsta->numElements); break; case USERDATAFLOAT: dsta->data = (float32*)udMalloc(sizeof(float32)*dsta->numElements); - memcpy(dsta->data, srca->data, sizeof(float32)*dsta->numElements); + memcpy_neon(dsta->data, srca->data, sizeof(float32)*dsta->numElements); break; case USERDATASTRING: dststrar = (char**)udMalloc(sizeof(char*)*dsta->numElements); @@ -241,7 +243,7 @@ UserDataExtension::add(const char *name, int32 datatype, int32 numElements) a = (UserDataArray*)udMalloc((this->numArrays+1)*sizeof(UserDataArray)); if(a == nil) return -1; - memcpy(a, this->arrays, this->numArrays*sizeof(UserDataArray)); + memcpy_neon(a, this->arrays, this->numArrays*sizeof(UserDataArray)); rwFree(this->arrays); this->arrays = a; i = this->numArrays++;