12. (25 points) Function
$$
f(x)=2(2 \cos x+1) \sin ^{2} x+\cos 3 x(x \in \mathbf{R}) \text {. }
$$

Find the maximum value of the function $f(x)$.
12. Notice,
$$
\begin{array}{l} 
f(x)=2 \sin 2 x \cdot \sin x+2 \sin ^{2} x+\cos (2 x+x) \\
= 2 \sin 2 x \cdot \sin x+2 \sin ^{2} x+\cos 2 x \cdot \cos x- \\
\sin 2 x \cdot \sin x \\
= \sin 2 x \cdot \sin x+\cos 2 x \cdot \cos x+2 \sin ^{2} x \\
= \cos (2 x-x)+2 \sin ^{2} x=2 \sin ^{2} x+\cos x \\
=-2 \cos ^{2} x+\cos x+2 \\
=-2\left(\cos x-\frac{1}{4}\right)^{2}+\frac{17}{8} \\
\leqslant \frac{17}{8} .
\end{array}
$$

When $\cos x=\frac{1}{4}$, the function $f(x)$ attains its maximum value $\frac{17}{8}$.