Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "rsCpuCore.h" |
| 18 | #include "rsCpuScript.h" |
| 19 | #include "rsCpuScriptGroup.h" |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 20 | #include "rsCpuScriptGroup2.h" |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 21 | |
| 22 | #include <malloc.h> |
| 23 | #include "rsContext.h" |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/resource.h> |
| 27 | #include <sched.h> |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 29 | #include <stdio.h> |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 30 | #include <string.h> |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 32 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 33 | #define REDUCE_ALOGV(mtls, level, ...) do { if ((mtls)->logReduce >= (level)) ALOGV(__VA_ARGS__); } while(0) |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 34 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 35 | static pthread_key_t gThreadTLSKey = 0; |
| 36 | static uint32_t gThreadTLSKeyCount = 0; |
| 37 | static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
| 38 | |
Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace renderscript { |
| 41 | |
| 42 | bool gArchUseSIMD = false; |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 43 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 44 | RsdCpuReference::~RsdCpuReference() { |
| 45 | } |
| 46 | |
| 47 | RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major, |
Jason Sams | cadfac4 | 2013-03-06 18:09:08 -0800 | [diff] [blame] | 48 | uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn |
David Gross | b043df0 | 2015-05-29 11:38:15 -0700 | [diff] [blame] | 49 | , RSSelectRTCallback pSelectRTCallback, |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 50 | const char *pBccPluginName |
Jason Sams | cadfac4 | 2013-03-06 18:09:08 -0800 | [diff] [blame] | 51 | ) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 52 | |
| 53 | RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc); |
| 54 | if (!cpu) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 55 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 56 | } |
| 57 | if (!cpu->init(version_major, version_minor, lfn, slfn)) { |
| 58 | delete cpu; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 59 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 60 | } |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 61 | |
Stephen Hines | 1d47662 | 2013-03-29 22:08:49 -0700 | [diff] [blame] | 62 | cpu->setSelectRTCallback(pSelectRTCallback); |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 63 | if (pBccPluginName) { |
| 64 | cpu->setBccPluginName(pBccPluginName); |
| 65 | } |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 66 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 67 | return cpu; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | Context * RsdCpuReference::getTlsContext() { |
| 72 | ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); |
| 73 | return tls->mContext; |
| 74 | } |
| 75 | |
| 76 | const Script * RsdCpuReference::getTlsScript() { |
| 77 | ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); |
| 78 | return tls->mScript; |
| 79 | } |
| 80 | |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 81 | pthread_key_t RsdCpuReference::getThreadTLSKey(){ return gThreadTLSKey; } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 82 | |
| 83 | //////////////////////////////////////////////////////////// |
| 84 | /// |
| 85 | |
| 86 | RsdCpuReferenceImpl::RsdCpuReferenceImpl(Context *rsc) { |
| 87 | mRSC = rsc; |
| 88 | |
| 89 | version_major = 0; |
| 90 | version_minor = 0; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 91 | mInKernel = false; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 92 | memset(&mWorkers, 0, sizeof(mWorkers)); |
| 93 | memset(&mTlsStruct, 0, sizeof(mTlsStruct)); |
| 94 | mExit = false; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 95 | mSelectRTCallback = nullptr; |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 96 | mEmbedGlobalInfo = true; |
| 97 | mEmbedGlobalInfoSkipConstant = true; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | void * RsdCpuReferenceImpl::helperThreadProc(void *vrsc) { |
| 102 | RsdCpuReferenceImpl *dc = (RsdCpuReferenceImpl *)vrsc; |
| 103 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 104 | uint32_t idx = __sync_fetch_and_add(&dc->mWorkers.mLaunchCount, 1); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 105 | |
| 106 | //ALOGV("RS helperThread starting %p idx=%i", dc, idx); |
| 107 | |
| 108 | dc->mWorkers.mLaunchSignals[idx].init(); |
| 109 | dc->mWorkers.mNativeThreadId[idx] = gettid(); |
| 110 | |
| 111 | memset(&dc->mTlsStruct, 0, sizeof(dc->mTlsStruct)); |
| 112 | int status = pthread_setspecific(gThreadTLSKey, &dc->mTlsStruct); |
| 113 | if (status) { |
| 114 | ALOGE("pthread_setspecific %i", status); |
| 115 | } |
| 116 | |
| 117 | #if 0 |
| 118 | typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t; |
| 119 | cpu_set_t cpuset; |
| 120 | memset(&cpuset, 0, sizeof(cpuset)); |
| 121 | cpuset.bits[idx / 64] |= 1ULL << (idx % 64); |
| 122 | int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx], |
| 123 | sizeof(cpuset), &cpuset); |
| 124 | ALOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret)); |
| 125 | #endif |
| 126 | |
| 127 | while (!dc->mExit) { |
| 128 | dc->mWorkers.mLaunchSignals[idx].wait(); |
| 129 | if (dc->mWorkers.mLaunchCallback) { |
| 130 | // idx +1 is used because the calling thread is always worker 0. |
| 131 | dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx+1); |
| 132 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 133 | __sync_fetch_and_sub(&dc->mWorkers.mRunningCount, 1); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 134 | dc->mWorkers.mCompleteSignal.set(); |
| 135 | } |
| 136 | |
| 137 | //ALOGV("RS helperThread exited %p idx=%i", dc, idx); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 138 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 141 | // Launch a kernel. |
| 142 | // The callback function is called to execute the kernel. |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 143 | void RsdCpuReferenceImpl::launchThreads(WorkerCallback_t cbk, void *data) { |
| 144 | mWorkers.mLaunchData = data; |
| 145 | mWorkers.mLaunchCallback = cbk; |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 146 | |
| 147 | // fast path for very small launches |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 148 | MTLaunchStructCommon *mtls = (MTLaunchStructCommon *)data; |
| 149 | if (mtls && mtls->dimPtr->y <= 1 && mtls->end.x <= mtls->start.x + mtls->mSliceSize) { |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 150 | if (mWorkers.mLaunchCallback) { |
| 151 | mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0); |
| 152 | } |
| 153 | return; |
| 154 | } |
| 155 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 156 | mWorkers.mRunningCount = mWorkers.mCount; |
| 157 | __sync_synchronize(); |
| 158 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 159 | for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { |
| 160 | mWorkers.mLaunchSignals[ct].set(); |
| 161 | } |
| 162 | |
| 163 | // We use the calling thread as one of the workers so we can start without |
| 164 | // the delay of the thread wakeup. |
| 165 | if (mWorkers.mLaunchCallback) { |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 166 | mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 169 | while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 170 | mWorkers.mCompleteSignal.wait(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void RsdCpuReferenceImpl::lockMutex() { |
| 176 | pthread_mutex_lock(&gInitMutex); |
| 177 | } |
| 178 | |
| 179 | void RsdCpuReferenceImpl::unlockMutex() { |
| 180 | pthread_mutex_unlock(&gInitMutex); |
| 181 | } |
| 182 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 183 | // Determine if the CPU we're running on supports SIMD instructions. |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 184 | static void GetCpuInfo() { |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 185 | // Read the CPU flags from /proc/cpuinfo. |
| 186 | FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 187 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 188 | if (!cpuinfo) { |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 192 | char cpuinfostr[4096]; |
Miao Wang | 5d70cb5 | 2015-07-17 11:53:04 -0700 | [diff] [blame] | 193 | // fgets() ends with newline or EOF, need to check the whole |
| 194 | // "cpuinfo" file to make sure we can use SIMD or not. |
| 195 | while (fgets(cpuinfostr, sizeof(cpuinfostr), cpuinfo)) { |
| 196 | #if defined(ARCH_ARM_HAVE_VFP) || defined(ARCH_ARM_USE_INTRINSICS) |
| 197 | gArchUseSIMD = strstr(cpuinfostr, " neon") || strstr(cpuinfostr, " asimd"); |
| 198 | #elif defined(ARCH_X86_HAVE_SSSE3) |
| 199 | gArchUseSIMD = strstr(cpuinfostr, " ssse3"); |
| 200 | #endif |
| 201 | if (gArchUseSIMD) { |
| 202 | break; |
| 203 | } |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 204 | } |
| 205 | fclose(cpuinfo); |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 206 | } |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 207 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 208 | bool RsdCpuReferenceImpl::init(uint32_t version_major, uint32_t version_minor, |
| 209 | sym_lookup_t lfn, script_lookup_t slfn) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 210 | mSymLookupFn = lfn; |
| 211 | mScriptLookupFn = slfn; |
| 212 | |
| 213 | lockMutex(); |
| 214 | if (!gThreadTLSKeyCount) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 215 | int status = pthread_key_create(&gThreadTLSKey, nullptr); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 216 | if (status) { |
| 217 | ALOGE("Failed to init thread tls key."); |
| 218 | unlockMutex(); |
| 219 | return false; |
| 220 | } |
| 221 | } |
| 222 | gThreadTLSKeyCount++; |
| 223 | unlockMutex(); |
| 224 | |
| 225 | mTlsStruct.mContext = mRSC; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 226 | mTlsStruct.mScript = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 227 | int status = pthread_setspecific(gThreadTLSKey, &mTlsStruct); |
| 228 | if (status) { |
| 229 | ALOGE("pthread_setspecific %i", status); |
| 230 | } |
| 231 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 232 | mPageSize = sysconf(_SC_PAGE_SIZE); |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 233 | // ALOGV("page size = %ld", mPageSize); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 234 | |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 235 | GetCpuInfo(); |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 236 | |
Jason Sams | 77d57a3 | 2014-10-23 17:43:53 -0700 | [diff] [blame] | 237 | int cpu = sysconf(_SC_NPROCESSORS_CONF); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 238 | if(mRSC->props.mDebugMaxThreads) { |
| 239 | cpu = mRSC->props.mDebugMaxThreads; |
| 240 | } |
| 241 | if (cpu < 2) { |
| 242 | mWorkers.mCount = 0; |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | // Subtract one from the cpu count because we also use the command thread as a worker. |
| 247 | mWorkers.mCount = (uint32_t)(cpu - 1); |
| 248 | |
Yang Ni | 554054c | 2016-04-20 09:04:02 -0700 | [diff] [blame] | 249 | if (mRSC->props.mLogScripts) { |
| 250 | ALOGV("%p Launching thread(s), CPUs %i", mRSC, mWorkers.mCount + 1); |
| 251 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 252 | |
| 253 | mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t)); |
| 254 | mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t)); |
| 255 | mWorkers.mLaunchSignals = new Signal[mWorkers.mCount]; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 256 | mWorkers.mLaunchCallback = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 257 | |
| 258 | mWorkers.mCompleteSignal.init(); |
| 259 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 260 | mWorkers.mRunningCount = mWorkers.mCount; |
| 261 | mWorkers.mLaunchCount = 0; |
| 262 | __sync_synchronize(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 263 | |
| 264 | pthread_attr_t threadAttr; |
| 265 | status = pthread_attr_init(&threadAttr); |
| 266 | if (status) { |
| 267 | ALOGE("Failed to init thread attribute."); |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { |
| 272 | status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this); |
| 273 | if (status) { |
| 274 | mWorkers.mCount = ct; |
| 275 | ALOGE("Created fewer than expected number of RS threads."); |
| 276 | break; |
| 277 | } |
| 278 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 279 | while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 280 | usleep(100); |
| 281 | } |
| 282 | |
| 283 | pthread_attr_destroy(&threadAttr); |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | void RsdCpuReferenceImpl::setPriority(int32_t priority) { |
| 289 | for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { |
| 290 | setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], priority); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | RsdCpuReferenceImpl::~RsdCpuReferenceImpl() { |
| 295 | mExit = true; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 296 | mWorkers.mLaunchData = nullptr; |
| 297 | mWorkers.mLaunchCallback = nullptr; |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 298 | mWorkers.mRunningCount = mWorkers.mCount; |
| 299 | __sync_synchronize(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 300 | for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { |
| 301 | mWorkers.mLaunchSignals[ct].set(); |
| 302 | } |
| 303 | void *res; |
| 304 | for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { |
| 305 | pthread_join(mWorkers.mThreadId[ct], &res); |
| 306 | } |
Miao Wang | 19601aa | 2016-04-21 20:36:57 -0700 | [diff] [blame] | 307 | // b/23109602 |
| 308 | // TODO: Refactor the implementation with threadpool to |
| 309 | // fix the race condition in the destuctor. |
| 310 | // rsAssert(__sync_fetch_and_or(&mWorkers.mRunningCount, 0) == 0); |
Jens Gulin | 07ef704 | 2014-02-19 18:16:01 +0100 | [diff] [blame] | 311 | free(mWorkers.mThreadId); |
| 312 | free(mWorkers.mNativeThreadId); |
| 313 | delete[] mWorkers.mLaunchSignals; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 314 | |
| 315 | // Global structure cleanup. |
| 316 | lockMutex(); |
| 317 | --gThreadTLSKeyCount; |
| 318 | if (!gThreadTLSKeyCount) { |
| 319 | pthread_key_delete(gThreadTLSKey); |
| 320 | } |
| 321 | unlockMutex(); |
| 322 | |
| 323 | } |
| 324 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 325 | // Set up the appropriate input and output pointers to the kernel driver info structure. |
| 326 | // Inputs: |
| 327 | // mtls - The MTLaunchStruct holding information about the kernel launch |
| 328 | // fep - The forEach parameters (driver info structure) |
| 329 | // x, y, z, lod, face, a1, a2, a3, a4 - The start offsets into each dimension |
| 330 | static inline void FepPtrSetup(const MTLaunchStructForEach *mtls, RsExpandKernelDriverInfo *fep, |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 331 | uint32_t x, uint32_t y, |
| 332 | uint32_t z = 0, uint32_t lod = 0, |
| 333 | RsAllocationCubemapFace face = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, |
| 334 | uint32_t a1 = 0, uint32_t a2 = 0, uint32_t a3 = 0, uint32_t a4 = 0) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 335 | for (uint32_t i = 0; i < fep->inLen; i++) { |
| 336 | fep->inPtr[i] = (const uint8_t *)mtls->ains[i]->getPointerUnchecked(x, y, z, lod, face, a1, a2, a3, a4); |
| 337 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 338 | if (mtls->aout[0] != nullptr) { |
| 339 | fep->outPtr[0] = (uint8_t *)mtls->aout[0]->getPointerUnchecked(x, y, z, lod, face, a1, a2, a3, a4); |
| 340 | } |
| 341 | } |
| 342 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 343 | // Set up the appropriate input and output pointers to the kernel driver info structure. |
| 344 | // Inputs: |
| 345 | // mtls - The MTLaunchStruct holding information about the kernel launch |
| 346 | // redp - The reduce parameters (driver info structure) |
| 347 | // x, y, z - The start offsets into each dimension |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 348 | static inline void RedpPtrSetup(const MTLaunchStructReduce *mtls, RsExpandKernelDriverInfo *redp, |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 349 | uint32_t x, uint32_t y, uint32_t z) { |
| 350 | for (uint32_t i = 0; i < redp->inLen; i++) { |
| 351 | redp->inPtr[i] = (const uint8_t *)mtls->ains[i]->getPointerUnchecked(x, y, z); |
| 352 | } |
| 353 | } |
| 354 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 355 | static uint32_t sliceInt(uint32_t *p, uint32_t val, uint32_t start, uint32_t end) { |
| 356 | if (start >= end) { |
| 357 | *p = start; |
| 358 | return val; |
| 359 | } |
| 360 | |
| 361 | uint32_t div = end - start; |
| 362 | |
| 363 | uint32_t n = val / div; |
| 364 | *p = (val - (n * div)) + start; |
| 365 | return n; |
| 366 | } |
| 367 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 368 | static bool SelectOuterSlice(const MTLaunchStructCommon *mtls, RsExpandKernelDriverInfo* info, uint32_t sliceNum) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 369 | uint32_t r = sliceNum; |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 370 | r = sliceInt(&info->current.z, r, mtls->start.z, mtls->end.z); |
| 371 | r = sliceInt(&info->current.lod, r, mtls->start.lod, mtls->end.lod); |
| 372 | r = sliceInt(&info->current.face, r, mtls->start.face, mtls->end.face); |
| 373 | r = sliceInt(&info->current.array[0], r, mtls->start.array[0], mtls->end.array[0]); |
| 374 | r = sliceInt(&info->current.array[1], r, mtls->start.array[1], mtls->end.array[1]); |
| 375 | r = sliceInt(&info->current.array[2], r, mtls->start.array[2], mtls->end.array[2]); |
| 376 | r = sliceInt(&info->current.array[3], r, mtls->start.array[3], mtls->end.array[3]); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 377 | return r == 0; |
| 378 | } |
| 379 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 380 | static bool SelectZSlice(const MTLaunchStructCommon *mtls, RsExpandKernelDriverInfo* info, uint32_t sliceNum) { |
| 381 | return sliceInt(&info->current.z, sliceNum, mtls->start.z, mtls->end.z) == 0; |
| 382 | } |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 383 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 384 | static void walk_general_foreach(void *usr, uint32_t idx) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 385 | MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr; |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 386 | RsExpandKernelDriverInfo fep = mtls->fep; |
| 387 | fep.lid = idx; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 388 | ForEachFunc_t fn = mtls->kernel; |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 389 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 390 | while(1) { |
| 391 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
| 392 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 393 | if (!SelectOuterSlice(mtls, &fep, slice)) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 397 | for (fep.current.y = mtls->start.y; fep.current.y < mtls->end.y; |
| 398 | fep.current.y++) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 399 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 400 | FepPtrSetup(mtls, &fep, mtls->start.x, |
| 401 | fep.current.y, fep.current.z, fep.current.lod, |
| 402 | (RsAllocationCubemapFace)fep.current.face, |
| 403 | fep.current.array[0], fep.current.array[1], |
| 404 | fep.current.array[2], fep.current.array[3]); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 405 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 406 | fn(&fep, mtls->start.x, mtls->end.x, mtls->fep.outStride[0]); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 409 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 410 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 411 | static void walk_2d_foreach(void *usr, uint32_t idx) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 412 | MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 413 | RsExpandKernelDriverInfo fep = mtls->fep; |
| 414 | fep.lid = idx; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 415 | ForEachFunc_t fn = mtls->kernel; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 416 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 417 | while (1) { |
| 418 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 419 | uint32_t yStart = mtls->start.y + slice * mtls->mSliceSize; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 420 | uint32_t yEnd = yStart + mtls->mSliceSize; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 421 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 422 | yEnd = rsMin(yEnd, mtls->end.y); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 423 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 424 | if (yEnd <= yStart) { |
| 425 | return; |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 426 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 427 | |
| 428 | for (fep.current.y = yStart; fep.current.y < yEnd; fep.current.y++) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 429 | FepPtrSetup(mtls, &fep, mtls->start.x, fep.current.y); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 430 | |
David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 431 | fn(&fep, mtls->start.x, mtls->end.x, fep.outStride[0]); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 434 | } |
| 435 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 436 | static void walk_1d_foreach(void *usr, uint32_t idx) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 437 | MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 438 | RsExpandKernelDriverInfo fep = mtls->fep; |
| 439 | fep.lid = idx; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 440 | ForEachFunc_t fn = mtls->kernel; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 441 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 442 | while (1) { |
| 443 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 444 | uint32_t xStart = mtls->start.x + slice * mtls->mSliceSize; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 445 | uint32_t xEnd = xStart + mtls->mSliceSize; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 446 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 447 | xEnd = rsMin(xEnd, mtls->end.x); |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 448 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 449 | if (xEnd <= xStart) { |
| 450 | return; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 451 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 452 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 453 | FepPtrSetup(mtls, &fep, xStart, 0); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 454 | |
David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 455 | fn(&fep, xStart, xEnd, fep.outStride[0]); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 456 | } |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 457 | } |
| 458 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 459 | // The function format_bytes() is an auxiliary function to assist in logging. |
| 460 | // |
| 461 | // Bytes are read from an input (inBuf) and written (as pairs of hex digits) |
| 462 | // to an output (outBuf). |
| 463 | // |
| 464 | // Output format: |
| 465 | // - starts with ": " |
| 466 | // - each input byte is translated to a pair of hex digits |
| 467 | // - bytes are separated by "." except that every fourth separator is "|" |
| 468 | // - if the input is sufficiently long, the output is truncated and terminated with "..." |
| 469 | // |
| 470 | // Arguments: |
| 471 | // - outBuf -- Pointer to buffer of type "FormatBuf" into which output is written |
| 472 | // - inBuf -- Pointer to bytes which are to be formatted into outBuf |
| 473 | // - inBytes -- Number of bytes in inBuf |
| 474 | // |
| 475 | // Constant: |
| 476 | // - kFormatInBytesMax -- Only min(kFormatInBytesMax, inBytes) bytes will be read |
| 477 | // from inBuf |
| 478 | // |
| 479 | // Return value: |
| 480 | // - pointer (const char *) to output (which is part of outBuf) |
| 481 | // |
| 482 | static const int kFormatInBytesMax = 16; |
| 483 | // ": " + 2 digits per byte + 1 separator between bytes + "..." + null |
| 484 | typedef char FormatBuf[2 + kFormatInBytesMax*2 + (kFormatInBytesMax - 1) + 3 + 1]; |
| 485 | static const char *format_bytes(FormatBuf *outBuf, const uint8_t *inBuf, const int inBytes) { |
| 486 | strcpy(*outBuf, ": "); |
| 487 | int pos = 2; |
| 488 | const int lim = std::min(kFormatInBytesMax, inBytes); |
| 489 | for (int i = 0; i < lim; ++i) { |
| 490 | if (i) { |
| 491 | sprintf(*outBuf + pos, (i % 4 ? "." : "|")); |
| 492 | ++pos; |
| 493 | } |
| 494 | sprintf(*outBuf + pos, "%02x", inBuf[i]); |
| 495 | pos += 2; |
| 496 | } |
| 497 | if (kFormatInBytesMax < inBytes) |
| 498 | strcpy(*outBuf + pos, "..."); |
| 499 | return *outBuf; |
| 500 | } |
| 501 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 502 | static void reduce_get_accumulator(uint8_t *&accumPtr, const MTLaunchStructReduce *mtls, |
| 503 | const char *walkerName, uint32_t threadIdx) { |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 504 | rsAssert(!accumPtr); |
| 505 | |
| 506 | uint32_t accumIdx = (uint32_t)__sync_fetch_and_add(&mtls->accumCount, 1); |
| 507 | if (mtls->outFunc) { |
| 508 | accumPtr = mtls->accumAlloc + mtls->accumStride * accumIdx; |
| 509 | } else { |
| 510 | if (accumIdx == 0) { |
| 511 | accumPtr = mtls->redp.outPtr[0]; |
| 512 | } else { |
| 513 | accumPtr = mtls->accumAlloc + mtls->accumStride * (accumIdx - 1); |
| 514 | } |
| 515 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 516 | REDUCE_ALOGV(mtls, 2, "%s(%p): idx = %u got accumCount %u and accumPtr %p", |
| 517 | walkerName, mtls->accumFunc, threadIdx, accumIdx, accumPtr); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 518 | // initialize accumulator |
| 519 | if (mtls->initFunc) { |
| 520 | mtls->initFunc(accumPtr); |
| 521 | } else { |
| 522 | memset(accumPtr, 0, mtls->accumSize); |
| 523 | } |
| 524 | } |
| 525 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 526 | static void walk_1d_reduce(void *usr, uint32_t idx) { |
| 527 | const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 528 | RsExpandKernelDriverInfo redp = mtls->redp; |
| 529 | |
| 530 | // find accumulator |
| 531 | uint8_t *&accumPtr = mtls->accumPtr[idx]; |
| 532 | if (!accumPtr) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 533 | reduce_get_accumulator(accumPtr, mtls, __func__, idx); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 537 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 538 | while (1) { |
| 539 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
| 540 | uint32_t xStart = mtls->start.x + slice * mtls->mSliceSize; |
| 541 | uint32_t xEnd = xStart + mtls->mSliceSize; |
| 542 | |
| 543 | xEnd = rsMin(xEnd, mtls->end.x); |
| 544 | |
| 545 | if (xEnd <= xStart) { |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | RedpPtrSetup(mtls, &redp, xStart, 0, 0); |
| 550 | fn(&redp, xStart, xEnd, accumPtr); |
| 551 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 552 | // Emit log line after slice has been run, so that we can include |
| 553 | // the results of the run on that line. |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 554 | FormatBuf fmt; |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 555 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 556 | format_bytes(&fmt, accumPtr, mtls->accumSize); |
| 557 | } else { |
| 558 | fmt[0] = 0; |
| 559 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 560 | REDUCE_ALOGV(mtls, 2, "walk_1d_reduce(%p): idx = %u, x in [%u, %u)%s", |
| 561 | mtls->accumFunc, idx, xStart, xEnd, fmt); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 565 | static void walk_2d_reduce(void *usr, uint32_t idx) { |
| 566 | const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 567 | RsExpandKernelDriverInfo redp = mtls->redp; |
| 568 | |
| 569 | // find accumulator |
| 570 | uint8_t *&accumPtr = mtls->accumPtr[idx]; |
| 571 | if (!accumPtr) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 572 | reduce_get_accumulator(accumPtr, mtls, __func__, idx); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 576 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 577 | while (1) { |
| 578 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
| 579 | uint32_t yStart = mtls->start.y + slice * mtls->mSliceSize; |
| 580 | uint32_t yEnd = yStart + mtls->mSliceSize; |
| 581 | |
| 582 | yEnd = rsMin(yEnd, mtls->end.y); |
| 583 | |
| 584 | if (yEnd <= yStart) { |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | for (redp.current.y = yStart; redp.current.y < yEnd; redp.current.y++) { |
| 589 | RedpPtrSetup(mtls, &redp, mtls->start.x, redp.current.y, 0); |
| 590 | fn(&redp, mtls->start.x, mtls->end.x, accumPtr); |
| 591 | } |
| 592 | |
| 593 | FormatBuf fmt; |
| 594 | if (mtls->logReduce >= 3) { |
| 595 | format_bytes(&fmt, accumPtr, mtls->accumSize); |
| 596 | } else { |
| 597 | fmt[0] = 0; |
| 598 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 599 | REDUCE_ALOGV(mtls, 2, "walk_2d_reduce(%p): idx = %u, y in [%u, %u)%s", |
| 600 | mtls->accumFunc, idx, yStart, yEnd, fmt); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 604 | static void walk_3d_reduce(void *usr, uint32_t idx) { |
| 605 | const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 606 | RsExpandKernelDriverInfo redp = mtls->redp; |
| 607 | |
| 608 | // find accumulator |
| 609 | uint8_t *&accumPtr = mtls->accumPtr[idx]; |
| 610 | if (!accumPtr) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 611 | reduce_get_accumulator(accumPtr, mtls, __func__, idx); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 615 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 616 | while (1) { |
| 617 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
| 618 | |
| 619 | if (!SelectZSlice(mtls, &redp, slice)) { |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | for (redp.current.y = mtls->start.y; redp.current.y < mtls->end.y; redp.current.y++) { |
| 624 | RedpPtrSetup(mtls, &redp, mtls->start.x, redp.current.y, redp.current.z); |
| 625 | fn(&redp, mtls->start.x, mtls->end.x, accumPtr); |
| 626 | } |
| 627 | |
| 628 | FormatBuf fmt; |
| 629 | if (mtls->logReduce >= 3) { |
| 630 | format_bytes(&fmt, accumPtr, mtls->accumSize); |
| 631 | } else { |
| 632 | fmt[0] = 0; |
| 633 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 634 | REDUCE_ALOGV(mtls, 2, "walk_3d_reduce(%p): idx = %u, z = %u%s", |
| 635 | mtls->accumFunc, idx, redp.current.z, fmt); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 639 | // Launch a general reduce-style kernel. |
| 640 | // Inputs: |
| 641 | // ains[0..inLen-1]: Array of allocations that contain the inputs |
| 642 | // aout: The allocation that will hold the output |
| 643 | // mtls: Holds launch parameters |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 644 | void RsdCpuReferenceImpl::launchReduce(const Allocation ** ains, |
| 645 | uint32_t inLen, |
| 646 | Allocation * aout, |
| 647 | MTLaunchStructReduce *mtls) { |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 648 | mtls->logReduce = mRSC->props.mLogReduce; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 649 | if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInKernel) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 650 | launchReduceParallel(ains, inLen, aout, mtls); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 651 | } else { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 652 | launchReduceSerial(ains, inLen, aout, mtls); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | // Launch a general reduce-style kernel, single-threaded. |
| 657 | // Inputs: |
| 658 | // ains[0..inLen-1]: Array of allocations that contain the inputs |
| 659 | // aout: The allocation that will hold the output |
| 660 | // mtls: Holds launch parameters |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 661 | void RsdCpuReferenceImpl::launchReduceSerial(const Allocation ** ains, |
| 662 | uint32_t inLen, |
| 663 | Allocation * aout, |
| 664 | MTLaunchStructReduce *mtls) { |
| 665 | REDUCE_ALOGV(mtls, 1, "launchReduceSerial(%p): %u x %u x %u", mtls->accumFunc, |
| 666 | mtls->redp.dim.x, mtls->redp.dim.y, mtls->redp.dim.z); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 667 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 668 | // In the presence of outconverter, we allocate temporary memory for |
| 669 | // the accumulator. |
| 670 | // |
| 671 | // In the absence of outconverter, we use the output allocation as the |
| 672 | // accumulator. |
| 673 | uint8_t *const accumPtr = (mtls->outFunc |
| 674 | ? static_cast<uint8_t *>(malloc(mtls->accumSize)) |
| 675 | : mtls->redp.outPtr[0]); |
| 676 | |
| 677 | // initialize |
| 678 | if (mtls->initFunc) { |
| 679 | mtls->initFunc(accumPtr); |
| 680 | } else { |
| 681 | memset(accumPtr, 0, mtls->accumSize); |
| 682 | } |
| 683 | |
| 684 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 685 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 686 | uint32_t slice = 0; |
| 687 | while (SelectOuterSlice(mtls, &mtls->redp, slice++)) { |
| 688 | for (mtls->redp.current.y = mtls->start.y; |
| 689 | mtls->redp.current.y < mtls->end.y; |
| 690 | mtls->redp.current.y++) { |
| 691 | RedpPtrSetup(mtls, &mtls->redp, mtls->start.x, mtls->redp.current.y, mtls->redp.current.z); |
| 692 | fn(&mtls->redp, mtls->start.x, mtls->end.x, accumPtr); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | // outconvert |
| 697 | if (mtls->outFunc) { |
| 698 | mtls->outFunc(mtls->redp.outPtr[0], accumPtr); |
| 699 | free(accumPtr); |
| 700 | } |
| 701 | } |
| 702 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 703 | // Launch a general reduce-style kernel, multi-threaded. |
| 704 | // Inputs: |
| 705 | // ains[0..inLen-1]: Array of allocations that contain the inputs |
| 706 | // aout: The allocation that will hold the output |
| 707 | // mtls: Holds launch parameters |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 708 | void RsdCpuReferenceImpl::launchReduceParallel(const Allocation ** ains, |
| 709 | uint32_t inLen, |
| 710 | Allocation * aout, |
| 711 | MTLaunchStructReduce *mtls) { |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 712 | // For now, we don't know how to go parallel in the absence of a combiner. |
| 713 | if (!mtls->combFunc) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 714 | launchReduceSerial(ains, inLen, aout, mtls); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | |
| 718 | // Number of threads = "main thread" + number of other (worker) threads |
| 719 | const uint32_t numThreads = mWorkers.mCount + 1; |
| 720 | |
| 721 | // In the absence of outconverter, we use the output allocation as |
| 722 | // an accumulator, and therefore need to allocate one fewer accumulator. |
| 723 | const uint32_t numAllocAccum = numThreads - (mtls->outFunc == nullptr); |
| 724 | |
| 725 | // If mDebugReduceSplitAccum, then we want each accumulator to start |
| 726 | // on a page boundary. (TODO: Would some unit smaller than a page |
| 727 | // be sufficient to avoid false sharing?) |
| 728 | if (mRSC->props.mDebugReduceSplitAccum) { |
| 729 | // Round up accumulator size to an integral number of pages |
| 730 | mtls->accumStride = |
| 731 | (unsigned(mtls->accumSize) + unsigned(mPageSize)-1) & |
| 732 | ~(unsigned(mPageSize)-1); |
| 733 | // Each accumulator gets its own page. Alternatively, if we just |
| 734 | // wanted to make sure no two accumulators are on the same page, |
| 735 | // we could instead do |
| 736 | // allocSize = mtls->accumStride * (numAllocation - 1) + mtls->accumSize |
| 737 | const size_t allocSize = mtls->accumStride * numAllocAccum; |
| 738 | mtls->accumAlloc = static_cast<uint8_t *>(memalign(mPageSize, allocSize)); |
| 739 | } else { |
| 740 | mtls->accumStride = mtls->accumSize; |
| 741 | mtls->accumAlloc = static_cast<uint8_t *>(malloc(mtls->accumStride * numAllocAccum)); |
| 742 | } |
| 743 | |
| 744 | const size_t accumPtrArrayBytes = sizeof(uint8_t *) * numThreads; |
| 745 | mtls->accumPtr = static_cast<uint8_t **>(malloc(accumPtrArrayBytes)); |
| 746 | memset(mtls->accumPtr, 0, accumPtrArrayBytes); |
| 747 | |
| 748 | mtls->accumCount = 0; |
| 749 | |
| 750 | rsAssert(!mInKernel); |
| 751 | mInKernel = true; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 752 | REDUCE_ALOGV(mtls, 1, "launchReduceParallel(%p): %u x %u x %u, %u threads, accumAlloc = %p", |
| 753 | mtls->accumFunc, |
| 754 | mtls->redp.dim.x, mtls->redp.dim.y, mtls->redp.dim.z, |
| 755 | numThreads, mtls->accumAlloc); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 756 | if (mtls->redp.dim.z > 1) { |
| 757 | mtls->mSliceSize = 1; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 758 | launchThreads(walk_3d_reduce, mtls); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 759 | } else if (mtls->redp.dim.y > 1) { |
| 760 | mtls->mSliceSize = rsMax(1U, mtls->redp.dim.y / (numThreads * 4)); |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 761 | launchThreads(walk_2d_reduce, mtls); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 762 | } else { |
| 763 | mtls->mSliceSize = rsMax(1U, mtls->redp.dim.x / (numThreads * 4)); |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 764 | launchThreads(walk_1d_reduce, mtls); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 765 | } |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 766 | mInKernel = false; |
| 767 | |
| 768 | // Combine accumulators and identify final accumulator |
| 769 | uint8_t *finalAccumPtr = (mtls->outFunc ? nullptr : mtls->redp.outPtr[0]); |
| 770 | // Loop over accumulators, combining into finalAccumPtr. If finalAccumPtr |
| 771 | // is null, then the first accumulator I find becomes finalAccumPtr. |
| 772 | for (unsigned idx = 0; idx < mtls->accumCount; ++idx) { |
| 773 | uint8_t *const thisAccumPtr = mtls->accumPtr[idx]; |
| 774 | if (finalAccumPtr) { |
| 775 | if (finalAccumPtr != thisAccumPtr) { |
| 776 | if (mtls->combFunc) { |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 777 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 778 | FormatBuf fmt; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 779 | REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): accumulating into%s", |
| 780 | mtls->accumFunc, |
| 781 | format_bytes(&fmt, finalAccumPtr, mtls->accumSize)); |
| 782 | REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): accumulator[%d]%s", |
| 783 | mtls->accumFunc, idx, |
| 784 | format_bytes(&fmt, thisAccumPtr, mtls->accumSize)); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 785 | } |
| 786 | mtls->combFunc(finalAccumPtr, thisAccumPtr); |
| 787 | } else { |
| 788 | rsAssert(!"expected combiner"); |
| 789 | } |
| 790 | } |
| 791 | } else { |
| 792 | finalAccumPtr = thisAccumPtr; |
| 793 | } |
| 794 | } |
| 795 | rsAssert(finalAccumPtr != nullptr); |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 796 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 797 | FormatBuf fmt; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 798 | REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): final accumulator%s", |
| 799 | mtls->accumFunc, format_bytes(&fmt, finalAccumPtr, mtls->accumSize)); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | // Outconvert |
| 803 | if (mtls->outFunc) { |
| 804 | mtls->outFunc(mtls->redp.outPtr[0], finalAccumPtr); |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 805 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 806 | FormatBuf fmt; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 807 | REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): final outconverted result%s", |
| 808 | mtls->accumFunc, |
| 809 | format_bytes(&fmt, mtls->redp.outPtr[0], mtls->redp.outStride[0])); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | // Clean up |
| 814 | free(mtls->accumPtr); |
| 815 | free(mtls->accumAlloc); |
| 816 | } |
| 817 | |
| 818 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 819 | void RsdCpuReferenceImpl::launchForEach(const Allocation ** ains, |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 820 | uint32_t inLen, |
| 821 | Allocation* aout, |
| 822 | const RsScriptCall* sc, |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 823 | MTLaunchStructForEach* mtls) { |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 824 | |
| 825 | //android::StopWatch kernel_time("kernel time"); |
| 826 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 827 | bool outerDims = (mtls->start.z != mtls->end.z) || |
| 828 | (mtls->start.face != mtls->end.face) || |
| 829 | (mtls->start.lod != mtls->end.lod) || |
| 830 | (mtls->start.array[0] != mtls->end.array[0]) || |
| 831 | (mtls->start.array[1] != mtls->end.array[1]) || |
| 832 | (mtls->start.array[2] != mtls->end.array[2]) || |
| 833 | (mtls->start.array[3] != mtls->end.array[3]); |
| 834 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 835 | if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInKernel) { |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 836 | const size_t targetByteChunk = 16 * 1024; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 837 | mInKernel = true; // NOTE: The guard immediately above ensures this was !mInKernel |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 838 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 839 | if (outerDims) { |
| 840 | // No fancy logic for chunk size |
| 841 | mtls->mSliceSize = 1; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 842 | launchThreads(walk_general_foreach, mtls); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 843 | } else if (mtls->fep.dim.y > 1) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 844 | uint32_t s1 = mtls->fep.dim.y / ((mWorkers.mCount + 1) * 4); |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 845 | uint32_t s2 = 0; |
| 846 | |
| 847 | // This chooses our slice size to rate limit atomic ops to |
| 848 | // one per 16k bytes of reads/writes. |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 849 | if ((mtls->aout[0] != nullptr) && mtls->aout[0]->mHal.drvState.lod[0].stride) { |
| 850 | s2 = targetByteChunk / mtls->aout[0]->mHal.drvState.lod[0].stride; |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 851 | } else if (mtls->ains[0]) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 852 | s2 = targetByteChunk / mtls->ains[0]->mHal.drvState.lod[0].stride; |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 853 | } else { |
| 854 | // Launch option only case |
| 855 | // Use s1 based only on the dimensions |
| 856 | s2 = s1; |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 857 | } |
| 858 | mtls->mSliceSize = rsMin(s1, s2); |
| 859 | |
| 860 | if(mtls->mSliceSize < 1) { |
| 861 | mtls->mSliceSize = 1; |
| 862 | } |
| 863 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 864 | launchThreads(walk_2d_foreach, mtls); |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 865 | } else { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 866 | uint32_t s1 = mtls->fep.dim.x / ((mWorkers.mCount + 1) * 4); |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 867 | uint32_t s2 = 0; |
| 868 | |
| 869 | // This chooses our slice size to rate limit atomic ops to |
| 870 | // one per 16k bytes of reads/writes. |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 871 | if ((mtls->aout[0] != nullptr) && mtls->aout[0]->getType()->getElementSizeBytes()) { |
| 872 | s2 = targetByteChunk / mtls->aout[0]->getType()->getElementSizeBytes(); |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 873 | } else if (mtls->ains[0]) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 874 | s2 = targetByteChunk / mtls->ains[0]->getType()->getElementSizeBytes(); |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 875 | } else { |
| 876 | // Launch option only case |
| 877 | // Use s1 based only on the dimensions |
| 878 | s2 = s1; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 879 | } |
| 880 | mtls->mSliceSize = rsMin(s1, s2); |
| 881 | |
| 882 | if (mtls->mSliceSize < 1) { |
| 883 | mtls->mSliceSize = 1; |
| 884 | } |
| 885 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 886 | launchThreads(walk_1d_foreach, mtls); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 887 | } |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 888 | mInKernel = false; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 889 | |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 890 | } else { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 891 | ForEachFunc_t fn = mtls->kernel; |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 892 | uint32_t slice = 0; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 893 | |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 894 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 895 | while(SelectOuterSlice(mtls, &mtls->fep, slice++)) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 896 | for (mtls->fep.current.y = mtls->start.y; |
| 897 | mtls->fep.current.y < mtls->end.y; |
| 898 | mtls->fep.current.y++) { |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 899 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 900 | FepPtrSetup(mtls, &mtls->fep, mtls->start.x, |
| 901 | mtls->fep.current.y, mtls->fep.current.z, mtls->fep.current.lod, |
| 902 | (RsAllocationCubemapFace) mtls->fep.current.face, |
| 903 | mtls->fep.current.array[0], mtls->fep.current.array[1], |
| 904 | mtls->fep.current.array[2], mtls->fep.current.array[3]); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 905 | |
David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 906 | fn(&mtls->fep, mtls->start.x, mtls->end.x, mtls->fep.outStride[0]); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 912 | RsdCpuScriptImpl * RsdCpuReferenceImpl::setTLS(RsdCpuScriptImpl *sc) { |
| 913 | //ALOGE("setTls %p", sc); |
| 914 | ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); |
| 915 | rsAssert(tls); |
| 916 | RsdCpuScriptImpl *old = tls->mImpl; |
| 917 | tls->mImpl = sc; |
| 918 | tls->mContext = mRSC; |
| 919 | if (sc) { |
| 920 | tls->mScript = sc->getScript(); |
| 921 | } else { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 922 | tls->mScript = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 923 | } |
| 924 | return old; |
| 925 | } |
| 926 | |
| 927 | const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) { |
| 928 | return mSymLookupFn(mRSC, name); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createScript(const ScriptC *s, |
| 933 | char const *resName, char const *cacheDir, |
| 934 | uint8_t const *bitcode, size_t bitcodeSize, |
| 935 | uint32_t flags) { |
| 936 | |
| 937 | RsdCpuScriptImpl *i = new RsdCpuScriptImpl(this, s); |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 938 | if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 939 | , getBccPluginName() |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 940 | )) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 941 | delete i; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 942 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 943 | } |
| 944 | return i; |
| 945 | } |
| 946 | |
Jason Sams | 7c4b888 | 2013-01-04 10:50:05 -0800 | [diff] [blame] | 947 | extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx, |
| 948 | const Script *s, const Element *e); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 949 | extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx, |
| 950 | const Script *s, const Element *e); |
| 951 | extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx, |
| 952 | const Script *s, const Element *e); |
| 953 | extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx, |
| 954 | const Script *s, const Element *e); |
| 955 | extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx, |
| 956 | const Script *s, const Element *e); |
| 957 | extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, |
| 958 | const Script *s, const Element *e); |
| 959 | extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, |
| 960 | const Script *s, const Element *e); |
| 961 | extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx, |
| 962 | const Script *s, const Element *e); |
Jason Sams | 2282e28 | 2013-06-17 16:52:01 -0700 | [diff] [blame] | 963 | extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx, |
| 964 | const Script *s, const Element *e); |
Jason Sams | 39ab94a | 2014-04-16 17:14:05 -0700 | [diff] [blame] | 965 | extern RsdCpuScriptImpl * rsdIntrinsic_Resize(RsdCpuReferenceImpl *ctx, |
| 966 | const Script *s, const Element *e); |
Tim Murray | 64c682b | 2015-01-09 12:08:43 -0800 | [diff] [blame] | 967 | extern RsdCpuScriptImpl * rsdIntrinsic_BLAS(RsdCpuReferenceImpl *ctx, |
| 968 | const Script *s, const Element *e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 969 | |
| 970 | RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s, |
| 971 | RsScriptIntrinsicID iid, Element *e) { |
| 972 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 973 | RsdCpuScriptImpl *i = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 974 | switch (iid) { |
Jason Sams | 7c4b888 | 2013-01-04 10:50:05 -0800 | [diff] [blame] | 975 | case RS_SCRIPT_INTRINSIC_ID_3DLUT: |
| 976 | i = rsdIntrinsic_3DLUT(this, s, e); |
| 977 | break; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 978 | case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 979 | i = rsdIntrinsic_Convolve3x3(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 980 | break; |
| 981 | case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 982 | i = rsdIntrinsic_ColorMatrix(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 983 | break; |
| 984 | case RS_SCRIPT_INTRINSIC_ID_LUT: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 985 | i = rsdIntrinsic_LUT(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 986 | break; |
| 987 | case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 988 | i = rsdIntrinsic_Convolve5x5(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 989 | break; |
| 990 | case RS_SCRIPT_INTRINSIC_ID_BLUR: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 991 | i = rsdIntrinsic_Blur(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 992 | break; |
| 993 | case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 994 | i = rsdIntrinsic_YuvToRGB(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 995 | break; |
| 996 | case RS_SCRIPT_INTRINSIC_ID_BLEND: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 997 | i = rsdIntrinsic_Blend(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 998 | break; |
Jason Sams | 2282e28 | 2013-06-17 16:52:01 -0700 | [diff] [blame] | 999 | case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM: |
| 1000 | i = rsdIntrinsic_Histogram(this, s, e); |
| 1001 | break; |
Jason Sams | 39ab94a | 2014-04-16 17:14:05 -0700 | [diff] [blame] | 1002 | case RS_SCRIPT_INTRINSIC_ID_RESIZE: |
| 1003 | i = rsdIntrinsic_Resize(this, s, e); |
| 1004 | break; |
Tim Murray | 64c682b | 2015-01-09 12:08:43 -0800 | [diff] [blame] | 1005 | case RS_SCRIPT_INTRINSIC_ID_BLAS: |
| 1006 | i = rsdIntrinsic_BLAS(this, s, e); |
| 1007 | break; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1008 | |
| 1009 | default: |
| 1010 | rsAssert(0); |
| 1011 | } |
| 1012 | |
| 1013 | return i; |
| 1014 | } |
| 1015 | |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 1016 | void* RsdCpuReferenceImpl::createScriptGroup(const ScriptGroupBase *sg) { |
| 1017 | switch (sg->getApiVersion()) { |
| 1018 | case ScriptGroupBase::SG_V1: { |
| 1019 | CpuScriptGroupImpl *sgi = new CpuScriptGroupImpl(this, sg); |
| 1020 | if (!sgi->init()) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1021 | delete sgi; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1022 | return nullptr; |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 1023 | } |
| 1024 | return sgi; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1025 | } |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 1026 | case ScriptGroupBase::SG_V2: { |
| 1027 | return new CpuScriptGroup2Impl(this, sg); |
| 1028 | } |
| 1029 | } |
| 1030 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1031 | } |
Chih-Hung Hsieh | 462de21 | 2016-11-16 11:33:57 -0800 | [diff] [blame] | 1032 | |
| 1033 | } // namespace renderscript |
| 1034 | } // namespace android |