blob: 30a4c5fd9fae7078ab247e2804e8f4179a241bef [file] [log] [blame]
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07001/*
Jason Samsfe1d5ff2012-03-23 11:47:26 -07002 * Copyright (C) 2011-2012 The Android Open Source Project
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07003 *
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
18#include "rsdCore.h"
Jason Sams7e8aae72011-05-26 16:33:01 -070019#include "rsdAllocation.h"
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070020#include "rsdProgramVertex.h"
21#include "rsdShader.h"
22#include "rsdShaderCache.h"
23
24#include "rsContext.h"
25#include "rsProgramVertex.h"
26#include "rsProgramFragment.h"
27
28#include <GLES/gl.h>
29#include <GLES/glext.h>
30#include <GLES2/gl2.h>
31#include <GLES2/gl2ext.h>
32
33using namespace android;
34using namespace android::renderscript;
35
36bool rsdProgramVertexInit(const Context *rsc, const ProgramVertex *pv,
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080037 const char* shader, size_t shaderLen,
38 const char** textureNames, size_t textureNamesCount,
39 const size_t *textureNamesLength) {
40 RsdShader *drv = new RsdShader(pv, GL_VERTEX_SHADER, shader, shaderLen,
41 textureNames, textureNamesCount, textureNamesLength);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070042 pv->mHal.drv = drv;
43
Jason Samsfe1d5ff2012-03-23 11:47:26 -070044 return true;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070045}
46
Jason Sams7e8aae72011-05-26 16:33:01 -070047static void SyncProgramConstants(const Context *rsc, const Program *p) {
48 for (uint32_t ct=0; ct < p->mHal.state.texturesCount; ct++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070049 const Allocation *a = p->mHal.state.textures[ct];
Alex Sakhartchoukbbc41c02011-08-05 15:27:25 -070050 if (!a) {
51 continue;
52 }
Jason Sams7e8aae72011-05-26 16:33:01 -070053 DrvAllocation *drvAlloc = (DrvAllocation *)a->mHal.drv;
54 if (drvAlloc->uploadDeferred) {
55 rsdAllocationSyncAll(rsc, a, RS_ALLOCATION_USAGE_SCRIPT);
56 }
57 }
58}
59
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070060void rsdProgramVertexSetActive(const Context *rsc, const ProgramVertex *pv) {
61 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
62
Jason Sams7e8aae72011-05-26 16:33:01 -070063 SyncProgramConstants(rsc, pv);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070064 dc->gl.shaderCache->setActiveVertex((RsdShader*)pv->mHal.drv);
65}
66
67void rsdProgramVertexDestroy(const Context *rsc, const ProgramVertex *pv) {
68 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
69
70 RsdShader *drv = NULL;
71 if(pv->mHal.drv) {
72 drv = (RsdShader*)pv->mHal.drv;
73 if (rsc->props.mLogShaders) {
Steve Block71f2cf12011-10-20 11:56:00 +010074 ALOGV("Destroying vertex shader with ID %u", drv->getShaderID());
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070075 }
76 if (drv->getShaderID()) {
77 dc->gl.shaderCache->cleanupVertex(drv->getShaderID());
78 }
79 delete drv;
80 }
81}
82
83bool rsdProgramFragmentInit(const Context *rsc, const ProgramFragment *pf,
Alex Sakhartchouk2123b462012-02-15 16:21:46 -080084 const char* shader, size_t shaderLen,
85 const char** textureNames, size_t textureNamesCount,
86 const size_t *textureNamesLength) {
87 RsdShader *drv = new RsdShader(pf, GL_FRAGMENT_SHADER, shader, shaderLen,
88 textureNames, textureNamesCount, textureNamesLength);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070089 pf->mHal.drv = drv;
90
Jason Samsfe1d5ff2012-03-23 11:47:26 -070091 return true;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070092}
93
94void rsdProgramFragmentSetActive(const Context *rsc, const ProgramFragment *pf) {
95 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
96
Jason Sams7e8aae72011-05-26 16:33:01 -070097 SyncProgramConstants(rsc, pf);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070098 dc->gl.shaderCache->setActiveFragment((RsdShader*)pf->mHal.drv);
99}
100
101void rsdProgramFragmentDestroy(const Context *rsc, const ProgramFragment *pf) {
102 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
103
104 RsdShader *drv = NULL;
105 if(pf->mHal.drv) {
106 drv = (RsdShader*)pf->mHal.drv;
107 if (rsc->props.mLogShaders) {
Steve Block71f2cf12011-10-20 11:56:00 +0100108 ALOGV("Destroying fragment shader with ID %u", drv->getShaderID());
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700109 }
110 if (drv->getShaderID()) {
111 dc->gl.shaderCache->cleanupFragment(drv->getShaderID());
112 }
113 delete drv;
114 }
115}
116
117