blob: 418f1279c0579a24497b67d343ea2bb97de4d538 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 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
Jason Sams326e0dd2009-05-22 14:03:28 -070017#include <GLES/gl.h>
18#include <GLES/glext.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070019
20#include "rsContext.h"
21#include "rsSampler.h"
22
Jason Sams1aa5a4e2009-06-22 17:15:15 -070023
Jason Sams326e0dd2009-05-22 14:03:28 -070024using namespace android;
25using namespace android::renderscript;
26
27
28Sampler::Sampler()
29{
30 // Should not get called.
31 rsAssert(0);
32}
33
34Sampler::Sampler(RsSamplerValue magFilter,
35 RsSamplerValue minFilter,
36 RsSamplerValue wrapS,
37 RsSamplerValue wrapT,
38 RsSamplerValue wrapR)
39{
40 mMagFilter = magFilter;
41 mMinFilter = minFilter;
42 mWrapS = wrapS;
43 mWrapT = wrapT;
44 mWrapR = wrapR;
45}
46
47Sampler::~Sampler()
48{
49}
50
51void Sampler::setupGL()
52{
Jason Sams2f2898c2009-05-28 16:16:24 -070053 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070054 GL_NEAREST, //RS_SAMPLER_NEAREST,
55 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-05-28 16:16:24 -070056 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
57 GL_REPEAT, //RS_SAMPLER_WRAP,
58 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams39c8bc72009-05-28 15:37:57 -070059
Jason Sams2f2898c2009-05-28 16:16:24 -070060 };
Jason Sams39c8bc72009-05-28 15:37:57 -070061
62
Jason Sams2f2898c2009-05-28 16:16:24 -070063 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Sams326e0dd2009-05-22 14:03:28 -070067
68}
69
70void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
71{
72 ss->mSamplers[slot].set(this);
73 mBoundSlot = slot;
74}
75
76void Sampler::unbindFromContext(SamplerState *ss)
77{
78 int32_t slot = mBoundSlot;
79 mBoundSlot = -1;
80 ss->mSamplers[slot].clear();
81}
82
83void SamplerState::setupGL()
84{
Jason Sams39c8bc72009-05-28 15:37:57 -070085 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070086 Sampler *s = mSamplers[ct].get();
87 if (s) {
88 s->setupGL();
89 } else {
90 glBindTexture(GL_TEXTURE_2D, 0);
91 }
92 }
93}
94
95////////////////////////////////
96
97namespace android {
98namespace renderscript {
99
100
101void rsi_SamplerBegin(Context *rsc)
102{
103 SamplerState * ss = &rsc->mStateSampler;
104
105 ss->mMagFilter = RS_SAMPLER_LINEAR;
106 ss->mMinFilter = RS_SAMPLER_LINEAR;
107 ss->mWrapS = RS_SAMPLER_WRAP;
108 ss->mWrapT = RS_SAMPLER_WRAP;
109 ss->mWrapR = RS_SAMPLER_WRAP;
110}
111
112void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
113{
114 SamplerState * ss = &rsc->mStateSampler;
115
116 switch(param) {
117 case RS_SAMPLER_MAG_FILTER:
118 ss->mMagFilter = value;
119 break;
120 case RS_SAMPLER_MIN_FILTER:
121 ss->mMinFilter = value;
122 break;
123 case RS_SAMPLER_WRAP_S:
124 ss->mWrapS = value;
125 break;
126 case RS_SAMPLER_WRAP_T:
127 ss->mWrapT = value;
128 break;
129 case RS_SAMPLER_WRAP_R:
130 ss->mWrapR = value;
131 break;
132 }
133
134}
135
136RsSampler rsi_SamplerCreate(Context *rsc)
137{
138 SamplerState * ss = &rsc->mStateSampler;
139
140
141 Sampler * s = new Sampler(ss->mMagFilter,
142 ss->mMinFilter,
143 ss->mWrapS,
144 ss->mWrapT,
145 ss->mWrapR);
146 return s;
147}
148
Jason Sams39c8bc72009-05-28 15:37:57 -0700149void rsi_SamplerDestroy(Context *rsc, RsSampler vs)
150{
151 Sampler * s = static_cast<Sampler *>(vs);
152 s->decRef();
153
154}
155
156
Jason Sams326e0dd2009-05-22 14:03:28 -0700157}}